Write a code to reverse a string

Q11. Write a code to reverse a string

Given a string and the task is to write a algorithm and program to reverse a string. A string is sequence of characters terminated with a null character \0. So we are given a input string and we need to print its reverse order.
For Example: We have given input string as “ALFATECHLAB” and it’s reverse output we have to find out.
Input=”ALFATECHLAB”
Output=”BALHCETAFLA”

Explanation:
1. First declare a string variable and initialize an empty string to “ALFATECHLAB”.

2. Now we have to calculate the length of the string which we can do using loop(increment the value of count until the string is \0) or using the in-build function(strlen function).

3. Define two variables start = 0 and end = length -1.(Suppose “ALFATECHLAB” has a length of 11, then we will swap the 0th character with the 10th character, the 1st character with the 9th character, and the loop will iterate until length / 2 = 5).

4. Iterate the loop from i = 0 to i = length / 2 and increment the variable i by 1.

5. As we already define the start and end variable values, so now we just have to swap the characters one by one and increment the value of start += 1 and decrement the value of end- = 1 during each iteration.

6. After completing iteration print the string and output will be “BALHCETAFLA”.

Algorithm For Reverse A String

//Algorithm To Reverse A String
START
Step 1 -> Input string str1.
Step 2 -> calculate the length of the string.
Step 3 -> define start=0 and end=length-1;
Step 4 -> for i=0 to length/2.
temp=str1[start];
str1[start]=str1[end];
str1[end]=temp;
start+=1;
end-=1;
Loop End
step 5-> Display String str1.
STOP

Code For Reverse A String

Output

orip ni gnidoc

Recommended Programs
Program to find factorial of a number
Program to count number of digits in a number

Write a code to make a simple calculator

Q10. Write a code to make a simple calculator

Calculator :- A calculator is used to make mathematical calculations. In this program we will create a simple calculator that can perform an arithmetic operation (+, -, *, /)
For Example :-
Enter an operator (+, -, *, /): +
Enter two operands: 4, 5
4 + 5 = 9
So as we can see that first we have to choose which arithmetic operation we want to do, then after selecting two operands for which we have to do arithmetic calculation.

There is Following Algorithm For simple calculator

START
Step 1 : Initialize the two numbers.
Step 2 : Ask the user to enter an option by giving four options.
Step 3 : After getting the option from the user write if conditions for every operation based on the option.
Step 4 : Perform the respective operation.
Step 5 : Print the result.
STOP

There is Following code to swap two numbers without using third variable

Output

Enter an operator (+, -, *, /): +
Enter two operands: 4,5
4+5=9

Recommended Programs
Program to find factorial of a number
Program to count number of digits in a number

Write a code to swap two numbers without using temporary variable

Q9. Write a code to swap two numbers without using temporary variable

Solution : We can swap two numbers without using temporary variable. By using + and – operator we can swap two number.

Swapping two number :- Swap two numbers means exchange the values of two variables with each other.
For example:
Before Swapping : num1 = 30 and num2 = 25.
After Swapping : num1 = 25 and num2 = 30.

Logic :- Suppose we are given 2 numbers num1 = 30 and num2 = 25 and we have to swap these 2 numbers without using third variable. Then we have to perform below operations to swap the numbers.

1. Firstly we will add both the number and will assign to the first number.
num1 = num1 + num2
num1 = 30 + 25=45

2. Secondly we will subtract second number from the first number (updated value after step 1)
num2 = num1 – num2
num2 = 45 – 25
num2 = 30

3. Third step will be subtracting second number (updated value after step 2) from the first number (updated value from step 1)
num1 = num1 – num2
num1 = 45 – 30
num1 = 25

After the above 3 steps we will get our required result i.e we have swapped 2 numbers without using third variable.

There is Following Algorithm to swap two numbers without using third variable

//Algorithm To Swap Two Numbers Without Using Temporary Variable
START
Step 1 -> Take two integer as input num1 and num2.
Step 2 -> Print number before swapping
Step 3 -> num1 = num1 + num2;
Step 4 -> num2 = num1 – num2;
Step 5 -> num1 = num1 – num2;
Step 6 -> Print numbers after swapping
STOP

There is Following code to swap two numbers without using third variable

Output

Enter Two Numbers :
30,25
Number before swapping is 30 and 25
Number after swapping is 25 and 30

Recommended Programs
Program to find factorial of a number
Program to count number of digits in a number

Write a code to find whether a number is power of two or not

Q8. Write a code to find whether a number is power of two or not

Power of Two :- we knoe that a power of two is a number of the form 2^n where n is an integer.
For Example :-
Input = 128
Output = 128 is the power of 2
So as we can see that 2^7 = 128. So the number 128 is the power of two.

Logic : Here we use a mathematical concept of Logarithm, The most simple method for finding whether a no is power of two or not is to simply take the log of the number on base 2 and if you get an integer then the number is the power of 2.

There is Following Algorithm to find whether a number is power of two or not

START
Step 1 → Take integer variable num
Step 2 → find out the log number with base 2
Step 3 → if outcome is integer then DISPLAY number is power of 2.
Step 4 → Otherwise, DISPLAY number is not power of 2.
STOP

There is Following Code to find whether a number is power of two or notr

Output

128 is the power of two.

Recommended Programs
Program to find factorial of a number
Program to count number of digits in a number

Write a code to count the number of digits in a number

Q7. Write a code to count the number of digits in a number

A number is a mathematical object used to count, measure, and label. For eg 4, 5, 12, etc are numbers. Our task is to count the number of digits in the given number. Here we will see its algorithm and program. Let us understand the question with the help of an example.

For Example :- Suppose an input number is given as 4523 then the output will be 4 as there are 4 digits in the given number and digits are 4, 5, 2, 3,
Input :- 4523
Output :- 4

There is Following Algorithm for count the number of digits in a given number

START
step 1 : Input a number from user. Store it in some variable say num.
step 2 : Initialize another variable to store total digits say digit = 0.
step 3 : If num > 0 then increment count by 1 i.e. count++.
step 4 : Divide num by 10 to remove last digit of the given number i.e. num = num / 10.
step 5 : Repeat step 3 to 4 till num > 0 or num != 0.
STOP

There is Following Code for count the number of digits in a given number

Output

Number of digits : 8

Recommended Programs
Program to find factorial of a number
Program to count number of digits in a number