Programming in ANSI C
Chapter-05
Programming Exercises
5.1: Write a to determine whether a given number is “odd” or “even” and print the message NUMBER IS EVEN or, NUMBER IS ODD.
(a) without using else option, (b) with else option.
| (a) Solution: #include <stdio.h> int main() { int num; printf(“Enter the number=”); scanf(“%d”,&num); { if(num%2==0) printf(“NUMBER IS EVEN”); } { if(num%2!=0) printf(“NUMBER IS ODD”); } return 0; } | (b) Sollution: #include <stdio.h> int main() { int num; printf(“Enter the number=”); scanf(“%d”,&num); if(num%2==0) printf(“NUMBER IS EVEN”); else printf(“NUMBER IS ODD”); return 0; } |
5.2: Write a program to find the number of and sum of all integers greater than 100 and less than 200 that are divisible by 7.
Solution:
#include <stdio.h>
int main()
{
int num,sum=0;
for(num=100; num<200; num++)
{
if(num%7==0)
sum=sum+num;
}
printf(“%d”,sum);
return 0;
}
5.3: A set of two linear equation two unknows x1 and x2 is given below:
ax1+bx2=m, cx1+dx2=n
the set has a unique solution
x1=(md=bn)/(ad-cb)
x2=(na-mc)/(ad-cb)
provided the determinate ad-cb is not equal to zero.
Write a program that will read the values of constants a,b,c,d,m and n and compute the values of x1 and x2 .An appropriate message should be printed if ad-cb=0.
solution:
#include <stdio.h>
int main()
{
float a,b,c,d,m,n,x1,x2;
printf(“Enter the value of a,b,c,d,m,n=”);
scanf(“%f %f %f %f %f %f”,&a,&b,&c,&d,&m,&n);
x1=(m*d-b*n)/(a*d-c*b);
x2=(n*a-m*c)/(a*d-c*b);
if((a*d-c*b)!=0)
printf(“x1= %f \nx2= %f”,x1,x2);
else
printf(“The value is infinity.”);
return 0;
}
5.4: Given a list of marks ranging from 0 to 100, write a program to print number of students:
(a)Who have obtained more than 80 marks,
(b) who have obtained more than 60 marks,
(c)Who have obtained more than 40 marks,
(d) who have obtained 40 or less marks,
(e)In the range 81 to 100,
(f) in the range 61 to 80,
(g)in the range 41 to 60,
(h) in the range 0 to 40.
The program should use a minimum numbers of if statements.
Solution:
#include <stdio.h>
int main()
{
int marks,student,a,b,c,d,i;
a=0; b=0; c=0; d=0;
printf(“Enter the number of student=\n”);
scanf(“%d”,&student);
for(i=1; i<=student; i++)
{
printf(“Input marks=”);
scanf(“%d”,&marks);
if(marks>80)
a++;
else if(marks<60)
b++;
else if(marks<40)
c++;
else if(marks<=40)
d++;
}
printf(“Number of students who have obtained more than 80 marks=%d\nNumber of students who have obtained more than 60 marks=%d\n Number of students who have obtained more than 40 marks=%d\n Number of students who have obtained 40 or less marks=%d”,a,b,c,d);
return 0;
}
5.5 Admission to a professional course is subjects to the following conditions:
(a) Marks in Mathematics>=60
(b) Marks in Physics>=50
(c) Marks in Chemistry>=40
(d) Total in all three subjects>=200 or
Total in Mathematics and Physics>=150
Given the marks in the three subjects, write a program to process the applications to list the eligible candidates.
Solution:
#include <stdio.h>
int main()
{
int a,b,m,p,c;
printf(“enter the numbers of Math,Physics,Chemistry=”);
scanf(“%d %d %d”,&m,&p,&c);
a=m+p+c;
b=m+p;
if(m>=60 && p>=50 && c>=40 && (a>=200 || b>=150))
printf(“The candidate is eligible”);
else
printf(“The candidate is not eligible”);
return 0;
}
5.7: (a) Shown below is a Floyd’s triangle .
1
2 3
4 5 6
7 8 9 10
11………..15
79…….. .. .. .. .. ..91
Solution:
#include <stdio.h>
int main()
{
int row,col,n,v=1;
printf(“How many rows is triangle=”);
scanf(“%d”,&n);
for(row=1; row<=n; row++)
{
for(col=1; col<=row; col++)
printf(“%d “,v++);
printf(“\n”);
}
return 0;
}
5.7: (b) Modify the program the following from of Floyd’s triangle.
1
0 1
1 0 1
0 1 0 1
1 0 1 0 1
Solution:
#include <stdio.h>
int main()
{
int row,col,n;
printf(“How many rows is triangle=”);
scanf(“%d”,&n);
for(row=1; row<=n; row++)
{
for(col=1; col<=row; col++)
printf(“%d “,(row+col+1)%2);
printf(“\n”);
}
return 0;
}
5.9 Write a program that will read the value of x and evaluate the following function
using
(a) nested if statements.
(b) else if statements and
(c) conditional operator ?
Solution:(a)
#include <stdio.h>
main()
{
float x,y;
printf(“Input x\n”);
scanf(“%f”,&x);
if(x!=0)
{
if(x<0)
printf(“y=1\n”);
if(x<0)
printf(“y=-1”);
}
if(x==0)
printf(“y=0”);
return 0;
}
(b) #include <stdio.h>
main()
{
float x,y;
printf(“Input x\n”);
scanf(“%f”,&x);
if(x<0)
printf(“y=1\ny=-1”);
else if(x==0)
printf(“y=0”);
else
printf(“There is no value of y”);
return 0;
}
(c) #include <stdio.h>
main()
{
float x,y;
printf(“Input x\n”);
scanf(“%f”,&x);
(x<0)?printf(“y=1\n y=-1”):((x==0)?printf(“y=0”):printf(“No value”));
return 0;
}
5.10: Write a program to compute the real roots of a quadratic equation : ax2+bx2+c=0
The roots are given by the equtions:
X1=
x2=
The program should request for the values of the constants a,b and c print the values of x1 and x2.Use the following rules:
(a) No solution, if both a and b are zero
(b) There is only one root if a=0(x=-c/b)
(c) There are no real roots, if b2-4ac is negative
(d) Otherwise, there no real roots
Test your program with appropriate data so that all logical paths are working as per your design. Incorporate appropriate output messages.
Solution: #include <stdio.h>
main()
{
float a,b,c,x,x1,x2,m;
printf(“Input a,b,c=”);
scanf(“%f %f %f”,&a,&b,&c);
m=b*b-4*a*c;
x=-(c/b);
x1= -b+m/(2*a);
x2= -b-m/(2*a);
if(a==0&&b==0)
printf(“No solution”);
else if(a==0)
{
printf(“x=%f”,x);
}
else if(m<0)
printf(“Roots are imaginary\n”);
else
printf(“x1=%f \nx2=%f”,x1,x2);
return 0;
}
5.11: Write a program to read three integer values from the keyboard and displays the output stating that they are the sides of right-angled triangle.
Solution: #include <stdio.h>
main()
{
int a,b,c,x,y,z;
printf(“Input three integer values a b and c\n”);
scanf(“%d%d%d”,&a,&b,&c);
x=a*a;
y=b*b;
z=c*c;
if(a>b && a>c &&(x==y+z))
printf(“The values are sides of right-angled triangle”);
else if(b>a && b>c &&(y==x+z))
printf(“The values are sides of right-angled triangle”);
else if(c>a && c>b && z==x+y)
printf(“The values are sides of right-angled triangle”);
else
printf(“The values are not sides of right-angled triangle”);
return 0;
}
5.12: An electricity board charges the following rates for the use of electricity:
For the first 200 units: 80 per unit
For the next 100 units: 90per unit
Beyond 300 units: Rs.1.00 per unit
All users are charged a minimum of Rs. 100 as meter charge. If the total amount is more than Rs.400, then an additional surcharge of 15% of total amount is charged. Write a program to read the names of users andnumber of units consumed and print out the charges with names.
Solution: #include <stdio.h>
main()
{
float unit, net, total;
char name;
printf(“Input users name & unit\n”);
scanf(“%s %f”,&name,&unit);
{
if(unit<=200)
total=100+(0.8*unit);
else if(unit<=300)
total=100+(0.9*unit);
else if(unit>300)
total=100+(1*unit);
{
if(total>400)
{
net=total+total*(0.15);
printf(“Total=%f”,total);
}
else
printf(“Total=%f”,total);
}}
return 0;
}
5.13: Write a program to compute and display the sum of all integers that are divisible by 6 but not divisible by 4 and lie between 0 to 100. The program should also count and display the number of such values.
Solution: #include <stdio.h>
main()
{
int i,count;
count=0;
for(i=0;i<=100;i++)
{
if(i%6==0&&i%4!=0)
{
count=count+1;
printf(” %d”,i);
}
}
printf(“\n”);
printf(“count=%d”,count);
return 0;
}
5.14 Write an interactive program that could read a positive integer number and decide whether the number is a prime number display the output accordingly. Modify the program to count all prime numbers that lie 100 to 200.
[Note: A prime number is positive integer that is divisible only by 1 or by itself]
Solution: #include <stdio.h>
main()
{
int i,n,c=0;
printf(“enter the number=”);
scanf(“%d”,&n);
for(i=1; i<=n; i++)
{
if(n%i==0)
{
c++;
}
}
if(c==2)
printf(“Prime”);
else
printf(“Non-prime”);
return 0;
}