Exercise 5.1(a) Write a program to determine whether a given
number is odd or even and print the message NUMBER
IS EVEN or NUMBER IS ODD without using else option
#include<stdio.h>
void main()
{
int a;
printf("Enter the number: \n");
scanf("%d", &a);
switch((a%2))
{
case 1:
{
printf("Odd Number.");
break;
}
default:
{
printf("Even Number.");
break;
}
}
}
Exercise 5.1(b) Write a program to determine whether a given number is odd or
even and print the message NUMBER IS EVEN or
NUMBER IS ODD using else option
#include<stdio.h>
void main()
{
int a;
printf("Enter the number: \n");
scanf("%d", &a);
if(a%2==0)
{
printf("Number is even.\n");
}
else
{
printf("number is odd.");
}
}
Exercise 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.
#include<stdio.h>
void main()
{
int n,sum=0;
printf("Numbers 100 to 200 That are
divided by 7 :\n");
for(n=105;n<=200;n+=7)
{
printf("%d\n",n);
sum+=n;
}
printf("The summation of all numbers
are = %d",sum);
}
Exercise 5.3 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.
#include<stdio.h>
void main()
{
float a,b,c,d,m,n,invld,x1,x2;
printf("Enter a,b,c,d,m,n separated by
Comma : ");
scanf("%f,%f,%f,%f,%f,%f",&a,&b,&c,&d,&m,&n);
invld=((a*d)-(c*b));
if(invld==0)
{
printf("Invalid Input!!!");
}
else
{
x1=(((m*d)-(n*b))/invld);
x2=(((n*a)-(m*c))/invld);
printf("X1=%f\nX2=%f",x1,x2);
}
}
Exercise 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,
#include<stdio.h>
int main()
{
int m,c1=0,c2=0,c3=0,c4=0,n,i;
printf("How many number you want to
check: ");
scanf("%d",&n);
printf("Enter Number :");
for(i=1;i<=n;i++)
{
scanf("%d",&m);
if(m<101&&m>80)
{
c1++;
}
else if(m>60)
{
c2++;
}
else if(m>40)
{
c3++;
}
else if(m>=0)
{
c4++;
}
else
{
printf("Invalid
input!!!\n\n");
}
}
printf("from 100-81=%d\nfrom
80-61=%d\nfrom 60-41=%d\nfrom 40-0=%d\n\n\n",c1,c2,c3,c4);
}
Exercise 5.5 write a program to process the applications to list the
eligible candidates. (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
#include<stdio.h>
void main()
{
float m,p,c,t,mp;
printf("Enter marks :");
scanf("%f %f %f",
&m,&p,&c);
t=m+p+c;
mp=m+p;
if(m>=60 && p>=50 &&
c>=40 && (t>=200 || mp>=150))
{
printf("Congratulations You Got
Admission");
}
else
{
printf("Sorry you Are Not
Eligible");
}
}
Exercise 5.6
#include<stdio.h>
#include<math.h>
void main()
{
float a,s;
printf("Enter your number : ");
scanf("%f", &a);
s=sqrt(a);
printf("Number\t0.0\t0.1\t0.2\t0.3\t0.4\t0.5\t0.6\t0.7\t0.8\t0.9\n\
____________________________________________________________________________________\
\n0.0\n1.0\n2.0\n3.0\n9.0\n\
____________________________________________________________________________________");
}
Exercise 5.7(a) Shown below is a
Floyd’s triangle .
1
2
3
4
5 6
7
8 9 10
11………..15
79........
.. .. .. .. ..91
#include<stdio.h>
void main()
{
int a=1,n=13,c,i;
for(i=1;i<=n;i++)
{
for(c=1;c<=i;c++)
{
printf("%3d",a);
a++;
}
printf("\n");
}
}
Exercise 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
#include<stdio.h>
void main()
{
int a=1,n=13,c,i;
for(i=1;i<=n;i++)
{
for(c=1;c<=i;c++)
{
printf("%3d",(i+c+1)%2);
}
printf("\n");
}
}
Exercise 5.8 Write
a program using switch and if statements to compute the net amount to be paid
by a coustomer.
#include<stdio.h>
void main()
{
float a;
char ch;
printf("Enter amount: ");
scanf("%f",&a);
printf("Enter item name for mill 'm'
for Handloom 'h': ");
scanf("%s",&ch);
if(a>0&&a<=100)
{
switch(ch)
{
case 'm':
case 'M':
{
printf("Amount =
%.2f",a);
break;
}
case 'h':
case 'H':
{
printf("Amount =
%.2f",(a-=(a*0.05)));
break;
}
}
}
else if(a>=101&&a<=200)
{
switch(ch)
{
case 'm':
case 'M':
{
printf("Amount =
%.2f",(a-=(a*0.05)));
break;
}
case 'h':
case 'H':
{
printf("Amount =
%.2f",(a-=(a*0.075)));
break;
}
}
}
else if(a>=201&&a<=300)
{
switch(ch)
{
case 'm':
case 'M':
{
printf("Amount = %.2f",(a-=(a*0.075)));
break;
}
case 'h':
case 'H':
{
printf("Amount =
%.2f",(a-=(a*0.1)));
break;
}
}
}
else if(a>300)
{
switch(ch)
{
case 'm':
case 'M':
{
printf("Amount =
%.2f",(a-=(a*0.1)));
break;
}
case 'h':
case 'H':
{
printf("Amount = %.2f",(a-=(a*0.15)));
break;
}
}
}
}
Exercise 5.9
Exercise 5.10 The program should request for the values of
the constants a,b and c print the values of x1
and x2.Use the
following:
(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.
#include<stdio.h>
#include<math.h>
void main()
{
float a,b,c,in;
printf("Enter A,B,C separated by
comma: ");
scanf("%f,%f,%f",&a,&b,&c);
if(a==0&&b==0)
{
printf("No solution");
}
else if(a==0)
{
printf("X = %.2f",(-c/b));
}
else if(((b*b)-(4*a*c))<0)
{
printf("There is no real
root");
}
else
{
in=sqrt(((b*b)-(4*a*c))/(2*a));
printf("X1 =
%.2f\n",(-b+in));
printf("X2 = %.2f",(-b-in));
}
}
Exercise 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.
#include<stdio.h>
void main()
{
int a,b,c,x,y,z;
printf("Input 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");
}
}
Exercise 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 and number of units consumed and
print out the charges with names.
#include<stdio.h>
void main()
{
int a;
float in,to=0;
char ch[100];
printf("Enter name: ");
gets(ch);
printf("Enter units: ");
scanf("%d",&a);
if(a>=0&&a<=200)
{
printf("\nName: %s\nTotal bill:
%.2f",ch,(100+((.80)*a)));
}
else if(a<=300)
{
printf("\nName: %s\nTotal bill:
%.2f",ch,(100+((.90)*a)));
}
else if(a>300)
{
to=100+a;
if(to>400)
{
printf("\nName: %s\nTotal
bill: %.2f",ch,(to+=(to*.15)));
}
else
{
printf("\nName: %s\nTotal
bill: %.2f",ch,to);
}
}
}
Exercise 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.
#include<stdio.h>
void main()
{
int a,c=0,n;
printf("Numbers Divided by 6 but not
divided by 4 : \n");
for(a=6;a<101;a+=6)
{
if((a%4)==0)
{
}
else
{
printf("%d,\t",a);
c++;
}
}
printf("\nTotal numbers = %d",c);
}
Exercise 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]
Exercise 5.15(a) Write a program read a double-type value x
that represents angle in radians and a character-type variable t that
represents the type of trigonometric function and display the value of
(a) sin(x), if s or S
is a assigned to T,
(b) cos(x), if c or C
is assigned to T, and
(c) tan(x), if t or T
is assigned to T
Using if…else statement
#include<stdio.h>
#include<math.h>
void main()
{
double x,i;
char ch;
printf("Enter value for X: ");
scanf("%lf",&i);
printf("To check\nsin(x) type 's'\ncos
(x) type 'c'\ntan (x) type 't'\nwating for input: ");
scanf("%s",&ch);
x=(i*(180/3.1416));
printf("value in angle is :
%lf",x);
if(ch=='s'||ch=='S')
{
printf("\nSin (x) =
%lf",sin(x));
}
else if(ch=='c'||ch=='C')
{
printf("\nCos (x) =
%lf",cos(x));
}
else if(ch=='t'||ch=='T')
{
printf("\nTan (x) =
%lf",tan(x));
}
}
Exercise 5.15 (b) Write a program read a double-type value x that represents angle
in radians and a character-type variable t that represents the type of
trigonometric function and display the value of
(a) sin(x), if s or S
is a assigned to T,
(b) cos(x), if c or C
is assigned to T, and
(c) tan(x), if t or T
is assigned to T
Using
switch statement
#include<stdio.h>
#include<math.h>
void main()
{
double x,i;
char ch;
printf("Enter value for X: ");
scanf("%lf",&i);
printf("To check\nsin(x) type 's'\ncos
(x) type 'c'\ntan (x) type 't'\nwating for input: ");
scanf("%s",&ch);
x=(i*(180/3.1416));
printf("value in angle is :
%lf",x);
switch(ch)
{
case 's':
case 'S':
{
printf("\nSin (x) =
%lf",sin(x));
break;
}
case 'c':
case 'C':
{
printf("\nCos (x) =
%lf",cos(x));
break;
}
case 't':
case 'T':
{
printf("\nTan (x) =
%lf",tan(x));
break;
}
}
}
No comments:
Post a Comment