C Conditional Statement Examples
Conditional statements in C help control program flow by making decisions based on conditions. Using if, else if, else, and nested if-else, programs execute different code blocks depending on whether conditions are true or false.
26. C Program to Check Even or Odd
#include <stdio.h>
int main(void){
int n;
printf("Enter number: ");
scanf("%d",&n);
if(n%2==0){
printf("Even\n");
}else{
printf("Odd\n");
}
return 0;
}
27. C Program to Check Positive, Negative or Zero
#include <stdio.h>
int main(void){
int n;
printf("Enter number: ");
scanf("%d",&n);
if(n>0){
printf("Positive\n");
}else if(n<0){
printf("Negative\n");
}else{
printf("Zero\n");
}
return 0;
}
28. C Program to Find Largest of Two Numbers
#include <stdio.h>
int main(void){
int a,b;
printf("Enter two numbers: ");
scanf("%d%d",&a,&b);
if(a>b){
printf("%d is larger\n",a);
}else if(b>a){
printf("%d is larger\n",b);
}else{
printf("Both are equal\n");
}
return 0;
}
29. C Program to Find Largest of Three Numbers
#include <stdio.h>
int main(void){
int a,b,c;
printf("Enter three numbers: ");
scanf("%d%d%d",&a,&b,&c);
if(a>=b && a>=c){
printf("%d is largest\n",a);
}else if(b>=a && b>=c){
printf("%d is largest\n",b);
}else{
printf("%d is largest\n",c);
}
return 0;
}
30. C Program to Check Divisible by 5 and 11
#include <stdio.h>
int main(void){
int n;
printf("Enter number: ");
scanf("%d",&n);
if(n%5==0 && n%11==0){
printf("Divisible by 5 and 11\n");
}else{
printf("Not divisible by both\n");
}
return 0;
}
31. C Program to Check Profit or Loss Calculation
#include <stdio.h>
int main(void){
float cp,sp;
printf("Enter cost and selling price: ");
scanf("%f%f",&cp,&sp);
if(sp>cp){
printf("Profit: %.2f\n",sp-cp);
}else if(cp>sp){
printf("Loss: %.2f\n",cp-sp);
}else{
printf("No profit no loss\n");
}
return 0;
}
32. C Program to Find Absolute Value
#include <stdio.h>
int main(void){
int n;
printf("Enter number: ");
scanf("%d",&n);
if(n<0){
n=-n;
}
printf("Absolute value: %d\n",n);
return 0;
}
33. C Program to Make Electricity Bill Calculator
#include <stdio.h>
int main(void){
float units,bill;
printf("Enter units: ");
scanf("%f",&units);
if(units<=100){
bill=units*1.5;
}else if(units<=200){
bill=100*1.5+(units-100)*2;
}else{
bill=100*1.5+100*2+(units-200)*3;
}
printf("Bill = ₹%.2f\n",bill);
return 0;
}
34. C Program to Check Given Character is a Vowel or Consonant
#include <stdio.h>
int main(void){
char ch;
printf("Enter letter: ");
scanf(" %c",&ch);
if(ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U'||ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'){
printf("Vowel\n");
}else{
printf("Consonant\n");
}
return 0;
}
35. C Program to Alphabet Check
#include <stdio.h>
int main(void){
char ch;
printf("Enter character: ");
scanf(" %c",&ch);
if((ch>='A' && ch<='Z')||(ch>='a' && ch<='z')){
printf("Alphabet\n");
}else{
printf("Not alphabet\n");
}
return 0;
}
36. C Program to Check Uppercase or Lowercase
#include <stdio.h>
int main(void){
char ch;
printf("Enter character: ");
scanf(" %c",&ch);
if(ch>='A' && ch<='Z'){
printf("Uppercase\n");
}else if(ch>='a' && ch<='z'){
printf("Lowercase\n");
}else{
printf("Not alphabet\n");
}
return 0;
}
37. C Program to Character Type Check
#include <stdio.h>
int main(void){
char ch;
printf("Enter character: ");
scanf(" %c",&ch);
if((ch>='A' && ch<='Z') || (ch>='a' && ch<='z')){
printf("Alphabet\n");
}else if(ch>='0' && ch<='9'){
printf("Digit\n");
}else{
printf("Special character\n");
}
return 0;
}
38. C Program to Leap Year Check
#include <stdio.h>
int main(void){
int y;
printf("Enter year: ");
scanf("%d",&y);
if((y%4==0 && y%100!=0) || y%400==0){
printf("Leap year\n");
}else{
printf("Not leap year\n");
}
return 0;
}
39. C Program to Print Student Grade from Marks
#include <stdio.h>
int main(void){
int marks;
printf("Enter marks: ");
scanf("%d",&marks);
if(marks>=90){
printf("Grade A\n");
}else if(marks>=75){
printf("Grade B\n");
}else if(marks>=60){
printf("Grade C\n");
}else if(marks>=40){
printf("Grade D\n");
}else{
printf("Fail\n");
}
return 0;
}
40. C Program to Triangle Validity Check
#include <stdio.h>
int main(void){
int a,b,c;
printf("Enter 3 sides: ");
scanf("%d%d%d",&a,&b,&c);
if(a+b>c && b+c>a && c+a>b){
printf("Valid triangle\n");
}else{
printf("Invalid triangle\n");
}
return 0;
}
41. C Program to Print Roots of Quadratic Equation
#include <stdio.h>
#include <math.h>
int main(void){
float a,b,c,d;
printf("Enter a,b,c: ");
scanf("%f%f%f",&a,&b,&c);
d=b*b-4*a*c;
if(d>0){
printf("Two real roots: %.2f and %.2f\n",(-b+sqrt(d))/(2*a),(-b-sqrt(d))/(2*a));
}else if(d==0){
printf("One real root: %.2f\n",-b/(2*a));
}else{
printf("Imaginary roots\n");
}
return 0;
}
42. C Program to Print Day of Week (Using Number)
#include <stdio.h>
int main(void){
int d;
printf("Enter day number (1-7): ");
scanf("%d",&d);
if(d==1){
printf("Monday\n");
}else if(d==2){
printf("Tuesday\n");
}else if(d==3){
printf("Wednesday\n");
}else if(d==4){
printf("Thursday\n");
}else if(d==5){
printf("Friday\n");
}else if(d==6){
printf("Saturday\n");
}else if(d==7){
printf("Sunday\n");
}else{
printf("Invalid\n");
}
return 0;
}
43. C Program to Print Temperature Category
#include <stdio.h>
int main(void){
float t;
printf("Enter temperature: ");
scanf("%f",&t);
if(t<0){
printf("Freezing\n");
}else if(t<=20){
printf("Cold\n");
}else if(t<=35){
printf("Warm\n");
}else{
printf("Hot\n");
}
return 0;
}
44. C Program to Make Simple Calculator (If-Else)
#include <stdio.h>
int main(void){
char op;
float a,b;
printf("Enter operator (+,-,*,/): ");
scanf(" %c",&op);
printf("Enter two numbers: ");
scanf("%f%f",&a,&b);
if(op=='+'){
printf("Result = %.2f\n",a+b);
}else if(op=='-'){
printf("Result = %.2f\n",a-b);
}else if(op=='*'){
printf("Result = %.2f\n",a*b);
}else if(op=='/'){
printf("Result = %.2f\n",a/b);
}else{
printf("Invalid operator\n");
}
return 0;
}
45. C Program to Check Whether a Number is Even, Odd, Whole, or Natural
#include <stdio.h>
int main(void){
int num;
// Prompt the user for input
printf("Enter a number: ");
scanf("%d",&num);
// Check if number is whole (0 and above)
if(num>=0){
printf("It is a Whole number.\n");
// Check if number is natural (positive integers only)
if(num>0){
printf("It is also a Natural number.\n");
}
// Check even or odd
if(num%2==0){
printf("It is an Even number.\n");
}else{
printf("It is an Odd number.\n");
}
}else{
printf("It is neither a Whole number nor a Natural number.\n");
}
return 0;
}
46. C Program to Find Nature of Triangle (Nested if–else)
#include <stdio.h>
int main() {
int a, b, c;
printf("Enter three sides of triangle: ");
scanf("%d %d %d", &a, &b, &c);
if (a + b > c && a + c > b && b + c > a) {
// Valid triangle
if (a == b && b == c) {
printf("Equilateral Triangle\n");
} else if (a == b || b == c || a == c) {
printf("Isosceles Triangle\n");
} else {
printf("Scalene Triangle\n");
}
} else {
printf("Not a valid triangle\n");
}
return 0;
}
47. C Program to Find Menu Driven Calculator (Nested if–else)
#include <stdio.h>
int main() {
int choice;
float a, b;
// input comes silently from STDIN
scanf("%d", &choice);
scanf("%f %f", &a, &b);
printf("Menu:\n1.Add\n2.Subtract\n3.Multiply\n4.Divide\n");
if (choice == 1)
printf("Result = %.2f\n", a + b);
else if (choice == 2)
printf("Result = %.2f\n", a - b);
else if (choice == 3)
printf("Result = %.2f\n", a * b);
else if (choice == 4 && b != 0)
printf("Result = %.2f\n", a / b);
else
printf("Error\n");
return 0;
}
48. C Program to Check Leap Year (Nested if–else)
#include <stdio.h>
int main() {
int year;
printf("Enter year: ");
scanf("%d", &year);
if (year % 4 == 0) {
// Year divisible by 4
if (year % 100 == 0) {
// Century year
if (year % 400 == 0) {
printf("Leap Year\n");
} else {
printf("Not a Leap Year\n");
}
} else {
printf("Leap Year\n");
}
} else {
printf("Not a Leap Year\n");
}
return 0;
}
49. C Program to Check Eligibility to Vote (Nested if)
#include <stdio.h>
int main() {
int age;
char citizenship;
printf("Enter age: ");
scanf("%d", &age);
printf("Are you an Indian citizen? (Y/N): ");
scanf(" %c", &citizenship);
if (age >= 18) {
// Nested condition for citizenship
if (citizenship == 'Y' || citizenship == 'y') {
printf("You are eligible to vote\n");
} else {
printf("Citizenship required to vote\n");
}
} else {
printf("You are not eligible to vote\n");
}
return 0;
}
50. C Program to Check Largest of Three Numbers (Nested if–else)
#include <stdio.h>
int main() {
int a, b, c;
printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);
// First if checks a
if (a > b) {
// Nested if checks a with c
if (a > c) {
printf("Largest number is %d\n", a);
} else {
printf("Largest number is %d\n", c);
}
} else {
// Nested if checks b with c
if (b > c) {
printf("Largest number is %d\n", b);
} else {
printf("Largest number is %d\n", c);
}
}
return 0;
}