Home
  • More

    More My Portfolio

Operator Related Problems

“Truth can only be found in one place: the code.”
- Robert C. Martin

Problem #1
  • Problem Statement
  • Solution
Program that will take two numbers X and Y as inputs, 
then calculate and print the values of their addition,
subtraction, multiplication, division (quotient and
reminder). 

Sample input (X,Y)	Sample output
5     10	     	Addition: 15
			Subtraction: -5
			Multiplication: 50
			Quotient : 0
			Reminder: 5

-5     10.5	     	Addition: 5.5
			Subtraction: -15.5
			Multiplication: -52.5
			Quotient: 0
			Reminder: -48
	#include
	#include
	#include

	int main(){
		double a,b,add,sub,mul,quo,rem;
		scanf("%lf %lf",&a,&b);
		add=a+b;
		sub=a-b;
		mul=a*b;
		quo=a/b;
		printf("\nAddition:%.1lf",add);
		printf("\nSubtraction:%.1lf",sub);
		printf("\nMultiplication:%.1lf",mul);
		printf("\nQuotient:%d",(int)quo);
		rem=(quo-(int)quo)*10;
		printf("\nRemainder:%d",(int)rem);

		return 0;
		getch();
	}
	
Problem #2
  • Problem Statement
  • Solution
Program that will calculate the area of a
circle having radius r. 
Area, A = Pi * r *r

Sample input (r)	Sample output
5			Area:  31.4
10.5			Area:  65.94
#include

int main(){
	float r,result;
	float Pi=3.1416;
	scanf("%f",&r);
	result=Pi*(r*r);
	printf("Area:%.2f",result);
	return 0;
}
	
Problem #3
  • Problem Statement
  • Solution
Program that will take two numbers (a, b) as inputs and
compute the value of the equation – (Without using math.h)

X =  (3.31 * a2 + 2.01 * b3) / (7.16 * b2 + 2.01 * a3)

Sample input (a, b)	Sample output
5         10.5		X = 2.315475
100      -250		X = -12.766287
#include

int main(){
	float a,b,x;
	scanf("%f %f",&a,&b);
	x = (3.31 * (a*a) + 2.01 * (b*b*b) ) / (7.16 * (b*b) + 2.01 * (a*a*a) );
	printf("X = %f",x);
	return 0;
}
Problem #4
  • Problem Statement
  • Solution
Program that will increment and decrement a number
X by 1 inside the printf function.
(Use ++ and - - operators)

Sample input(X) 	Sample output
5	 		X++ :    5
			++X :    6
			X-- :    5
			--X :    4
-5			X++ :   -5
			++X :   -4
			X-- :   -5
			--X :   -6

#include<stdio.h>
int main(){
	int x,y;
	scanf("%d",&x);
	y=x;
	printf("\nX++:%d",x++);
	x=y;
	printf("\n++X:%d",++x);
	x=y;
	printf("\nX--:%d",x--);
	x=y;
	printf("\n--X:%d",--x);
	return 0;
}
Problem #5
  • Problem Statement
  • Solution
Program that will increment and decrement a number X by Y.
 (Use += and -= operators)

Sample input(X,Y)	Sample output
5     10		Incremented Value:    10
			Decremented Value:   -5
-5      5		Incremented Value:      0
			Decremented Value:   -10

#include<stdio.h>
int main(){
	int x,y,a,b;
	scanf("%d %d",&x,&y);
	a=x;
	b=y;
	printf("\nIncremented Value:%d",(x+=y));
	x=a;
	y=b;
	printf("\nDecremented Value:%d",(x-=y));
	return 0;
}
Problem #6
  • Problem Statement
  • Solution
Program that will multiply and divide a number X by Y. (Use *= and /= operators)

Sample input(X,Y)	Sample output
56       10		Multiplication:    560
			Division:   5
-56      -10		Multiplication:    560
			Division:   5

#include<stdio.h>
int main(){
	int x,y,a,b;
	scanf("%d %d",&x,&y);
	a=x;
	b=y;
	printf("\nMultiplication:%d",(x*=y));
	x=a;
	y=b;
	printf("\nDivision:%d",(x/=y));
	return 0;
}
Problem #7
  • Problem Statement
  • Solution
Program that will declare and initialize an integer and 
a floating point number. Then it will perform floating to
integer and integer to floating conversions using
(a)	Assignment operation
(b)	Type casting

Sample input		Sample output
-150      123.125	Assignment:   123.125000 assigned to an int produces 123
			Assignment:  -150 assigned to a float produces -150.000000
			Type Casting: (float) -150 produces -150.000000
			Type Casting: (int) 123.125 produces -123

#include<stdio.h>
int main(){
	int a;
	float b;
	scanf("%d %f",&a,&b);
	int a1=a;
    float b1=b;
	a=b;
	printf("\nAssignment: %f assigned to an int produces: %d",b,a);
	a=a1;
	b=a;
	printf("\nAssignment: %d assigned to a float produces %f",a,b);
	b=b1;
	printf("\nType Casting: (float) %d produces %f",a,(float)a);
	printf("\nType Casting: (int) %f produces %d",b,(int)b);
	return 0;
}
Problem #8
  • Problem Statement
  • Solution
Program that will take two numbers as inputs and
print the maximum value. (Using conditional operator - ?)

Sample input (x, y)	Sample output
20     100		Max: 100
50     -20		Max: 50

#include<stdio.h>
int main(){
	int a,b;
	scanf("%d %d",&a,&b);
	(a>b)?printf("Max:%d",a):printf("Max:%d",b);
	return 0;
}
Problem #9
  • Problem Statement
  • Solution
Program that will evaluate the following equations - 
X = a – b / 3 + c * 2 – 1
Y = a – ( b / ( 3 + c ) * 2) - 1 
Z = a – ( ( b / 3) + c * 2) - 1

Sample input (a, b, c)		Sample output
9     12     3			X = 10
				Y = 4
				Z = -15

#include<stdio.h>
int main(){
	int a,b,c,x,y,z;
	scanf("%d %d %d",&a,&b,&c);

	x = a-(b/3)+(c*2)-1;
	y = a-(b/(3+c)*2)-1;
	z = a-((b/3)+c*2)-1;

	printf("\nX = %d",x);
	printf("\nY = %d",y);
	printf("\nZ = %d",z);
	return 0;
}
© , Designed & Coded by Robiuddin Robi. Using Now UI Kit Pro.

Have you tried once by yourself first?