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

Write a code to check whether the number is even or odd

Q6. Write a code to check whether the number is even or odd

Here you will find an algorithm and program to check whether a number is an even number or an odd number. we
know that :-

Even Number :- Even numbers are the numbers which are divisible by 2. For Example :- 2, 4, 6, …

Odd Number :- Odd numbers are the numbers which are not divisible by 2. For Example :- 1, 3, 5, …

Now let us understand what logic we need to use in this –
As we know that, to know number is even or odd we used to check its divisibility with 2, so here we will check the remainder we get on dividing the input number with 2 if we get remainder as 0 then it is divisible by 2 and hence it is even number else it is odd number.

For Example :-

Input : 12
Output : Even Number
Explanation : As we can see that the number given in input, which is 12, on dividing with 2 we get remainder 0 therefore 12 is Even Number.

Algorithm to check given number  is even or odd

\\Algorithm to check number is even or odd.
START
Step 1: [ Take Input ] Read: Number N
Step 2: Check: If N%2 == 0 Then
Print : N is an Even Number.
Else
Print : N is an Odd Number.
STOP

Code to check given number is even or odd

Output

12 is an Even number

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