If Conditions

17. WRITE A C PROGRAM TO FIND GREATEST OF TWO NUMBERS USING SIMPLE IF

/*C program to find greatest of two numbers using simple if*/
#include <stdio.h>
int main()
{
int num1, num2;
/*
* Reads any two integer values from user
*/
printf("Enter any two numbers:\n");
scanf("%d %d", &num1, &num2);
/*
* Check if num1 > num2 or not and prints the maximum
*/
if(num1 > num2)
{
printf("%d is maximum", num1); 
}
else
{
printf("%d is maximum", num2);
}

return 0;
}
output
enter any two numbers25
36
36 is maximum


18. C PROGRAM TO FIND GENDER OF A PERSON USING IF ELSE

/*C program to find gender of a person using if else*/
#include<stdio.h>
#include<conio.h>
int main()
{
char a;
printf(“enter the value of character”);
scanf(“%c”,&a);
if(a==’m’)
{
printf(“the gender is male”);
}
else
{
printf(“the gender is female”);
}
getch();
return 0;
}
output
output 1:
enter the value of character
m
the gender is male

output 2:
enter the value of character

the gender is female


19. WRITE A C PROGRAM FIND LARGEST AMONG 3 VARIABLES USING NESTED IF

/* Find largest among 3 variables using nested if */
#include <stdio.h>
#include<conio.h>
int main()
{
int a,b,c;
printf("Enter 3 numbers: \n");
scanf("%d %d %d",&a,&b,&c);
if(a>=b)
{
if(a>=c)
printf("%d is largest",a);
else
printf("%d is largest",c);
}
else if(b>=c)
printf("%d is largest",b);
else
printf("%d is largest",c);
return 0; 
}
output
enter three numbers
10
20
30
30 is largest


20. WRITE A C PROGRAM TO EXPLAIN ELSE IF LADDER

/* C program to enter marks of five subjects and calculate percentage and grade*/
#include <stdio.h>
int main()
{
int phy, chem, bio, math, comp; //Five subjects
float per; 

/* Reads marks of five subjects from user */
printf("Enter five subjects marks: ");
scanf("%d%d%d%d%d", &phy, &chem, &bio, &math, &comp);


/* Calculates percentage */
per = (phy + chem + bio + math + comp) / 5.0;

printf("Percentage = %.2f\n", per);


/* Finds grade according to the percentage */
if(per >= 90)
{
printf("Grade A");
}
else if(per >= 80)
{
printf("Grade B");
}
else if(per >= 70)
{
printf("Grade C");
}
else if(per >= 60)
{
printf("Grade D");
}
else if(per >= 40)
{
printf("Grade E");
}
else
{
printf("Grade F");
}

return 0;
}
output
Enter five subjects marks: 95
95
97
98
90
Percentage = 95.00 
Grade A


21. WRITE A C PROGRAM TO CALCULATE ELECTRICITY BILL

C program to calculate electricity bill
Write a C program to enter electricity unit charges and calculate the total electricity bill according to the given condition:
For first 50 units Rs. 0.50/unit
For next 100 units Rs. 0.75/unit
For next 100 units Rs. 1.20/unit
For unit above 250 Rs. 1.50/unit
An additional surcharge of 20% is added to the bill
How to calculate electricity bill using if else in C programming. Program to find electricity bill using if else in C.
/*C program to calculate electricity bill*//* C program to calculate total electricity bill*/

#include <stdio.h>
int main()
{
int unit;
float amt, total_amt, sur_charge;

/*
* Reads unit charges from user
*/
printf("Enter total consumed units: ");
scanf("%d", &unit);


/*
* Calculates electricity bill according to given conditions
*/
if(unit <= 50)
{
amt = unit * 0.50;
}
else if(unit <= 150)
{
amt = 25 + ((unit-50)*0.75);
}
else if(unit <= 250)
{
amt = 100 + ((unit-150)*1.20);
}
else if(unit > 250)
{
amt = 220 + ((unit-250)*1.50);
}

/*
* Calculates total electricity bill
* after adding sur charges
*/
sur_charge = amt*0.20;
total_amt = amt+sur_charge;

printf("\nElectricity Bill = Rs. %.2f", total_amt);

return 0;
}
output
Enter total consumed units: 150
Electricity Bill = Rs. 125.00

Comments