Home
  • More

    More My Portfolio

Condition Related Problems

“Take positive care of your mind, and it would surely take positive care of your life.” ― Edmond Mbiaka

Problem #1
  • Problem Statement
  • Solution
WPA that will decide whether a number is positive or not.

Sample input 	Sample output
100				Positive
-11.11          Negative
0				Positive
#include<stdio.h>

int main(){
	int a;

	scanf("%d",&a);

	if(a>=0) printf("Positive");

	else printf("Negative");

	return 0;
}
	
Problem #2
  • Problem Statement
  • Solution
Program that will decide whether a number is even or odd.

Sample input 			Sample output
50							Even
-77							Odd
0							Even
#include<stdio.h>

int main(){
	int a;
	scanf("%d",&a);
	if(a%2==0) printf("Even");
	else printf("Odd");
	return 0;
}
	
Problem #3
  • Problem Statement
  • Solution
Program that will take an integer of length one from the terminal and then display the digit in English.

Sample input 	Sample output
9		nine 
0		zero
#include<stdio.h>

int main(){
	int a;
	scanf("%d",&a);
	if(a>=0&&a<=9){
		switch(a){
			case 0: printf("Zero"); break;
			case 1: printf("One"); break;
			case 2: printf("Two"); break;
			case 3: printf("Three"); break;
			case 4: printf("Four"); break;
			case 5: printf("Five"); break;
			case 6: printf("Six"); break;
			case 7: printf("Seven"); break;
			case 8: printf("Eight"); break;
			case 9: printf("Nine"); break;
		}
	}
}
Problem #4
  • Problem Statement
  • Solution
Program that will check whether a triangle is valid or not,
 when the three angles (angle value should be such that,
 (0 < value < 180) of the triangle are entered through the keyboard. 
### [Hint: A triangle is valid if the sum of all the three angles is equal to 180 degrees.]

    Sample input 			Sample output
90       45        45			Yes
30       110       40			Yes
160      20        30			No
0        180       0			No

#include<stdio.h>

int main(){
	int a,b,c;
	scanf("%d %d %d",&a,&b,&c);
	if(a>0&&a<180&&b>0&&b<180&&c>0&&c<180){
		((a+b+c)==180)? printf("Yes"):printf("No");
	}
	else printf("No");
	return 0;
}
Problem #5
  • Problem Statement
  • Solution
Program that will take two numbers X & Y as inputs and decide whether X is greater than/less than/equal to Y.

Sample input (X,Y)		        Sample output
5   -10					5 is greater than -10
5    10					5 is less than 10
5     5					5 is equal to 5

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

	(a>b)?printf("%d is greater than %d",a,b):(a==b)?printf("%d is equal of %d",a,b):printf("%d is less than %d",a,b);
	return 0;
}
Problem #6
  • Problem Statement
  • Solution
Program that will decide whether a year is leap year or not.

	### Yes,if(Year%4==0&&year%100!=0)||(Year%400==0)

Sample input 	   Sample output
2000			Yes
2004			Yes
2014			No

#include<stdio.h>
int main(){
	int year;
	scanf("%d",&year);

	((year%4==0 && year%100!=0)||(year%400==0))?printf("Yes"):printf("No");
	return 0;
}
Problem #7
  • Problem Statement
  • Solution
Program that will categorize a single character that is entered at the terminal,
 whether it is an alphabet, a digit or a special character.

 ### (Restriction: Without math.h)

Sample input 	             Sample output
z				Alphabet 
A				Alphabet
8				Digit
*				Special

#include<stdio.h>
int main(){
	char a;
	scanf("%c",&a);
	(a>='0'&&a<='9')?printf("Digit"):(a>='a'&&a<='z'||a>='A'&&a<='Z')?printf("Alphabet"):printf("Special");
	return 0;
}
Problem #8
  • Problem Statement
  • Solution
Program that will evaluate simple expressions of the form-  

		       

		; where operators are (+, - , *, /) 

And if the operator is “/”, then check if  nonzero or not.

Sample input 	            sample output
100     *      55.5	    Multiplication:  5550
100     /      -5.5	    Division:   -18.181818
100    /        0       Division:    Zero as divisor is not valid!

#include<stdio.h>
int main(){
    float number1,number2,result;
    char op;
    scanf("%f  %c %f",&number1,&op,&number2);
      if(op=='+'){
       result=number1+number2;
       printf("addition:%f",result);}
    else if(op=='-'){
       result=number1-number2;
       printf("substraction:%f",result);}
    else if(op=='*'){
       result=number1*number2;
       printf("multiplication:%f",result);}
    else if(op=='/'){
       if(number2!=0){
       result=number1/number2;
       printf("division:%f",result);}
       else
        printf("division: zero as a divisor is not valid!");
      }
    return 0;
}
Problem #9
  • Problem Statement
  • Solution
Program that will take the final score of a student in a particular subject as input and find his/her grade. 

Marks	Letter Grade	Marks	Letter Grade		Marks		Letter Grade
90-100	      A 		70-73		C+ 			Less than 55		F 
86-89	      A- 		66-69		C 		
82-85	      B+ 		62-65		C-		
78-81	      B 		58-61		D+ 		
74-77	      B- 		55-57		D 		 

Sample input			Sample output
91.5					Grade: A
50					Grade: F

#include<stdio.h>
int main(){
	float a;
	scanf("%f",&a);
	if(a>=90&&a<=100){
		printf("A");
	}
	else if(a>=86&&a<=89){
		printf("A-");
	}
	else if(a>=82&&a<=85){
		printf("B+");
	}
	else if(a>=78&&a<=81){
		printf("B");
	}
	else if(a>=74&&a<=77){
		printf("B-");
	}
	else if(a>=70&&a<=73){
		printf("C+");
	}
	else if(a>=66&&a<=69){
		printf("C");
	}
	else if(a>=62&&a<=65){
		printf("C-");
	}
	else if(a>=58&&a<=61){
		printf("D+");
	}
	else if(a>=55&&a<=57){
		printf("D");
	}
	else if(a<55){
		printf("F");
	}
	else printf("Wrong Input");
	return 0;
}
Problem #10
  • Problem Statement
  • Solution
Program that will construct a menu for performing arithmetic operations. 
The user will give two real numbers (a, b) on which the arithmetic 
operations will be performed and an integer number (1 <= Choice <= 4) 
as a choice. Choice-1, 2, 3, 4 are for performing addition, subtraction, 
multiplication, division (quotient) respectively.

Sample input (a, b, Choice)	Sample output
5     10
3					Multiplication: 50
-5     10.5
4					Quotient: 0

#include<stdio.h>

int main(){
	float a,b;
	int i;
	scanf("%f %f %d",&a,&b,&i);
	
	switch(i){
		case 1:
			printf("Addition:%.2f",(a+b)); break;
		case 2:
			printf("Subtraction:%.2f",(a-b)); break;
		case 3:
			printf("Multiplication:%.2f",(a*b)); break;
		case 4:
			printf("Quotient:%.0f",(a/b)); break;
	}
	return 0;
}
Problem #11
  • Problem Statement
  • Solution
Program that will construct a menu for performing arithmetic operations. 
The user will give two real numbers (a, b) on which the arithmetic operations will be performed and 
an integer number (1 <= Choice <= 4) as a choice. Choice-1, 2, 3, 4 are for performing addition, 
subtraction, multiplication, division respectively. 

### If Choice-4 is selected, again the program will ask for another choice 
(1 <= Case <=2), where Case-1, 2 evaluate quotient and reminder respectively.


Sample input			Sample output
5     10
3				Multiplication: 50
-5     10.5
4
1				Quotient: 0

-5     10.5
4
2				Reminder:  -48

#include<stdio.h>

int main(){
	float a,b;
	int i,j;
	scanf("%f %f %d",&a,&b,&i);
	
	switch(i){
		case 1:
			printf("Addition:%.2f",(a+b)); break;
		case 2:
			printf("Subtraction:%.2f",(a-b)); break;
		case 3:
			printf("Multiplication:%.2f",(a*b)); break;
		case 4:
			scanf("%d",&j);
			switch(j){
				case 1:
					printf("Quotient:%.0f",(a/b)); break;
				case 2:
					printf("Reminder:%f",(a-(b*(int)(a/b))));break;
			}break;
			//
	}
	return 0
}
Problem #12
  • Problem Statement
  • Solution
Program that will construct a menu for performing arithmetic operations. 
The user will give two real numbers (a, b) on which the arithmetic operations will be performed and 
an integer number (1 <= Choice <= 4) as a choice. Choice-1, 2, 3, 4 are for performing addition, 
subtraction, multiplication, division respectively. 

If Choice-4 is selected, the program will check if b is nonzero. 

If the check is true, the program will ask for another choice (1 <= Case <=2), 
where Case-1, 2 evaluate quotient and reminder respectively. 
If the check is false, it will print an error message “Error: Divisor is zero” and halt.

Sample input		Sample output
5     10
3			Multiplication: 50
-5     10.5
4
2			Reminder: -48

-5     0
4			Error: Divisor is zero

#include<stdio.h>

int main(){
	float a,b;
	int i,j;
	scanf("%f %f %d",&a,&b,&i);
	
	switch(i){
		case 1:
			printf("Addition:%.2f",(a+b)); break;
		case 2:
			printf("Subtraction:%.2f",(a-b)); break;
		case 3:
			printf("Multiplication:%.2f",(a*b)); break;
		case 4:
			if(b != 0){
				scanf("%d",&j);
			switch(j){
				case 1:
					printf("Quotient:%.0f",(a/b)); break;
				case 2:
					printf("Reminder:%.0f",(((a/b)-(int)(a/b))*100));break;
				sol:
					printf("Wrong Input");break;
			}
			}
			else printf("Error:Divisor is zero");break;
		sol:
			printf("Wrong input"); break;
	}
	return 0;
}
Special fun
  • Problem Statement
  • Solution
Program for “Guessing Game”:
Player-1 picks a number X and Player-2 has to guess that number within N = 3 tries.
 For each wrong guess by Player-2, the program prints “Wrong, N-1 Chance(s) Left!” 
 If Player-2 successfully guesses the number, the program prints “Right, Player-2 wins!” 
 and stops allowing further tries (if any left). Otherwise after the completion of N = 3 wrong tries,
 the program prints “Player-1 wins!” and halts.

### [ Restriction: Without using loop/break/continue]
### [Hint: Use flag ]
 
Sample input(X, n1, n2, n3) 	    Sample output
5  12    8    5	 		Wrong, 2 Chance(s) Left!
				Wrong, 1 Chance(s) Left!
				Right, Player-2 wins!
						
						
100 50    100			Wrong, 2 Chance(s) Left!
				Right, Player-2 wins!
						
20 12    8    5			Wrong, 2 Chance(s) Left!
				Wrong, 1 Chance(s) Left!
				Wrong, 0 Chance(s) Left!
				Player-1 wins!

#include<stdio.h>
int main(){
	int p1,n1,n2,n3;
	scanf("%d",&p1);
	scanf("%d",&n1);
	if(n1==p1){
		printf("Right,Player-2wins!\n"); return 0;
	}
	else{
		printf("Wrong,2 Chance(s) Left!\n");
		scanf("%d",&n2);
		if(n2==p1){
			printf("Right,Player-2wins!\n"); return 0;
		}
		else{
			printf("Wrong,1 Chance(s) Left!\n");
			scanf("%d",&n3);
		if(n3==p1){
			printf("Right,Player-2wins!\n"); return 0;
		}
		else{
			printf("Wrong,0 Chance(s) Left!\n");
			printf("Player-1 wins!\n"); return 0;
			}
		}
	}
}
© , Designed & Coded by Robiuddin Robi. Using Now UI Kit Pro.

Have you tried once by yourself first?