KPIT Assessment
KPIT Assessment
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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;
}
#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 ;
}
# 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 ;
}
# 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 ;
}
# 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( )
{
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 ;
}
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 ;
}
# 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 ;
}
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 ;
}
# 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 ;
}
# 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 ;
}
# 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 ;
}
#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 ;
}
#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 ;
}
#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 ;
}
# 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 ;
}