Ex.
No:2
03.09.25
CONTROL STATEMENTS – BRANCHING
AIM:
To create C program that utilizes the control statements to read data from the
user and to display it on the computer.
1.Find the largest among three numbers using else if.
Program:
#include <stdio.h>
int main()
{
int a,b,c;
printf("Enter three numbers: ");
scanf("%d %d %d",&a,&b,&c);
if (a>=b&&a>=c)
{
printf("The largest number is: %d\n",a);
}
else if (b>=a&&b>=c)
{
printf("The largest number is: %d\n",b);
}
else
{
printf("The largest number is: %d\n",c);
}
return 0;
}
Input:
Enter three numbers: 5 15 25
Output:
The largest number is: 25
2.Grade Calculation.
Program:
#include <stdio.h>
int main()
{
int a;
printf("Enter marks: ");
scanf("%d",&a);
if (a>=90)
{
printf("Grade A");
}
else if (a>=75)
{
printf("Grade B");
}
else if (a>=50)
{
printf("Grade C");
}
else
{
printf("Fail");
}
return 0;
}
Input:
Enter marks: 60
Output:
Grade C
3. Check whether a character is a vowel, consonant, digit, or special
character.
Program:
#include <stdio.h>
#include <ctype.h>
int main()
{
char ch;
printf("Enter a character: ");
scanf("%c",&ch);
if (isalpha(ch))
{
if (ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')
{
printf("The character '%c' is a Vowel.\n",ch);
}
else
{
printf("The character '%c' is a Consonant.\n",ch);
}
}
else if (isdigit(ch))
{
printf("The character '%c' is a Digit.\n",ch);
}
else
{
printf("The character '%c' is a Special Character.\n", ch);
}
return 0;
}
Input:
Enter a character: a
Output:
The character 'a' is a Vowel.
4.Theatre where tickets normally cost 2 million TL each, 20% discount if one
person buys more than 15 tickets. Students get an extra 5% discount. Write C
program to check for discount and output the total amount to be paid.
Program:
#include <stdio.h>
int main() {
int a,b;
float c=2000000;
float total,discount=0;
printf("Enter number of tickets: ");
scanf("%d", &a);
printf("Are you a student? (1 = Yes, 0 = No): ");
scanf("%d", &b);
total = a*c;
if (a>15)
{
discount+=20;
}
if (b==1)
{
discount+=5;
}
total=total-(total*(discount/100));
printf("Total amount to be paid: %.2f",total);
return 0;
}
Input:
Enter number of tickets: 10
Are you a student? (1 = Yes, 0 = No): 0
Output:
Total amount to be paid: 20000000.00
5. Input a temperature in Celsius and print whether cold, comfortable, hot,
out of range.
Program:
#include <stdio.h>
int main()
{
float c;
printf("Enter temperature in Celsius: ");
scanf("%f", &c);
if (c>=-40&&c<10)
{
printf("COLD");
}
else if (c>=10&&c< 20)
{
printf("COMFORTABLE");
}
else if(c>=20&&c<50)
{
printf("HOT",c);
}
else
{
printf("OUT OF RANGE",c);
}
return 0;
}
Input:
Enter temperature in Celsius: 15
Output:
COMFORTABLE
6. Write C program which will input the age and height of a child and decide
whether he is normal, tall or short.
Program:
#include <stdio.h>
int main()
{
int a;
float h;
printf("Enter age: ");
scanf("%d", &a);
if (a<2||a>5)
{
printf("The age given is out of range.\n");
}
else
{
printf("Enter height of child: ");
scanf("%f", &h);
if (a>=2&&a<=3)
{
if(h<55)
printf("SHORT");
else if (h<75)
printf("NORMAL");
else
printf("TALL");
}
else if (a>=4&&a<=5)
{
if(h<75)
printf("SHORT");
else if(h<100)
printf("NORMAL");
else
printf("TALL");
}
}
return 0;
}
Input:
Enter age: 3
Enter height of child: 60
Output:
NORMAL
7. Input the necessary information for a person trying to borrow books.
Output a message stating whether he may or may not borrow these books.
Program:
#include <stdio.h>
int main()
{
int a,b;
printf("Enter 1 for Academic Staff, 2 for Student: ");
scanf("%d", &a);
if (a==1)
{
printf("You are Academic Staff.You can borrow as many books as you
wish,from any section.");
}
else if (a==2)
{
printf("Enter number of books you want to borrow: ");
scanf("%d",&b);
if (b<=10)
{
printf("You may borrow %d books.\n",b);
}
else
{
printf("Students can only borrow up to 10 books");
}
}
else
{
printf("Invalid choice");
}
return 0;
}
Input:
Enter 1 for Academic Staff, 2 for Student: 2
Enter number of books you want to borrow: 5
Output:
You may borrow 5 books.
8. Write a C program to input the necessary information for a person and
output his/her group number (Group1-Males under 25 years old, Group2-
Females under 25 years old, Group3- Males between 25 - 45 years old,
Group4- Females between 25 - 45 years old, Group5- All people over 45 years
old.)
Program:
#include <stdio.h>
int main() {
int g,a;
printf("Enter gender (1 for male, 2 for female): ");
scanf("%d",&g);
printf("Enter age: ");
scanf("%d",&a);
if (a<25)
{
if(g==1)
printf("Group1");
else if(g==2)
printf("Group2");
}
else if (a>=25&&a<=45)
{
if(g==1)
printf("Group3");
else if(g==2)
printf("Group4");
}
else {
printf("Group5");
}
return 0;
}
Input:
Enter gender (1 for male, 2 for female): 2
Enter age: 30
Output:
Group4
9. Write a C program to input the necessary information for a person and
output his/her group number(Group1-University graduate, speaks German,
Group2-University graduate, doesn’t speak German, Group3- High school
graduate, over 10 years experience, Group4- High school graduate, 5 - 10
years experience, Group5- High school graduate, up to 5 years experience ).
Program:
#include <stdio.h>
int main()
{
int a, b, c;
printf("Enter your education (1=University graduate,2=High school
graduate):");
scanf("%d",&a);
if (a==1)
{
printf("Do you speak German? (1=Yes,0=No): ");
scanf("%d", &b);
if (b==1)
printf("Group1");
else
printf("Group2");
}
else if (a==2)
{
printf("Enter years of experience: ");
scanf("%d", &c);
if (c>10)
printf("Group3");
else if(c>=5&&c<=10)
printf("Group4");
else
printf("Group5");
}
else
{
printf("Invalid input");
}
return 0;
}
Input:
Enter your education (1=University graduate,2=High school graduate):1
Do you speak German? (1=Yes,0=No): 1
Output:
Group1
10. In a certain insurance agency, you get a 30% discount on your yearly
premium if you had no accidents last year. Women drivers also get a 5%
extra discount on their yearly premium. Write a program to input the
normal premium and output the premium the customer must pay, making
necessary discounts if any.
Program:
#include <stdio.h>
int main() {
float a,b,discount=0;
int acc,gender;
printf("enter normal yearly premium: ");
scanf("%f", &a);
printf("did you have any accidents last year? (1=yes,0=no): ");
scanf("%d", &acc);
printf("enter gender(1=male,2=female): ");
scanf("%d", &gender);
if (acc==0)
discount+=30;
if (gender==2)
discount+=5;
b=a-(a * (discount/100));
printf("final amount: %.2f",b);
return 0;
}
Input:
enter normal yearly premium: 10000
did you have any accidents last year? (1=yes,0=no): 0
enter gender(1=male,2=female): 2
output:
final amount: 6500.00
Result:
Thus the c program were successfully implemented and verified that it utilizes
the control statements to read data from the keyboard and to display data on
the computer screen.