Close
Close

Practice questions on Operators

Level 1

1.
What would be the value of 'a':

a. int a = 10/45*23%45/(45%4*21)

b. float a = 10+45.0*23-45+(4*21.0)


2.
True or false (0 or not):

a. 4>5 && 5>4

b. 4>5 || 5>4

c. (232+23*1233) || 0

d. (232+23*1233) && 0


3.
If a is 15, then what would be the value of:

printf("%d",++a);

printf("%d",a++);

printf("%d",--a);

printf("%d",a--);


4.
What would be the output of:

printf("%d\n",1==5==5);


5.
What is the error in:

int a;
3=a


Level 2

1.
Length and breadth of a rectangle are 5 and 7 respectively. Write a program to calculate the area and perimeter of the rectangle.
#include <stdio.h>
main(){
	int l = 5, b = 7;
	int area, perimeter;
	area = l*b;
	perimeter = 2*(l + b);
	printf("area: %d\n", area);
	printf("perimeter: %d\n", perimeter);
}

2.
Write a program to input the value of the radius of a circle from keyboard and then calculate its perimeter and area.
#include <stdio.h>
main(){
	int r, area, perimeter;
	printf("Enter radius of circle\n");
	scanf("%d", &r);
	area = 3.14 * r * r;
	perimeter = 2 * 3.14 * r;
	printf("area: %d\n", area);
	printf("perimeter: %d\n", perimeter);
}

3.
Write a program to enter a 4 digit number from keyboard. Add 8 to the number and then divide it by 3. Now, the modulus of that number is taken with 5 and then multiply the resultant value by 5. Display the final result.
#include <stdio.h>
main(){
	int n;
	printf("Enter a 4 digit number\n");
	scanf("%d", &n);
	float i;
	i = (((n + 8)/3)%5)*5;
	printf("%f\n", i);
}
									

4.
Now, solve the above question using assignment operators (eg. +=, -=, *=).

5.
Enter two numbers from keyboard. Write a program to check if the two numbers are equal.
#include <stdio.h>
main(){
	int x, y;
	scanf("%d %d", &x, &y);
	printf("%d\n", (x == y));     //value of the expression (x == y) is 1 when true and 0 when false
}

6.
Write a program to enter the values of two variables 'a' and 'b' from keyboard and then check if both the conditions 'a < 50' and 'a < b' are true.
#include <stdio.h>
main(){
	int a, b;
	scanf("%d %d", &a, &b);
	printf("%d\n", ((a < 50) && (a < b)));     //value of the expression is 1 when both the conditions are true and 0 when atleast one of the conditions is false
}

7.
Now solve the above question to check if atleast one of the conditions 'a < 50' and 'a < b' is true.
#include <stdio.h>
main(){
	int a, b;
	scanf("%d %d", &a, &b);
	printf("%d\n", ((a < 50) || (a < b)));     //value of the expression is 1 when either of the conditions is true and 0 when both are false
}

8.
If the marks of Robert in three subjects are 78,45 and 62 respectively (each out of 100 ), write a program to calculate his total marks and percentage marks.

9.
Write a program to enter the values of two variables from the keyboard and then interchange the values of the two variables. E.g.-
If entered value of x is 5 and y is 10 then
printf("%d %d",x,y )
should print 10 5.
#include <stdio.h>
main(){
	int x, y, temp;
	printf("Enter the values of the numbers\n");
	scanf("%d %d", &x, &y);
	printf("Before interchanging\n x = %d\n y = %d\n", x, y);
	temp = x;    //temp is assigned the value of x
	x = y;       //x is assigned the value of y
	y = temp;    //y is assigned the value of temp i.e. the original value of x
	printf("After interchanging\n x = %d\n y = %d\n", x, y);
}

10.
Take input of some length in meter and covert it to feet and inches.

11.
Write a program to convert Fahrenheit into Celsius.

12.
The total number of students in a class are 45 out of which 25 are boys. If 80% of the total students secured grade 'A' out of which 15 are boys, then write a program to calculate the total number of girls getting grade 'A'.
#include <stdio.h>
main(){
	int total, b, g;
	total = (80*45)/100;  //total students getting grade A
	b = 15;               //number of boys getting grade A
	g = total - b;        //number of girls getting grade A
	printf("number of girls getting grade A = %d\n", g);
	/*note that we did not write 'total = (80/100)*45'*/
}

Level 3

1.

Write a program to calculate the sum of the first and the second last digit of a 5 digit number entered from the keyboard.

#include <stdio.h>
main(){
	int n, first, second, third, forth, fifth, sum;
	printf("Enter a 5-digit number\n");
	scanf("%d", &n);
	/*Now we will take out each digit of this number and then finally add the first and the second last digits*/
	first = n/10000;     //first digit
	n = n%10000;

	second = n/1000;     //second digit
	n = n%1000;

	third = n/100;       //third digit
	n = n%100;

	forth = n/10;        //forth digit
	fifth = n%10;        //fifth digit

	sum = first + forth;
	printf("sum : %d\n", sum);
}

2.

Suppose you entered a 4 digit number. Write a program to display a number whose digits are 2 greater than the corresponding digits of the entered number.
For example, if he entered number is 5696, then the displayed number should be 7818.


3.

Write a program to calculate the sum of the digits of a 3-digit number which is entered from keyboard. E.g.-
INPUT : 132        OUTPUT : 6

#include <stdio.h>
main(){
	int n, first, second, third, sum;
	printf("Enter a 3-digit number\n");
	scanf("%d", &n);

	first = n/100;     //first digit
	n = n%100;

	second = n/10;     //second digit
	third = n%10;      //third digit

	sum = first + second + third;
	printf("sum : %d\n", sum);
}

4.

Write a program to reverse a 3-digit number which is entered from keyboard. E.g.-
INPUT : 132        OUTPUT : 231

#include <stdio.h>
main(){
	int n, first, second, third, reverse;
	printf("Enter a 3-digit number\n");
	scanf("%d", &n);
	/*finding the digits of the entered number n*/
	first = n/100;     //first digit
	n = n%100;

	second = n/10;     //second digit
	third = n%10;      //third digit

	/*reverse number */
	reverse = third*100 + second*10 + first;
	printf("reverse number : %d\n", reverse);
}

Ask Yours
Post Yours