0% found this document useful (0 votes)
15 views26 pages

KPIT Assessment

Uploaded by

venkatarun112002
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views26 pages

KPIT Assessment

Uploaded by

venkatarun112002
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 26

KPIT Assessment

Name: Kiran Jisha J


Reg No- RA2011004010338
Dept: ECE
College: SRMIST, KTR
Mail Id: [email protected]

1. Temperature of a city in Fahrenheit degrees is input through the


keyboard. Write a program to convert this temperature into
Centigrade degrees.

#include <stdio.h>
int main()
{
float tf,tc;
printf("Enter the temperature in Farenhiet: ");
scanf("%f",&tf);
tc=((tf-32)*5)/9;
printf("The temperature in Centigrade degrees is: %.2f",tc);
return 0;
}

2. The length and breadth of a rectangle and radius of a circle are


input through the keyboard. Write a program to calculate the
area and perimeter of the rectangle, and the area and
circumference of the circle.

#include <stdio.h>
#include<math.h>
int main()
{
int ar,br,ac,arearec,perirec,areacir;
float pi=3.14,cmfcir;
printf("Enter the length of the rectangle: ");
scanf(" %d", &ar);
printf("Enter the breadth of the rectangle: ");
scanf("%d", &br);
printf("Enter the radius of the circle: ");
scanf("%d", &ac);
arearec=ar*br;
perirec=2*(ar+br);
areacir=ac*ac;
cmfcir=2*pi*ac;
printf("Area of the rectangle is %d",arearec);
printf("\nPerimeter of the rectangle is %d",perirec);
printf("\nArea of the circle is %d",areacir);
printf("\nCircumference of the circle is %.3f",cmfcir);
return 0;
}

3. If a five-digit number is input through the keyboard, write a


program to calculate the sum of its digits. (Hint: Use the
modulus operator %)

#include<stdio.h>
int main(){
int num,a,b,c,d,e,br,cr,dr;
printf("Enter a five digit number: ");
scanf("%d",&num);
a=num/10000;
b=num/1000;
br=b%10;
c=num/100;
cr=c%10;
d=num/10;
dr=d%10;
e=num%10;
printf("The sum of its digits is %d",a+br+cr+dr+e);
return 0;
}

4. Write a program to receive Cartesian co-ordinates (x, y) of a


point and convert them into polar co-ordinates (r, o). Hint: r =
sqrt(x²+y²) and tan (y/x)

#include<stdio.h>
#include<math.h>
int main(){
float x,y,r,row;
printf("Enter the Cartesian co-ordinates (x,y): ");
scanf("%f %f", &x,&y);
r=sqrtf((x*x)+(y*y));
row= atan(y/x);
printf("The polar co-ordinates are %.2f %.2f",r,row);
return 0;
}

5. Write a program to receive values of latitude (L1, L2) and


longitude (G1, G2), in degrees, of two places on the earth and
output the distance (D) between them in nautical miles. The
formula for distance in nautical miles is: D=3963 cos (sin L1 sin
L2 + cos L1 cos L2 cos (G2-G1))

#include<stdio.h>
#include<math.h>
int main(){
float l1,l2,g1,g2;
int D;
printf("Enter the values of latitude (L1,L2): ");
scanf("%f %f", &l1,&l2);
printf("Enter the values of latitude (G1,G2): ");
scanf("%f %f", &g1,&g2);
D= 3963*(acos(sin(l1)*sin(l2)+cos(l1)*cos(l2)*cos(g2-g1)));
printf("The distance between them in nautical miles is %d", D);
return 0;
}

6. Wind-chill factor is the felt air temperature on exposed skin due


to wind. The wind-chill temperature is always lower than the air
temperature, and is calculated as per the following formula:
wcf35.74+0.6215t+ (0.4275t-35.75) 36 where t is temperature and
v is wind velocity. Write a program to receive values of t and v
and calculate wind-chill factor (wcf).

#include<stdio.h>
#include<math.h>
int main(){
int wcf;
int t,v;
printf("Enter the values of t and v: ");
scanf("%d %d", &t,&v);
wcf=35.74+(0.6215*t)+((0.4275*t)-35.75)*(pow(v,0.16));
printf("The wind-chill temperature is %d",wcf);
return 0;
}

7. Two numbers are input through the keyboard into two locations
C and D. Write a program to interchange the contents of C and
D.

#include<stdio.h>
int main(){
int c,d,temp;
printf("Enter the two numbers: ");
scanf("%d %d",&c,&d);
temp=c;
c=d;
d=temp;
printf("%d %d",c,temp);
return 0;
}

8. A five-digit number is entered through the keyboard. Write a


program to obtain the reversed number and to determine
whether the original and reversed numbers are equal or not.

#include <stdio.h>
int main() {
int originalNumber, reversedNumber = 0, remainder, temp;
printf("Enter a five-digit number: ");
scanf("%d", &originalNumber);
temp = originalNumber;

while (temp != 0) {
remainder = temp % 10;
reversedNumber = reversedNumber * 10 + remainder;
temp /= 10;
}

printf("Reversed number: %d\n", reversedNumber);


if (originalNumber == reversedNumber) {
printf("The original and reversed numbers are equal.\n");
}
else {
printf("The original and reversed numbers are not equal.\n");
}
return 0;
}

9. If value of an angle is input through the keyboard, write a


program to print all its Trigonometric ratios.

#include <stdio.h>
#include <math.h>
int main() {
double angle_degrees;
double angle_radians;
double sine, cosine, tangent, cosecant, secant, cotangent;
printf("Enter the angle in degrees: ");
scanf("%lf", &angle_degrees);
angle_radians = angle_degrees * M_PI / 180.0;
sine = sin(angle_radians);
cosine = cos(angle_radians);
tangent = tan(angle_radians);
cosecant = 1.0 / sine;
secant = 1.0 / cosine;
cotangent = 1.0 / tangent;
printf("Trigonometric Ratios for %.2lf degrees:\n", angle_degrees);
printf("Sin: %.6lf\n", sine);
printf("Cos: %.6lf\n", cosine);
printf("Tan: %.6lf\n", tangent);
printf("Cosec: %.6lf\n", cosecant);
printf("Sec: %.6lf\n", secant);
printf("Cot: %.6lf\n", cotangent);
return 0;
}

10. Paper of size A0 has dimensions 1189 mm x 841 mm. Each


subsequent size A(n) is defined as A(n-1) cut in half, parallel to
its shorter sides. Thus, paper of size A1 would have dimensions
841 mm x 594 mm. Write a program to calculate and print
paper sizes A0, A1, A2, … A8.

#include <stdio.h>
int main() {
int length = 1189;
int breadth = 841;
printf("Paper sizes from A0 to A8:\n");
for (int i = 0; i <= 8; i++) {
printf("A%d: %d mm x %d mm\n", i, length, breadth);
int temp = length;
length = breadth;
breadth = temp / 2;
}
return 0;
}

11. If ages of Ram, Shyam and Ajay are input through the keyboard,
write a program to determine the youngest of the three.
# include <stdio.h>
int main( )
{
int r, s, a, young ;
printf ( "\nEnter age of Ram, Shyam and Ajay: " ) ;
scanf ( "%d %d %d", &r, &s, &a ) ;
if ( r < s )
{
if ( r < a )
young = r ;
Chapter 3: Decision Control Instruction 31
else
young = a ;
}
else
{
if ( s < a )
young = s ;
else
young = a ;
}
printf ( "The youngest of Ram(%d), Shyam(%d) and Ajay(%d) is
%d\n", r, s, a, young ) ;
return 0 ;
}

12. Write a program to check whether a triangle is valid or not, if three


angles of the triangle are entered through the keyboard. A triangle is
valid if the sum of all the three angles is equal to 180 degrees.

# include <stdio.h>
int main( )
{
float angle1, angle2, angle3 ;
printf ( "\nEnter three angles of the triangle: " ) ;
scanf ( "%f %f %f", &angle1, &angle2, &angle3 ) ;
if ( ( angle1 + angle2 + angle3 ) == 180 )
printf ( "The triangle is a valid triangle\n" ) ;
else
printf ( "The triangle is an invalid triangle\n" ) ;
return 0 ;
}

13. Write a program to find the absolute value of a number entered


through the keyboard.

# include <stdio.h>
int main( )
{
int no ;
printf ( "\nEnter any number: " ) ;
scanf ( "%d", &no ) ;
if ( no < 0 )
no = no * -1 ;
printf ( "The absolute value of given number is %d\n", no ) ;
return 0 ;
}

14. Given the length and breadth of a rectangle, write a program to


find whether the area of the rectangle is greater than its perimeter. For
example, the area of the rectangle with length = 5 and breadth = 4 is greater
than its perimeter.

# include <stdio.h>
int main( )
{
int l, b, area, peri ;
printf ( "\nEnter length and breadth of the rectangle: " ) ;
scanf ( "%d %d", &l, &b ) ;
area = l * b ;
peri = 2 * ( l + b ) ;
if ( area > peri )
printf ( "area is greater than perimeter\n" ) ;
else
printf ( "area is lesser than perimeter\n" ) ;
return 0 ;
}

15. Given three points (x1, y1), (x2, y2) and (x3, y3), write a
program to check if the three points fall on one straight line.

# include <stdio.h>
# include <math.h>
int main( )
int x1, y1, x2, y2, x3, y3 ;
int s1, s2, s3 ;
printf ( "\nEnter the values of x1 and y1 coord. of first point: " ) ;
scanf ( "%d%d", &x1, &y1 ) ;
printf ( "\nEnter the values of x2 and y2 coord. of first point: " ) ;
scanf ( "%d%d", &x2, &y2 ) ;
printf ( "\nEnter the values of x3 and y3 coord. of first point: " ) ;
scanf ( "%d%d", &x3, &y3 ) ;
s1 = abs ( x2- x1 ) / abs ( y2 - y1 ) ;
s2 = abs ( x3 - x1 ) / abs ( y3 - y1 ) ;
s3 = abs ( x3 - x2 ) / abs ( y3 - y2 ) ;
if ( ( s1 == s2 ) && ( s1 == s3 ) )
printf ( "Points are Co-linear\n" ) ;
else
printf ( "Points are NOT Co-linear\n" ) ;
return 0 ;
}

16. Given the coordinates (x, y) of center of a circle and its radius, write a
program that will determine whether a point lies inside the circle, on the
circle or outside the circle. (Hint: Use sqrt( ) and pow( ) functions)

# include <stdio.h>
int main( )
{
int x, y, r ;
int dis, d ;
printf ( "\nEnter radius of circle& coordinates of point ( x, y ): " ) ;
scanf ( "%d %d %d", &r, &x, &y ) ;
dis = x * x + y * y ; /* or use pow( ) function*/
d=r*r;
if ( dis == d )
printf ( "Point is on the circle\n" ) ;
else if ( dis > d )
printf ( "Point is outside the circle\n" ) ;
else
printf ( "Point is inside the circle\n" ) ;
}
return 0 ;
}

17. Given a point (x, y), write a program to find out if it lies on x-
axis, y-axis or origin.

# include <stdio.h>
int main( )
{
int x, y ;
printf ( "\nEnter the x and y coordinates of a point: " ) ;
scanf ( "%d%d", &x, &y ) ;
if ( x == 0 && y == 0 )
printf ( "Point lies on origin\n" ) ;
else
if ( x == 0 && y != 0 )
printf ( "Point lies on Y axis\n" ) ;
else
if ( x != 0 && y == 0 )
printf ( "Point lies on X axis\n" ) ;
else
printf ( "Point neither lies on any axis, nor origin\n" );
return 0 ;
}
18. According to Gregorian calendar, it was Monday on the date
01/01/01. If any year is input through the keyboard write a program to find
out what is the day on 1st January of this year.

# include <stdio.h>
int main( )
{

int leapdays, firstday, yr ;


long int normaldays, totaldays ;
printf ( "Enter year:" ) ;
scanf ( "%d", &yr );
normaldays = ( yr - 1 ) * 365L ;
leapdays = ( yr - 1 ) / 4 - ( yr - 1 ) / 100 + ( yr - 1 ) / 400 ;
totaldays = normaldays + leapdays ;
firstday = totaldays % 7 ;
if ( firstday == 0 )
printf ( "Monday\n" ) ;
if ( firstday == 1 )
printf ( "Tuesday\n" ) ;
if ( firstday == 2 )
printf ( "Wednesday\n" ) ;
if ( firstday == 3 )
printf ( "Thursday\n" ) ;
if ( firstday == 4 )
printf ( "Friday\n" ) ;
if ( firstday == 5 )
printf ( "Saturday\n" ) ;
if ( firstday == 6 )
printf ( "Sunday\n" ) ;
return 0 ;
}

19. If the lengths of three sides of a triangle are entered through the
keyboard, write a program to check whether the triangle is an isosceles, an
equilateral, a scalene or a right-angled triangle.

# include <stdio.h>
int main( )
{
int s1, s2, s3, a, b, c ;
printf ( "\nEnter three sides of a triangle: " ) ;
scanf ( "%d %d %d", &s1, &s2, &s3 ) ;
if ( s1 != s2 && s2 != s3 && s3 != s1 )
printf ( "Scalene triangle\n" ) ;
if ( ( s1 == s2 ) && ( s2 != s3 ) )
printf ( "Isosceles triangle\n" ) ;
if ( ( s2 == s3 ) && ( s3 != s1 ) )
printf ( "Isosceles triangle\n" ) ;
if ( ( s1 == s3 ) && ( s3 != s2 ) )
printf ( "Isosceles triangle\n" ) ;
if ( s1 == s2 && s2 == s3 )
printf ( "Equilateral triangle\n" ) ;
a = ( s1 * s1 ) == ( s2 * s2 ) + ( s3 * s3 ) ;
b = ( s2 * s2 ) == ( s1 * s1 ) + ( s3 * s3 ) ;
c = ( s3 * s3 ) == ( s1 * s1 ) + ( s2 * s2 ) ;
if ( a || b || c )
printf ( "Right-angled triangle\n" ) ;
return 0 ;
}

20. In digital world colors are specified in Red-Green-Blue (RGB)


format, with values of R, G, B varying on an integer scale from 0 to
255. In print publishing the colors are mentioned in Cyan-Magenta-
Yellow- Black (CMYK) format, with values of C, M, Y, and K
varying on a real scale from 0.0 to 1.0. Write a program that converts
RGB color to CMYK color as per the following formulae:

Note that if the RGB values are all 0, then the CMY values are all 0 and the
K value is 1.

# include <stdio.h>
int main( )
{
float red, green, blue ;
float white, cyan, magenta, yellow, black ;
float max ;
printf ( "\nEnter Red, Green, Blue values (0 to 255): " ) ;
scanf ( "%f %f %f", &red, &green, &blue ) ;
if ( red == 0 && green == 0 && blue == 0 )
{
cyan = magenta = yellow = 0 ;
black = 1 ;
}
else
{
red = red / 255 ;
green = green / 255 ;
blue = blue / 255 ;
max = red ;
if ( green > max )
max = green ;
if ( blue > max )
max = blue ;
white = max ;
cyan = ( white - red ) / white ;
magenta = ( white - green ) / white ;
yellow = ( white - blue ) / white ;
black = 1 - white ;
}
printf ( "CMYK = %f %f %f %f\n", cyan, magenta, yellow, black ) ;
return 0 ;
}

21. A certain grade of steel is graded according to the following


conditions:
(i) Hardness must be greater than 50
(ii) Carbon content must be less than 0.7
(iii) Tensile strength must be greater than 5600

The grades are as follows:


Grade is 10 if all three conditions are met
Grade is 9 if conditions (i) and (ii) are met
Grade is 8 if conditions (ii) and (iii) are met
Grade is 7 if conditions (i) and (iii) are met
Grade is 6 if only one condition is met
Grade is 5 if none of the conditions are met
Write a program, which will require the user to give values of hardness,
carbon content and tensile strength of the steel under consideration and
output the grade of the steel.

# include <stdio.h>
# include <stdlib.h>
int main( )
{
float hard, carbon, tensile ;
printf ( "\nEnter Hardness of steel: " ) ;
scanf ( "%f", &hard ) ;
printf ( "\nEnter Carbon content: " ) ;
scanf ( "%f", &carbon ) ;
printf ( "\nEnter Tensile strength:" ) ;
scanf ( "%f", &tensile ) ;
if ( hard > 50 && carbon < 0.7 && tensile > 5600 )
{
printf ( "Grade 10\n" ) ;
exit ( 0 ) ; /* Terminates the execution */
}
if ( hard > 50 && carbon < 0.7 && tensile <= 5600 )
{
printf ( "Grade 9\n" ) ;
exit ( 0 ) ;
}
if ( hard <= 50 && carbon < 0.7 && tensile > 5600 )
{
printf ( "Grade 8\n" ) ;
exit ( 0 ) ;
}
if ( hard > 50 && carbon >= 0.7 && tensile > 5600 )
{
printf ( "Grade 7\n" ) ;
exit ( 0 ) ;
}
if ( hard > 50 || carbon < 0.7 || tensile > 5600 )
{
printf ( "Grade 6\n" ) ;
exit ( 0 ) ;
}
printf ( "Grade 5\n" ) ;
return 0 ;
}

22. The Body Mass Index (BMI) is defined as ratio of the weight of a person
(in kilograms) to the square of the height (in meters). Write a program that
receives weight and height, calculates the BMI, and reports the BMI
category as per the following table:
# include <stdio.h>
# include <math.h>
int main( )
{
float wt, ht, bmi ;
printf ( "Enter weight in kg and height in meters: " ) ;
scanf ( "%f %f", &wt, &ht ) ;
bmi = wt / ( ht * ht ) ;
printf ( "Body Mass Index = %f\n", bmi ) ;
if ( bmi < 15 )
printf ( "BMI Category: Starvation\n" ) ;
else if ( bmi < 17.5 )
printf ( "BMI Category: Anorexic\n" ) ;
else if ( bmi < 18.5 )
printf ( "BMI Category: Underweight\n" ) ;
else if ( bmi < 25 )
printf ( "BMI Category: Ideal\n" ) ;
else if ( bmi < 30 )
printf ( "BMI Category: Overweight\n" ) ;
else if ( bmi < 40 )
printf ( "BMI Category: Obese\n" ) ;
else
printf ( "BMI Category: Morbidly Obese\n" ) ;
return 0 ;
}

23. Write a program to print all the ASCII values and their
equivalent characters using a while loop. The ASCII values vary from 0 to
255.

# include <stdio.h>
int main( )
{
int i = 0 ;
while ( i <= 255 )
{
printf ( "%d %c\n", i, i ) ;
i++ ;
}
return 0 ;
}

24. Write a program to print out all Armstrong numbers between 1


and 500. If sum of cubes of each digit of the number is equal to the number
itself, then the number is called an Armstrong number. For example, 153 = (
1 * 1 * 1 ) + ( 5 * 5 * 5 ) + ( 3 * 3 * 3 ).
# include <stdio.h>
int main( )
{
int i = 1, a, b, c ;
printf ( "Armstrong numbers between 1 & 500 are:\n" ) ;
while ( i <= 500 )
{
a = i % 10 ; /* extract last digit */
b = i % 100 ;
b = ( b - a ) / 10 ; /* extract second digit */
c = i / 100 ; /* extract first digit */
if ( ( a * a * a ) + ( b * b * b ) + ( c * c * c ) == i )
printf ( "%d\n", i ) ;
i++ ;
}
return 0 ;
}

25. Write a program for a matchstick game being played between the
computer and a user. Your program should ensure that the computer always
wins. Rules for the game are as follows:
− There are 21 matchsticks.
− The computer asks the player to pick 1, 2, 3, or 4 matchsticks.
− After the person picks, the computer does its picking.
− Whoever is forced to pick up the last matchstick loses the game.

# include <stdio.h>
int main( )
{
int m = 21, p, c ;
while ( 1 )
{
printf ( "\n\nNo. of matches left = %d\n", m ) ;
printf ( "Pick up 1, 2, 3 or 4 matches: " ) ;
scanf ( "%d", &p ) ;
if ( p > 4 || p < 1 )
continue ;
m=m-p;
printf ( "No. of matches left = %d\n", m ) ;
c=5-p;
printf ( "Out of which computer picked up %d\n", c ) ;
m=m-c;
if ( m == 1 )
{
printf ( "Number of matches left %d\n\n", m ) ;
printf ( "You lost the game !!\n" ) ;
break ;
}
}
return 0 ;
}

26. Write a program to enter numbers till the user wants. At the end it
should display the count of positive, negative and zeros entered.

# include <stdio.h>
int main( )
{
int pos, neg, zero, num ;
char ans = 'y' ;
pos = neg = zero = 0 ;
while ( ans == 'y' || ans == 'Y' )
{
printf ( "\nEnter a number: " ) ;
scanf ( "%d", &num ) ;
if ( num == 0 )
zero++ ;
if ( num > 0 )
pos++ ;
if ( num < 0 )
neg++ ;
fflush ( stdin ) ; // clears standard input stream
printf ( "\nDo you want to continue? " ) ;
scanf ( "%c", &ans ) ;
}
printf ( "You entered %d positive number(s)\n", pos ) ;
printf ( "You entered %d negative number(s)\n", neg ) ;
printf ( "You entered %d zero(s)\n", zero ) ;
return 0 ;
}

27. Write a program to receive an integer and find its octal


equivalent. Hint: To obtain octal equivalent of an integer, divide it
continuously by 8 till dividend doesn’t become zero, then write the
remainders obtained in reverse direction.

# include <stdio.h>
# include <math.h>
int main( )
{
int n1, n2, rem, oct, p ;
printf ( "\nEnter any number: " ) ;
scanf ( "%d", &n1 ) ;
n2 = n1 ;
p = oct = 0 ;
while ( n1 > 0 )
{
rem = n1 % 8 ;
n1 = n1 / 8 ;
oct = oct + rem * pow ( 10.0, p ) ;
p++ ;
}
printf ( "The octal equivalent of %d is %d\n", n2, oct ) ;
return 0 ;
}

28. Write a program to find the range of a set of numbers. Range is


the difference between the smallest and biggest number in the list.

# include <stdio.h>
int main( )
{
int n, no, flag, small, big, range ;
flag = 0 ;
printf ( "\nHow many numbers are there in a set? " ) ;
scanf ( "%d", &n ) ;
while ( n > 0 )
{
printf ( "\nEnter no: " ) ;
scanf ( "%d", &no ) ;
if ( flag == 0 )
{
small = big = no ;
flag = 1 ;
}
else
{
if ( no > big )
big = no ;
if ( no < small )
small = no ;
}
n-- ;
}
if ( small < 0 )
range = small + big ;
else
range = big - small ;
if ( range < 0 )
range = range * -1 ;
printf ( "\nThe range of given set of numbers is %d\n", range ) ;
return 0 ;
}
29. Write a program to print the multiplication table of the number
entered by the user. The table should get displayed in the following form:
29 * 1 = 29
29 * 2 = 58

Program:
/* Generate and print table of a given number */
# include <stdio.h>
int main( )
{
int i, num ;
printf ( "\nEnter the number: " ) ;
scanf ( "%d", &num ) ;
for ( i = 1 ; i <= 10 ; i++ )
printf ( "%d * %d = %d\n", num, i, num * i ) ;
return 0 ;
}

30. According to a study, the approximate level of intelligence of a


person can be calculated using the following formula:
i = 2 + ( y + 0.5 x )
Write a program that will produce a table of values of i, y and x, where y
varies from 1 to 6, and, for each value of y, x varies from 5.5 to 12.5 in steps
of 0.5.

# include <stdio.h>
int main( )
{
int y ;
float i, x ;
for ( y = 1 ; y <= 6 ; y++ )
{
for ( x = 5.5 ; x <= 12.5 ; x += 0.5 )
{
i = 2 + ( y + 0.5 * x ) ;
printf ( "y = %d, x = %f i = %f\n", y, x, i ) ;
}
}
return 0 ;
}

31. When interest compounds q times per year at an annual rate of r % for n
years, the principal p compounds to an amount a as per the following
formula

a = p ( 1 + r / q ) nq
Write a program to read 10 sets of p, r, n & q and calculate the
corresponding as.

# include <stdio.h>
# include <math.h>

int main( )

{
float q, r, n, p, a ;
int i ;
for ( i = 1 ; i <= 10 ; i++ )
{
printf ( "\nEnter the principal amount:" ) ;
scanf ( "%f", &p ) ;
printf ( "\nEnter the rate of interest:" ) ;
scanf ( "%f", &r ) ;
printf ( "\nEnter the number of years: " ) ;
scanf ( "%f", &n ) ;
printf ( "\nEnter the compounding period: " ) ;
scanf ( "%f", &q ) ;
a = p + pow ( (1 + ( r / q ) ), ( n * q ) ) ;
printf ( "\n\nTotal amount = %f\n", a ) ;
}
return 0 ;
}

32. The natural logarithm can be approximated by the following


series.
If x is input through the keyboard, write a program to calculate the
sum of first seven terms of this series.
Program:
/* Compute natural logarithm */
# include <stdio.h>
# include <math.h>
int main( )
{
int x, i ;
float result = 0 ;
printf ( "\nEnter the value of x: " ) ;
scanf ( "%d", &x ) ;
for ( i = 1; i <= 7 ; i++ )
{
if ( i == 1 )
result = result + pow ( ( x - 1.0 ) / x, i ) ;
else
result = result + ( 1.0 / 2 ) * pow ( ( x - 1.0 ) / x, i ) ;
}
printf ( "\nLog ( %d ) = %f\n", x, result ) ;
return 0 ;
}

33. Write a program to generate all Pythagorean Triplets with side


length less than or equal to 30.

#include <stdio.h>
int main( )
{
int i, j, k ;
for ( i = 1 ; i <= 30 ; i++ )
{
for ( j = 1 ; j <= 30 ; j++ )
{
for ( k = 1 ; k <= 30 ; k++ )
{
if ( i * i + j * j == k * k )
printf ( "%d %d %d\n", i, j, k ) ;
}
}
}
return 0 ;
}

34. Population of a town today is 100000. The population has


increased steadily at the rate of 10% per year for last 10 years. Write a
program to determine the population at the end of each year in the last
decade.

#include <stdio.h>
#include <math.h>
int main( )
{
int pop, n ;
float p, r ;
r = 10 ;
p = 100000 ;
for ( n = 1 ; n <= 10 ; n++ )
{
pop = p / pow ( ( 1 + r / 100 ), n ) ;
printf ( "Population %d years ago = %d\n", n, pop ) ;
}
return 0 ;
}
35. Ramanujan number (1729) is the smallest number that can be
expressed as sum of two cubes in two different ways—1729 can be
expressed as 13 + 123 and 93 + 103. Write a program to print all such
numbers up to a reasonable limit.

#include <stdio.h>
int main( )
{
int i, j, k, l ;
for ( i = 1 ; i <= 30 ; i++ )
{
for ( j = 1 ; j <= 30 ; j++ )
{
for ( k = 1 ; k <= 30 ; k++ )
{
for ( l = 1 ; l <= 30 ; l++ )
{
if ( ( i != j && i != k && i != l ) &&
( j != i && j != k && j != l ) &&
( k != i && k != j && k != l ) &&
( l != i && l != j && l != k ) )
{
if ( i * i * i + j * j * j == k * k * k + l * l * l )
printf ( "%d %d %d %d\n", i, j, k, l ) ;
}
}
}
}
}
return 0 ;
}

36. Write a program to print 24 hours of day with suitable suffixes


like AM, PM, Noon and Midnight.

#include <stdio.h>
int main( )
{
int hour ;
for ( hour = 0 ; hour <= 23 ; hour++ )
{
if ( hour == 0 )
{
printf ( "12 Midnight\n" ) ;
continue ;
}
if ( hour < 12 )
printf ( "%d AM\n", hour ) ;
if ( hour == 12 )
printf ( "12 Noon\n" ) ;
if ( hour > 12 )
printf ( "%d PM\n", hour % 12 ) ;
}
return 0 ;
}

37. Write a program to produce the following output:

# include <stdio.h>
int main( )
{
int i , j, k, l, sp ;
sp = 20 ;
for ( i = 1, k = 1 ; i < 5 ; i++ )
{
for ( l = 1 ; l <= sp ; l++ )
printf ( " " ) ;
sp -= 2 ;
for ( j = 1 ; j <= i ; j++, k++ )
printf ( " %d ", k ) ;
printf ( "\n" ) ;
}
return 0 ;
}

38. Write a program which to find the grace marks for a student
using switch. The user should enter the class obtained by the student and the
number of subjects he has failed in. Use the following logic:

− If the student gets first class and he fails in more than 3 subjects, he does
not get any grace. Otherwise, he gets a grace of 5 marks per subject.

− If the student gets second class and he fails in more than 2 subjects, he
does not get any grace. Otherwise, he gets a grace of 4 marks per subject.

− If the student gets third class and he fails in more than 1 subject, then he
does not get any grace. Otherwise, he gets a grace of 5 marks.

# include <stdio.h>
int main( )
{
int c, sub ;
printf ( "\nEnter the class and number of subjects failed: " ) ;
scanf ( "%d %d", &c, &sub ) ;
switch ( c )
{
case 1 :
if ( sub <= 3 )
printf ( "Student gets total of %d grace marks\n", 5 * sub ) ;
else
printf ( "No grace marks\n" ) ;
break ;
case 2 :
if ( sub <= 2 )
printf ( "Student gets total of %d grace marks\n", 4 * sub ) ;
else
printf ( "No grace marks\n" ) ;
break ;
case 3 :
if ( sub == 1 )
printf ( "Student gets 5 grace marks\n" ) ;
else
printf ( "No grace marks\n" ) ;
break ;
default :
printf ( "Wrong class entered\n" ) ;
}
return 0 ;
}

You might also like