Table of Contents
Programming Practices for If-Else
1. C program to find the greatest number among three numbers :
#include <stdio.h>
int main(void)
{
int a, b, c;
printf(" Enter the number1 = ");
scanf("%d", &a);
printf("\n Enter the number2 = ");
scanf("%d", &b);
printf("\n Enter the number3 = ");
scanf("%d", &c);
if (a > b)
{
if (a > c)
{
printf("\n Greatest number = %d \n",a);
}
else
{
printf("\n Greatest number = %d \n",c);
}
}
else if (b > c)
{
printf("\n Greatest number = %d \n",b);
}
else
{
printf("\n Greatest number = %d \n",c);
}
return 0;
}
***************************************************************
2.C program to find out whether a given year is a leap year or not :
#include <stdio.h>
int IsLeapYear(int year)
{
// Function to check leap year.
if (year % 4 != 0)
return 0;
if (year % 100 != 0)
return 1;
return (year % 400) == 0;
}
int main()
{
unsigned int uiYear=0;
printf("Enter the year:");
scanf("%u",&uiYear);
(IsLeapYear(uiYear)? printf("Leap Year."): printf("Not Leap Year."));
return 0;
}
**********************************************************************
3.C program to convert temperature from Fahrenheit to Celsius and Celsius to Fahrenheit :
The basic formula to convert Fahrenheit and Celsius to each other.
Celsius to Fahrenheit: (°C × 9/5) + 32 = °F
Fahrenheit to Celsius: (°F − 32) x 5/9 = °C
#include <stdio.h>
int main()
{
float fh,cl;
char ch;
printf("\n\n Press c to convert temperature from Fahrenheit to Celsius.");
printf("\n\n Press f to convert temperature from Celsius to Fahrenheit.");
printf("\n\n Enter your choice (c, f): ");
scanf("%c",&ch);
if((ch =='c') ||(ch =='C'))
{
printf("\n\nEnter temperature in Fahrenheit: ");
scanf("%f",&fh);
cl= (fh - 32) / 1.8;
printf("\n\nTemperature in Celsius: %.2f\n\n",cl);
}
else if((ch =='f') ||(ch =='F'))
{
printf("\n\nEnter temperature in Celsius: ");
scanf("%f",&cl);
fh= (cl*1.8)+32;
printf("\n\nTemperature in Fahrenheit: %.2f\n\n",fh);
}
else
{
printf("\n\nInvalid Choice !!!\n\n");
}
return 0;
}
*******************************************************************
4. C program to check sign of a given number:
The MSB bit of a number defines their sign. If the MSB bit is set, the number will be negative.
#include <stdio.h>
int main()
{
int sign = 0;
int data = 0;
printf("\n\n Enter the number = ");
scanf("%d",&data); //Get the number
sign = (data > 0) - (data < 0); // check the sign of the number
if(sign == 1)
{
printf("\n\n Enter number is a positve number\n");
}
else if(sign == -1)
{
printf("\n\n Enter number is a negative number\n");
}
else
{
printf("\n\n Enter number is zero\n");
}
return 0;
}
********************************************************************
5.C program to check whether triangle is equilateral, scalene or isosceles:
Before writing the program, we should know the properties of isosceles, equilateral and scalene triangles.
Isosceles triangle: In geometry, an isosceles triangle is a triangle that has two sides of equal length.
Equilateral triangle: In geometry, an equilateral triangle is a triangle in which all three sides are equal.
Scalene triangle: A scalene triangle is a triangle that has three unequal sides.
#include <stdio.h>
int main(void)
{
int triSide1, triSide2, triSide3;
/* Get sides of a triangle from the user */
printf("\n Enter first side of triangle: = ");
scanf("%d", &triSide1);
printf("\n Enter second side of triangle: = ");
scanf("%d",&triSide2);
printf("\n Enter third side of triangle: = ");
scanf("%d",&triSide3);
if((triSide1==triSide2) && (triSide2==triSide3))
{
/* If all sides are equal, then Equilateral triangle*/
printf("\n Equilateral triangle.\n\n");
}
else if((triSide1==triSide2) || (triSide1==triSide3) || (triSide2==triSide3))
{
/* If two sides are equal, then Isosceles triangle*/
printf("\n Isosceles triangle.\n\n");
}
else
{
/* If none sides are equal, then Scalene triangle*/
printf("\n Scalene triangle.\n\n");
}
return 0;
}
***************************************************************************
6. C program to enter week number and print day of week:
#include <stdio.h>
int main()
{
/* Declare constant name of weeks */
const char * WEEKS[] = { "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday",
"Sunday"};
int week;
/* Input week number from user */
printf("Enter week number (1-7): ");
scanf("%d", &week);
if(week > 0 && week < 8)
{
/* Print week name using array index */
printf("%s", WEEKS[week-1]);
}
else
{
printf("Invalid input! Please enter week number between 1-7.");
}
return 0;
}
*************************************************************************
7.C program to check whether the triangle is valid or not if sides are given:
#include <stdio.h>
int main(void)
{
int a, b, c, sum; //variable to store angles
// Get three sides of triangle from the user
printf("\n Enter 1 side of triangle: = ");
scanf("%d", &a);
printf("\n Enter 2 side of triangle: = ");
scanf("%d", &b);
printf("\n Enter 3 side of triangle: = ");
scanf("%d", &c);
//check validity of triangle
if((a + b > c) && (a + c > b) && (b + c > a))
{
printf("\n Valid triangle.\n\n");
}
else
{
printf("\n Not valid triangle.\n\n");
}
return 0;
}
************************************************************************
8.C program to enter student marks and find percentage and grade:
A college has the following rules for grading system:
1. Below 25 – F
2. 25 to 45 – E
3. 45 to 50 – D
4. 50 to 60 – C
5. 60 to 80 – B
6. Above 80 – A
Ask the user to enter mark of 5 subjects and print the corresponding grade.
#include <stdio.h>
int main(void)
{
float subMark[5]= {0};
float per = 0.0;
int i = 0;
float sum = 0.0;
/* Get subject Marks from user */
for(i=0 ; i <5; i++) { printf("\n Enter subject %d marks: ",i); scanf("%f",&subMark[i]); //check validty of marks if(subMark[i]> 100)
{
printf("\n Enter valid number ! \n");
i--;
}
else
sum += subMark[i];
}
/* total marks */
printf("\n total marks = %f\n", sum);
/* Calculate percentage */
per = (sum / 500.0)*100;
printf("\n Percentage = %.2f\n", per);
/*Grade according to the percentage */
if(per >= 80)
{
printf("\n Grade A");
}
else if(per >= 60)
{
printf("\n Grade B");
}
else if(per >= 50)
{
printf("Grade C");
}
else if(per >= 45)
{
printf("\n Grade D");
}
else if(per >= 25)
{
printf("\n Grade E");
}
else
{
printf("\n Grade F");
}
return 0;
}
*******************************************************************
9.Write a C program to input basic salary of an employee and calculate gross salary according to given conditions.
Basic Salary <= 10000 : HRA = 20%, DA = 80%
Basic Salary is between 10001 to 20000 : HRA = 25%, DA = 90%
Basic Salary >= 20001 : HRA = 30%, DA = 95%
#include <stdio.h>
int main()
{
float basic, gross, da, hra;
/* Input basic salary of employee */
printf("Enter basic salary of an employee: ");
scanf("%f", &basic);
/* Calculate D.A and H.R.A according to specified conditions */
if(basic <= 10000)
{
da = basic * 0.8;
hra = basic * 0.2;
}
else if(basic <= 20000)
{
da = basic * 0.9;
hra = basic * 0.25;
}
else
{
da = basic * 0.95;
hra = basic * 0.3;
}
/* Calculate gross salary */
gross = basic + hra + da;
printf("GROSS SALARY OF EMPLOYEE = %.2f", gross);
return 0;
}




