5.12 – programs to practice – decision flow statements

by on September 23, 2017

1) Enter the ages of smith, john, and black. Print the youngest and elder persons.

```#include<stdio.h>
int main()
{
int sage,jage,bage;
printf("Enter the Smith age:");
scanf("%d",&sage);
printf("Enter the John age:");
scanf("%d",&jage);
printf("Enter the Black age:");
scanf("%d",&bage);
if(sage==jage&&jage==bage)
printf("All are with the same age");
else
{
(sage>jage&&sage>bage)?printf("\nSmith is elder"):((jage>bage)?printf("\nJohn is elder"):printf("\nBlack is elder"));
(sage<jage&&sage<bage)?printf("\nSmith is younger"):((jage>bage)?printf("\nJohn is younger"):printf("\nBlack is younger"));
}
return 0;
}
```

Execution:
Enter the Smith age:25
Enter the John age:45
Enter the Black age:20
John is elder
Black  is younger

2) Accept the gender, age, percentage of marks of a person and print whether the person is selected or not. Here selections criteria are a first class male with the age group between 25 and 34, if not selected then print the details.
```#include<stdio.h>
void main()
{
int age;
float percent;
char gender;
printf("Enter the gender m/f :");
scanf("%c",&gender);
printf("Age:");
scanf("%d",&age);
printf("Enter the percentage in qualifying exam:");
scanf("%f",&percent);
if(gender=='m' && percent>=60 && age>=25 && age<=34)
printf("Selected");
else
{
if(gender=='f')
printf("Required male not a female.");
if(age<25)
printf("\nUnder age");
if(age>34)
printf("\nOver age");
if(percent<60)
printf("\nNot enough percentage");
}
return 0;
}
```

Execution 1:
Enter the gender m/f: m
Age: 30
Enter the percentage in qualifying exam: 75
Selected

Execution 2:
Enter the gender m/f: f
Age: 30
Enter the percentage in qualifying exam: 50
Required male not a female
Not enough percentage

3) If coordinates of three points are input then print whether they are in straight line or not.

Logic:

```#include<stdio.h>
#include<math.h>
int main()
{
int x1,x2,x3,y1,y2,y3;
float d1,d2,d3;
printf("Enter the first point:\n");
scanf("%d%d",&x1,&y1);
printf("Enter the second point:\n");
scanf("%d%d",&x2,&y2);
printf("Enter the third point:\n");
scanf("%d%d",&x3,&y3);
d1=sqrt(pow(x2-x1,2)+pow(y2-y1,2)); /* distance among (x1,y1) and (x2,y2) */
d2=sqrt(pow(x3-x2,2)+pow(y3-y2,2)); /* distance among (x2,y2) and (x3,y3) */
d3=sqrt(pow(x3-x1,2)+pow(y3-y1,2)); /* distance among (x1,y1) and (x3,y3) */
if(d1+d2==d3)
printf("In stright line");
else
printf("Not in stright line");
return 0;
}
```

Execution:
Enter the first point:
10      10
Enter the second point:
20      20
Enter the third point:
30      30

In straight line

4) Accept the center of a circle and radios; write a program to determine whether a given point is existed either inside or outside the circle.

Logic:

```#include<stdio.h>
#include<math.h>
int main()
{
int x1,y1,x2,y2;
float r,d;
printf("Enter the center of circle:\n");
scanf("%d%d",&x1,&y1);
scanf("%f",&r);
printf("Enter the coordinates of point:\n");
scanf("%d%d",&x2,&y2);
d=sqrt(pow(x2-x1,2)+pow(y2-y1,2)); /* distance among (x1,y1) and (x2,y2) */
if(d==r)
printf("on the circle");
else if(d>r)
printf("Out side the circle");
else
printf("Within the circle");
return 0;
}
```

Execution:
Enter the center of circle:
10      10
25
Enter the coordinates of point:
20      20
Within the circle

5) Accept the coordinates of a point and print whether it is on x-axis, y-axis or on origin (0,0)

```#include<stdio.h>
int main()
{
int x,y;
printf("Enter the coordinates of a point:\n");
scanf("%d%d",&x,&y);
if(x==0&&y==0)
printf("On origin");
else if(x==0&&y!=0)
printf("On Y-axis");
else if(x!=0&&y==0)
printf("On X-axis");
else
printf("Some where else");
return 0;
}
```

Execution 1:
Enter the coordinates of a point:
0       20
On Y-axis

Execution 2:
Enter the coordinates of a point:
20      0
On X-axis

Execution 3:
Enter the coordinates of a point:
0       0
On origin

6) Library charges fine on returning books late. For first 5 days late charge 0.50 per day, for 6-10 days fine one rupee, for more than 10 days charge 5 rupees and cancel the membership if exceeds to 30 days.

```#include<stdio.h>
int main()
{
int d;
printf("Enter the number of days late:");
scanf("%d",&d);
if(d>30)
printf("Cancel the membership");
else if(d>10)
printf("Fine %d",d*5);
else if(d>=6)
printf("Fine %d",d*1);
else
printf("Fine %.2f",d*0.50);
return 0;
}
```

Execution 1:
Enter the number of days late:35
Cancel the membership

Execution 2:
Enter the number of days late:25
Fine 125

Execution 3:
Enter the number of days late:3
Fine 1.50

7) Accept three side of a triangle and print whether valid or not. Print valid if sum of two sides is greater than the largest side.

```#include<stdio.h>
int main()
{
int a,b,c;
printf("Enter three sides of a triangle:\n");
scanf("%d%d%d",&a,&b,&c);
if(a==b&&b==c)
printf("Invalid triangle");
else if(a>b&&a>c&&a<a+c)
printf("Valid triangle");
else if(b>c&&b<a+c)
printf("Valid triangle");
else if(c<a+b)
printf("Valid triangle");
else
printf("Invalid triangle");
return 0;
}
```

Execution 1:
Enter three sides of a triangle:
20      15      50
Invalid triangle

Execution 2:
Enter three sides of a triangle:
42      20      30
Valid triangle

8) Accept three sides of a triangle and print whether triangle is isosceles, equilateral, scalene or right angle triangle.

Logic:

• A triangle is called isosceles triangle if any two sides of triangle are equal.
• A triangle is called equilateral triangle if all the sides are equal.
• A triangle is called scalene triangle if all the sides are of different sizes.
• A triangle is called right angle triangle if it satisfies Pythagorean Theorem a2=b2+c2 where a is hypotenuse and b, c are other two legs.
```#include<stdio.h>
#include<math.h>
int main()
{
int a,b,c;
printf("Enter three sides of triangle:\n");
scanf("%d%d%d",&a,&b,&c);
if(a==b||b==c||a==c)
printf("\nisosceles triangle");
if(a==b&&b==c)
printf("\nequilateral triangle");
if(a!=b&&b!=c&&a!=c)
printf("\nscalene triangle");
if(pow(a,2)==pow(b,2)+pow(c,2)|| pow(b,2)==pow(a,2)+pow(c,2)|| pow(c,2)==pow(a,2)+pow(b,2))
printf("\nright angle triangle");
return 0;
}
```

Execution 1:
Enter three sides of triangle:
20      50      20
isosceles triangle

Execution 2:
Enter three sides of triangle:
50      50      50
isosceles triangle
equilateral triangle

Execution 3:
Enter three sides of triangle:
3       4       5
scalene triangle
right angle triangle

9) Accept the type of electrical connection, present month reading, previous month reading and print the number of units and bill according to the following table.

 Type of connection Units Charge/unit customer charges Domestic More than 500 6.25 45 301-500 6.00 45 201-300 4.75 40 101-200 3.05 35 51-100 2.80 30 0-50 1.45 25 Commercial More than 100 6.50 40 51-100 6.20 35 0-50 3.85 30 Temples More than 200 4.00 40 0-200 2.00 40
```#include<stdio.h>
int main()
{
char type;
int cmr,pmr,nu,cch;
float bill;
printf("Enter the type of connection d/c/t:");
scanf("%c",type);
printf("Enter the current month meter reading:");
scanf("%d",&cmr);
printf("Enter the previous month meter reading:");
scanf("%d",&pmr);
nu=cmr-pmr;
if(type==’d’)
{
if(nu>=500)
{
bill=nu*6.25;
cch=45;
}
else if(nu>=301)
{
bill=nu*6.00;
cch=45;
}
else if(nu>=201)
{
bill=nu*4.75;
cch=40;
}
else if(nu>=101)
{
bill=nu*3.05;
cch=35;
}
else if(nu>=51)
{
bill=nu*2.80;
cch=30;
}
else
{
bill=nu*1.45;
cch=25;
}
}
else if(type==’c’)
{
if(nu>100)
{
bill=nu*6.50;
cch=40;
}
else if(nu>=51)
{
bill=nu*6.20;
cch=35;
}
else
{
bill=nu*3.85;
cch=30;
}
}
else if(type==’t’)
{
if(nu>200)
{
bill=nu*4.00;
cch=40;
}
else
{
bill=nu*2.00;
cch=40;
}
}
printf("No.of units consumed %d",nu);
printf("\nbill %f",bill);
printf("\nCustomer charges %d",cch);
printf("\nTotal bill %f",bill+cch);
return 0;
}
```

Execution 1:
Enter the type of connection d/c/t: d
Enter the current month meter reading: 350
Enter the previous month meter reading: 210
No.of units consumed: 140
bill 427.000000
Customer charges: 35
Total bill 462.000000

Previous post: