Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Let Us C Solutions

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 45
At a glance
Powered by AI
The document discusses different C programs to calculate salary, convert units, find marks percentage etc.

Gross salary is calculated as basic salary + DA (40% of basic salary) + HRA (20% of basic salary)

Distance in km is first converted to meters by multiplying with 1000. Then meters is converted to feet by multiplying with 3, inches by multiplying with 12 and cm by multiplying with 100.

1.Rameshs basic salary is input through the keyboard.

His dearness allowance is


40% of basic salary, and house rent allowance is 20% of basic salary. Write a
program to calculate his gross salary.


#include<stdio.h>
#include<conio.h>
void main(void)
{
int bas_sal;
float med_al, h_rent, gross_sal;
clrscr();
printf(Please enter Ramesh's basic Salary);
scanf(%d, &bas_sal);
med_al = 40*bas_sal/100;
h_rent = 20*bas_sal/100;
gross_sal= bas_sal + med_al + h_rent;
printf(Ramesh's Gross Salary=%f, gross_sal);
getch();
}

2. The distance between two cities (in km.) is input through the keyboard. Write a
program to convert and print this distance in meters, feet, inches and centimeters.


#include<stdio.h>
#include<conio.h>
void main(void)
{
int km;
long int meters, feet, inches, centimeters;
clrscr();
printf(Please enter distance between two cities: );
scanf(%d, &km);
meters=km*1000;
feet=meters*3;
inches=feet*12;
centimeters=meters*100;
printf(Meters=%ld, meters);
printf(Feet=%ld, feet);
printf(Inches=%ld, inches);
printf(centimeters=%ld, centimeters);
getch();
}

3.If the marks obtained by a student in five different subjects are input through the
keyboard, find out the aggregate marks and percentage marks obtained by the
student. Assume that the maximum marks that can be obtained by a student in each
subject is 100.


#include<stdio.h>
#include<conio.h>
void main(void)
{
int maths, phy, chem, eng, urdu, aggr;
float per;
clrscr();
printf(Please enter Marks for Maths, Physics, Chemistry, English and Urdu
respectively: );
scanf(%d%d%d%d%d, &maths, &phy, &chem, &eng, &urdu );
aggr = maths + phy + chem + eng + urdu ;
per= aggr*100.0/500.0;
printf(Aggregate Marks are %d, aggr);
printf(Percentage of the Marks is %f, per);
getch();
}
4. 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>
#include<conio.h>
void main(void)
{
float fahr, centi;
clrscr();
printf(Please enter temperature in Fahrenheit);
scanf(%f, &fahr);
centi=5.0*(fahr-32.0)/9.0
printf(The temperature in Centigrade would be %f, centi);
getch();
}

5. The length & breadth of a rectangle and radius of a circle are input through the
keyboard. Write a program to calculate the area & perimeter of the rectangle, and
the area & circumference of the circle.



#include<stdio.h>
#include<conio.h>
void main(void)
{
int length, breadth, radius;
float area_circle, area_rect, perimeter, circum;
clrscr();
printf(Enter Length and Breadth of rectangle respectively: );
scanf(%d%d, &length, &breadth );
printf(Enter the radius of the Circle:);
scanf(%d, &radius);
area_rect = length*breadth;
perimeter = 2*length + 2*breadth;
area_circle= 3.14*radius*radius;
circum = 2 * 3.14 * radius;
printf(Area of Rect= %f,area_rect);
printf(Perimeter of the rectangle = %f, perimeter);
printf(Area of Circle = %f, area_circle);
printf(Circumference of Circle = %f, circum);
getch();
}

6. 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>
#include<conio.h>
void main(void)
{
int c, d, e;
clrscr();
printf(Enter values for C and D respectively: );
scanf(%d%d, &c, &d);
e=c;
c=d;
d=c;
printf(C=%d, D=%d, c, d);
getch();
}

7. 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>
#include<conio.h>
void main(void)
{
long int num,a,b,c,d,e,f,g,h,sum;
printf(Please enter a five digit number: );
scanf(%ld, &num);
a=num/10,000; // retrives the first digit(left most digit)
b=num%10000;
c=b/1000; //retrieves the second digit
d=b%1000;
e=d/100; //retrieves the third digit
f=d%100;
g=f/10; //retrieves the fourth digit
h=f%10; //retrieves the fifth digit(right most digit)
sum=a+c+e+g+h;
printf(The sum of all digits is %ld, sum);
getch();
}

8. If a five-digit number is input through the keyboard, write a program to reverse the
number.



#include<stdio.h>
#include<conio.h>
void main(void)
{
long int num,a,b,c,d,e,f,g,h, reverse ;
printf(Please enter a five digit number: );
scanf(%ld, &num);
a=num/10,000; // retrives the first digit(left most digit)
b=num%10000;
c=b/1000; //retrieves the second digit
d=b%1000;
e=d/100; //retrieves the third digit
f=d%100;
g=f/10; //retrieves the fourth digit
h=f%10; //retrieves the fifth digit(right most digit)
h=h*10000;
g=g*1000;
e=e*100;
c=c*10;
a=a*1;
reverse=h+g+e+c+a;
printf(The Reverse number is %ld, reverse);
getch();
}

9. If a four-digit number is input through the keyboard, write a program to obtain the
sum of the first and last digit of this number.



#include<stdio.h>
#include<conio.h>
void main(void)
{
int num, a, b, sum;
printf(Please enter a four digit number: );
scanf(%d, &num);
a=num/1000; // retrives the first digit(left most digit)
b=num%10; //retrieves the fourth digit(right most digit)
sum=a+b;
printf(The sum of first and last digit is %d, sum);
getch();
}

10. In a town, the percentage of men is 52. The percentage of total literacy is 48. If
total percentage of literate men is 35 of the total population, write a program to find
the total number of illiterate men and women if the population of the town is 80,000.



#include<conio.h>
#include<stdio.h>
void main(void)
{
long int pop=80000, pop_men, pop_wom, lit, illit, lit_men, lit_wom, illit_men, illit_wom;
clrscr();
pop_men=52*pop/100;
pop_wom=pop - pop_men;
lit=48*pop/100;
illit= pop-lit;
lit_men=35*pop/100;
lit_wom=lit-lit_men;
illit_men= pop_men lit_men;
illit_wom= pop_wom-lit_wom;
printf( illiterate Men=%ld, illit_men);
printf( illiterate Women=%ld, illit_wom);
getch();
}

11. A cashier has currency notes of denominations 10, 50 and 100. If the amount to
be withdrawn is input through the keyboard in hundreds, find the total number of
currency notes of each denomination the cashier will have to give to the withdrawer.



#include<conio.h>
#include<stdio.h>
void main(void)
{
int amount, ten, fifty, hundred;
clrscr();
printf(Please type the amount u need:);
scanf(%d, &amount);
ten=amount/10;
fifty=amount/50;
hundred=amount/100;
printf(The cashier will have to give:\n);
printf(%d notes of Ten, ten);
printf(OR\n);
printf(%d notes of Fifty, fifty);
printf(OR\n);
printf(%d notes of Hundred, hundred);
getch();
}

12. If the total selling price of 15 items and the total profit earned on them is input
through the keyboard, write a program to find the cost price of one item.



#include<conio.h>
#include<stdio.h>
void main(void)
{
int sell_pr, profit, cost_pr, cost_pr_one_item ;
clrscr();
printf(Please enter the selling price of 15 items:);
scanf(%d, &sell_pr);
printf(Please enter the profit earned on 15 items:);
scanf(%d, &profit);
cost_pr = sell_pr profit;
cost_pr_one_item = cost_pr/15;
printf(Cost price of one item is %d, cost_pr_one_item);
getch();
}

13. If a five-digit number is input through the keyboard, write a program to print a
new number by adding one to each of its digits. For example if the number that is
input is 12391 then the output should be displayed as 23402.



#include<stdio.h>
#include<conio.h>
void main(void)
{
long int num,a,b,c,d,e,f,g,h,new;
printf(Please enter a five digit number: );
scanf(%ld, &num);
a=num/10,000; // retrives the first digit(left most digit)
b=num%10000;
c=b/1000; //retrieves the second digit
d=b%1000;
e=d/100; //retrieves the third digit
f=d%100;
g=f/10; //retrieves the fourth digit
h=f%10; //retrieves the fifth digit(right most digit)
a=(a+1)*10000;
c=(c+1)*1000;
e=(e+1)*100;
g=(g+1)*10;
h=(h+1)*1;
new=a+c+e+g+h;
printf(The new number is is %ld, new);
getch();
}
Chapter-02 The Decision Control Structure
Example 2.1: While purchasing certain items, a discount of 10% is offered if the quantity
purchased is more than 1000. If quantity and price per item are input through the keyboard,
write a program to calculate the total expenses.
#include<conio.h>
#include<stdio.h>
void main( void)
{
int qty, dis = 0 ;
float rate, tot ;
printf ( "Enter quantity and rate " ) ;
scanf ( "%d %f", &qty, &rate) ;
if ( qty > 1000 )
dis = 10 ;
tot = ( qty * rate ) - ( qty * rate * dis / 100 ) ;
printf ( "Total expenses = Rs. %f", tot ) ;
getch();
}
Example 2.2: The current year and the year in which the employee joined the organization are
entered through the keyboard. If the number of years for which the employee has served the
organization is greater than 3 then a bonus of Rs. 2500/- is given to the employee. If the years of
service are not greater than 3, then the program should do nothing.
#include<conio.h>
#include<stdio.h>
void main( void)
{
int qty, dis = 0 ;
float rate, tot ;
printf ( "Enter quantity and rate " ) ;
scanf ( "%d %f", &qty, &rate) ;
if ( qty > 1000 )
dis = 10 ;
tot = ( qty * rate ) - ( qty * rate * dis / 100 ) ;
printf ( "Total expenses = Rs. %f", tot ) ;
getch();
}

Example 2.3: In a company an employee is paid as under:
If his basic salary is less than Rs. 1500, then HRA = 10% of basic salary and DA = 90% of basic
salary. If his salary is either equal to or above Rs. 1500, then HRA = Rs. 500 and DA = 98% of
basic salary. If the employee's salary is input through the keyboard write a program to find his
gross salary.
#include<conio.h>
#include<stdio.h>
void main( void)
{
float bs, gs, da, hra ;

printf ( "Enter basic salary " ) ;
scanf ( "%f", &bs ) ;
if ( bs < 1500 )
{
hra = bs * 10 / 100 ;
da = bs * 90 / 100 ;
}
else
{
hra = 500 ;
da = bs * 98 / 100 ;
}
gs = bs + hra + da ;

printf ( "gross salary = Rs. %f", gs ) ;
getch();
}
Example 2.4: The marks obtained by a student in 5 different subjects are input through the
keyboard. The student gets a division as per the following rules:
Percentage above or equal to 60 - First division
Percentage between 50 and 59 - Second division
Percentage between 40 and 49 - Third division
Percentage less than 40 - Fail
Write a program to calculate the division obtained by the student.

#include<conio.h>
#include<stdio.h>
void main( void)
{
int m1, m2, m3, m4, m5, per ;
printf ( "Enter marks in five subjects " ) ;
scanf ( "%d %d %d %d %d", &m1, &m2, &m3, &m4, &m5 ) ;
per = ( m1 + m2 + m3 + m4 + m5 ) / 5 ;
if ( per >= 60 )
printf ( "First division ") ;
else
{
if ( per >= 50 )
printf ( "Second division" ) ;
else
{
if ( per >= 40 )
printf ( "Third division" ) ;
else
printf ( "Fail" ) ;
}
}
getch();
}
Example 2.5: A company insures its drivers in the following cases:
- If the driver is married.
- If the driver is unmarried, male & above 30 years of age.
- If the driver is unmarried, female & above 25 years of age.
In all other cases the driver is not insured. If the marital status, sex and age of the driver are the
inputs, write a program to determine whether the driver is to be insured or not.

#include<conio.h>
#include<stdio.h>
void main( void)
{
char sex, ms ;
int age ;
printf ( "Enter age, sex, marital status " ) ;
scanf ( "%d %c %c", &age, &sex, &ms ) ;
if ( ms == 'M' )
printf ( "Driver is insured" ) ;
else
{
if ( sex == m' )
{
if ( age > 30 )
printf ( "Driver is insured" ) ;
else
printf ( "Driver is not insured" ) ;
}
else
{
if ( age > 25 )
printf ( "Driver is insured" ) ;
else
printf ( "Driver is not insured" ) ;
}
}

getch();
}
Example 2.6: Write a program to calculate the salary as per the following table:

Gender
Years of Service Qualifications Salary
Male >= 10 Post-Graduate 15000
>= 10 Graduate 10000
< 10 Post-Graduate 10000
< 10 Graduate 7000
Female >= 10 Post-Graduate 12000

>= 10 Graduate 9000
< 10 Post-Graduate 10000
< 10 Graduate 6000

#include<conio.h>
#include<stdio.h>
void main( void)
{
char g ;
int yos, qual, sal ;
printf ( "Enter Gender, Years of Service and Qualifications ( 0 = G, 1 = PG ):" ) ;
scanf ( "%c%d%d", &g, &yos, &qual ) ;
if ( g == 'm' && yos >= 10 && qual == 1 )
sal = 15000 ;
else if ( ( g == 'm' && yos >= 10 && qual == 0 ) || ( g == 'm' && yos < 10 && qual == 1 ) )
sal = 10000 ;
else if ( g == 'm' && yos < 10 && qual == 0 )
sal = 7000 ;
else if ( g == 'f' && yos >= 10 && qual == 1 )
sal = 12000 ;
else if ( g == 'f' && yos >= 10 && qual == 0 )
sal = 9000 ;
else if ( g == 'f' && yos < 10 && qual == 1 )
sal = 10000 ;
else if ( g == 'f' && yos < 10 && qual == 0 )
sal = 6000 ;
printf ( "\nSalary of Employee = %d", sal ) ;
getch();
}

If cost price and selling price of an item is input through the keyboard, write a program to
determine whether the seller has made profit or incurred loss. Also determine how much profit
he made or loss he incurred.
#include<conio.h>
#include<stdio.h>
void main(void)
{
int sel_pr, cost_pr, profit, loss;
clrscr();
printf(Please Enter Selling and Cost Price of an Item);
scanf(%d%d, & sel_pr, &cost_pr);
if(sel_pr > cost_pr)
{
profit= sel_pr - cost_pr;
printf(Profit of Rs. %d, profit);
}
else if(sel_pr < cost_pr)
{
loss=cost_pr sel_pr;
printf(Loss of Rs. %d, loss);
}
else
printf(No profit, no loss);
getch();
Any integer is input through the keyboard. Write a program to find out whether it is an odd
number or even number.
#include<conio.h>
#include<stdio.h>
void main(void)
{
int num;
clrscr();
printf(Enter a number);
scanf(%d, &num);
if(num%2==0)
printf(Even);
else
printf(Odd);
getch();
}

Any year is entered through the keyboard, write a program to determine whether the year is leap
or not. Use the logical operators && and ||.
#include<conio.h>
#include<stdio.h>
void main(void)
{
int year;
clrscr();
printf(Enter any year);
scanf(%d, &year);
if(num%4==0)
printf(Leap Year);
else
printf(Not a leap year);
getch();
}
According to the Gregorian calendar, it was Monday on the date 01/01/1900. 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>
#include<conio.h>
void main(void)
{
int yr, lp_yrs, difference, total_days, day_of_week, weekday;

printf("Enter the year:");
scanf ("%d", &yr);

difference = (yr%100); /*difference between entered year and reference year*/

lp_yrs = (yr%100) / 4; /*no. of leap years between concerned year and reference year*/

total_days = (difference*365) + lp_yrs ;

day_of_week = total_days % 7;
if (day_of_week == 1)
printf("The day on Jan 1st of this year is Monday");
else if (day_of_week ==2)
printf("The day on Jan 1st of this year is Tuesday");
else if (day_of_week == 3)
printf("The day on Jan 1st of this year is Wednesday");
else if (day_of_week == 4)
printf("The day on Jan 1st of this year is Thursday");
else if (day_of_week ==5)
printf("The day on Jan 1st of this year is Friday");
else if (day_of_week ==6)
printf("The day on Jan 1st of this year is Saturday");
else if (day_of_week ==0)
printf("The day on Jan 1st of this year is Sunday");

getch();
}
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>
#include<conio.h>
void main(void)
{
long int num,a,b,c,d,e,f,g,h, reverse;
printf(Please enter a five digit number: );
scanf(%ld, &num);
a=num/10,000; // retrives the first digit(left most digit)
b=num%10000;
c=b/1000; //retrieves the second digit
d=b%1000;
e=d/100; //retrieves the third digit
f=d%100;
g=f/10; //retrieves the fourth digit
h=f%10; //retrieves the fifth digit(right most digit)
h=h*10000;
g=g*1000;
e=e*100;
c=c*10;
a=a*1;
reverse=h+g+e+c+a;
if(reverse==num)
printf(The Reverse number and Original number are equall);
else
printf(The Reverse number and Original number are not equall);
getch();
}
If the ages of Ram, Shyam and Ajay are input through the keyboard, write a program to
determine the youngest of the three.
#include<stdio.h>
#include<conio.h>
void main()

{

int ram,shyam,ajay;
printf("Enter the age of RAM, SHYAM & AJAY respectively\n");

scanf("%d%d%d",&ram,&shyam,&ajay);
if (ram>shyam && ram>ajay)
{
printf("RAM Is Youngest.");
}
else if (shyam>ram && shyam>ajay)
{
printf("SHYAM Is Youngest.");
}
else
{
printf("AJAY Is Youngest.");
}
getch();

}
Write a program to check whether a triangle is valid or not, when the 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>
#include<conio.h>
void main(void)
{
int d1,d2,d3;

printf("\nEnter the three degrees:);
scanf("%d%d%d",&d1,&d2,&d3);
if(d1+d2+d3==180)
printf(The triangle is valid);
else
printf(The triangle is not valid);
getch();
}
Find the absolute value of a number entered through the keyboard.
#include<stdio.h>
#include<conio.h>

void main(void)

{

int no;

printf("\nEnter any number:");
scanf("%d",&no);


if(no<1)
no=no*-1;

printf(The absolute value of given number is %d", no);

getch();

}
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>
#include<conio.h>

void main(void)

{

int length, breadth;

printf("\nEnter the length and breadth of a rectangle);
scanf("%d%d",&length, &breadth);

if(length*breadth>2*(length+breadth))
printf(The area is greater);
else
printf(The perimeter is greater);

getch();

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

#include<stdio.h>
#include<conio.h>

void main()

{

int x1, x2, x3, y1, y2, y3 ;

printf("\nEnter the Co-ordinates of first point(x1,y1));
scanf("%d%d",&1x, &y1);

printf("\nEnter the Co-ordinates of 2nd point(x2,y2));
scanf("%d%d",&x2, &y2);
printf("\nEnter the Co-ordinates of 3rd point(x3,y3));
scanf("%d%d",&x3, &y3);
if( (y2-y1)/(x2-x1)==(y3-y2)/(x3-x2) )
printf(The three points lie on straight line);
else
printf(The three points do not lie on straight line);

getch();

}
Given the coordinates (x, y) of a center of a circle and it's radius, write a program which 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>
#include<conio.h>
#include<math.h>
void main(void)
{
int x,y,h,k,r,ans;
clrscr();

printf("enter the center of circle(in (h,k) form seprated by a space):");
scanf("%d%d",&h,&k);
printf("enter the radius of circle:");
scanf("%d",&r);
printf("enter the point to check:");
scanf("%d%d",&x,&y);

ans=(x*x)+(y*y)+(h*h)+(k*k)-( 2*x*h)-(2*y*k)-(r*r);

if(ans<0)printf("the point is in side the circle");
else if(ans>0)printf("the point is out side the circle");
else if(ans==0)printf("the point is on the circle");

getch();
}
Given a point (x, y), write a program to find out if it lies on the x-axis, y-axis or at the origin, viz.
(0, 0).
#include<stdio.h>
#include<conio.h>
void main()
{
int x, y;

printf("\nEnter the Co-ordinates point(x,y));
scanf("%d%d",&x, &y);
if(x==0)
printf(The points lies on y-axis);
else if(y==0)
printf(The points lies on x-axis);
else if(x==0&&y==0)
printf(The points lies on Origin);

getch();
}
Chapter-03 The Loop Control Structure
Write a program to determine whether a number is prime or not. A prime number is one, which is
divisible only by 1 or itself.


#include<stdio.h>
#include<conio.h>
void main(void)
{
clrscr();
int a,num;
printf("Please enter a num:");
scanf("%d", &num);
for(a=2; a<=num-1; a++)
{
if(num%a==0)
{
printf("no prime");
break;
}
}
if(a==num)
printf("Prime");
getch();
}
Write a program to calculate overtime pay of 10 employees. Overtime is paid at the rate of Rs. 12.00 per
hour for every hour worked above 40 hours. Assume that employees do not work for fractional part of an
hour.


Using for loop
#include<stdio.h>
#include<conio.h>
main()
{
int overtime, pay;
for(int a=1; a<=10; a++)
{
printf(Please enter Overtime of %d ,a );
scanf(%d, &overtime);
pay= 12*overtime;
printf(His overtime pay is %d ,pay);
}
getch();
}

Using while loop
#include<stdio.h>
#include<conio.h>
main()
{
int overtime, pay;
int a=1;
while(a<=10)
{
printf(Please enter Overtime of %d ,a );
scanf(%d, &overtime);
pay= 12*overtime;
printf(His overtime pay is %d ,pay);
a++;
}
getch();
}

Using do-while loop
#include<stdio.h>
#include<conio.h>
main()
{
int overtime, pay;
int a=1;
do
{
printf(Please enter Overtime of %d ,a );
scanf(%d, &overtime);
pay= 12*overtime;
printf(His overtime pay is %d ,pay);
a++;
} while(a<=10);
getch();
}
Write a program to find the factorial value of any number entered through the keyboard.


#include<conio.h>
#include<stdio.h>
void main(void)
{
int a, num, fact=1;
printf(Please type a number);
scanf(%d, &num);

for(a=1; a<=num; a++)
{
fact=fact*a;
}
printf(Factorial is %d, fact);
getch();
}
Two numbers are entered through the keyboard. Write a program to find the value of one number raised
to the power of another.
#include <stdio.h>
#include <conio.h>
void main(void)
{
int num,power,answer=1;
printf(Enter number and its power);
scanf(" %d%d",&num, &power);
for(int i=1;i<=power;i++)
answer=answer*num;
printf("result is.. %d",answer);
getch();
}
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>
#include <conio.h>
void main(void)
{
int overtime, pay;
int a=1;
while(a<=255)
{
printf(%d=%c,a,a );
a++;
}
getch();
}
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>
#include <conio.h>
void main()
{
int i=1,a,b,c;
printf("ARMSTRONG NUMBERS BETWEEN 1 to 500 ARE \n");
while (i<=500)
{
a=i%10; /*Extract Last Digit */
b=i%100;
b=(b-a)/10; /*Extract First Digit */
c=i/100;/*Extract first Digit*/
if ((a*a*a)+(b*b*b)+(c*c*c)==i)
printf("%d\n",i);
i++;
}
getch();
}
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>
#include<conio.h>
void main(void)
{
int matchsticks=21, user, computer;
printf("Do not enter Invalid Numbers. Numbers above 4 are invalid.");
printf(If you do so, the computer automatically wins.");
while (matchsticks>=1)
{
printf("\nNumber of matchsticks available right now is %d.", matchsticks);
printf("\n\nYour Turn...\n\n\n");
printf("\nPick up the matchstick(s)-- (1-4): ");
scanf ("%d", &user);
if (user>4)
{
printf("Invalid Selection");
break;
}
computer=5-user;
printf("\nComputers Turn..\n" );
printf("\nComputer chooses:%d", computer);
matchsticks=matchsticks-user-computer;
continue;
if(matchsticks==1)
break;
matchsticks--;
}
printf("\nComputer Wins");
}
Write a program to enter the numbers till the user wants and at the end it should display the count of
positive, negative and zeros entered.
#include<stdio.h>
#include<conio.h>
main()
{
int a,p=0,n=0,z=0,m,i;
printf("enter the nth no:\n");
scanf("%d",&m);
for(i=1;i<=m;i++)
{
printf("enter %d\n",i);
scanf("%d",&a);
if(a>0)
p++;
else if(a<0)
n++;
else
z++;
}
printf("positive=%d \n Negative= %d \n Zeroes= %d \n",p,n,z);
getch();
}
Write a program to find the octal equivalent of the entered number.
Formula to convert Decimal number into equivalent Octal number is as below:

Decimal number: 345
Divide 345 by 8:
345 / 8 = [43] reminder = 1; 1 becomes LSD (Least Significant Digit), the digit which will be at the end of
the number;
Now we need to divide 43 by 8:
43 / 8 = [5] reminder = 3;
Now we need to divide 5 by 8;
5 / 8 = [0] reminder = 5; 5 becomes MSD (Most Significant Digit), the digit which will be at the beginning
of the number;

Now 345 in decimal is equal to 531 in octal.
Now my dear students, implement the above formula to a C program
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>
#include<conio.h>
void main(void)
{
int max=0,min=0;
int temp;
int n,i;
clrscr();
printf("what is the lenght of number set?\n");
scanf("%d",&n);
printf("\n\nNow enter the numbers\n");
for(i=1;i<=n;i++)
{
scanf("%d",&temp);
if(temp>max)max=temp;
if(i==1)min=temp;
if(temp<min)min=temp;
}
printf("\n\nThe range of set is %d",max-min);
getch();
}

Write a program to print all prime numbers from 1 to 300. (Hint: Use nested loops, break and continue)
#include<stdio.h>
#include<conio.h>
main()
{
clrscr();
int number, div, ifprime;
for (number=2;number<=300;number++)
{
ifprime=1;
for (div=2; div<number; div++)
{
if (number%div==0)
{
ifprime=0;
break;
}
ifprime=1;
}
if(ifprime==1)
{
//printf("%d=" ,ifprime);
printf("%d,", number);
}
}
getch();
}
Write a program to fill the entire screen with a smiling face. The smiling face has an ASCII value 1.
#include<stdio.h>
#include<conio.h>
void main(void)
{
clrscr();
for(int a=1;a<=47*(80);a++)
{
printf("%c",1);
}
getch();
}
Write a program to add first seven terms of the following series using a for loop:

#include <stdio.h>
#include<conio.h>
void main(void)
{
float total=0.000, fact=1.00;
for(float a=1;a<=4;a++)
{
fact=fact*a;
total=total+(a/fact);
}
printf("%f",total);
getch();
}
Write a program to generate all combinations of 1, 2 and 3 using for loop.
#include <stdio.h>
#include<conio.h>
void main(void)
{
int i, j, k;
for (i=1; i<=3; i++)
{
for (j=1; j<=3; j++)
{
for (k=1; k<=3; k++)
printf("\n%d %d %d", i, j, k);
}
}

getch();
}
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, which 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>
#include<conio.h>
int main(void)
{
float i,x,y;
clrscr();
for(y=1;y<=6;y++)
{
for(x=5.5;x<=12.5;x+=0.5)
{
i=2+(y+0.5*(x));
printf("%f,",i);
}
}
getch();
}
Write a program to produce the following output:


#include<stdio.h>
#include<conio.h>
void main(void)
{
int iteration, space, last=70,new_value=70, put_space=1;
char alphabet;
printf("ABCDEFGFEDCBA\n");
for (iteration=1; iteration<=6; iteration++)
{
for (alphabet=65; alphabet<=last; alphabet++)
{
printf("%c", alphabet);
continue;
}
last--;
for (space=1; space<=put_space; space++)
{ printf("%c", 32); }
put_space=put_space+2;
for (alphabet=new_value; alphabet>=65; alphabet--)
{
printf("%c", alphabet);
continue;
}
new_value--;
printf("\n");
}
}
Another Way
#include<stdio.h>
#include<conio.h>
void main(void)
{
int i,n=65,m=72,f=0,s=-6;
clrscr();
while(m-->=n)
{
s+=4;
for(i=n;i<m;i++)
printf("%c ",i);
if(f==0) ++m;
else
for(i=0;i<s;i++) printf(" ");
for(i=m-1;i>=n;i--)
printf("%c ",i);
printf("\n");f=1;
}
getch();
}
Write a program to fill the entire screen with diamond and heart alternatively. The ASCII value for heart
is 3 and that of diamond is 4.

#include <stdio.h>
#include<conio.h>
void main(void)
{
for(int a=0;a<=25*79;a++)
{
printf("%c%c",3,4);
}
getch();
}
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


#include<stdio.h>
#include<conio.h>
void main(void)
{
int num;
printf(Please enter a number:);
scanf(%d,&num);
for(int a=1;a<=10;a++)
{
printf(%d*%d=%d\n",num,a,num*a);
}
getch();
}
Write a program to produce the following output:


#include<stdio.h>
#include<conio.h>
#include<math.h>
void main(void)
{
clrscr();
int i,j,k,l=0;
for(i=0;i<4;i++)
{
for(j=-3;j<4;j++)
{
k=i-abs(j);
if(k<0||(k%2)!=0)
printf("\t");
else
printf("%8d",++l);
}
printf("\n");

}
getch();
}
Write a program to produce the following output:


#include<stdio.h>
#include<conio.h>
#include<MATH.H>
void main(void)
{
int i,j,k,l=0; clrscr();
for(i=0;i<5;i++)
{
for(j=-4;j<5;j++)
{
k=i-abs(j)+1;
if(k==1&&k%2==1)
printf("%4d",k);
else if(k>1&&k%2==1)
printf("%4d",k+i-3);
else
printf(" ");
}
printf("\n");
}
getch();
}
When interest compounds q times per year at an annual rate of r % for n years, the principle p
compounds to an amount a as per the following formula


Write a program to read 10 sets of p, r, n & q and calculate the corresponding as.

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
float a,b,c=1,sum=0,p,q,r; int n,k,h;
printf("Enter the value of p, r, q and n");
scanf("%f%f%f%d",&p,&r,&q,&n);
b=a+r/q;
h=q;
for (k=1;k<=n*q;k++)
{
c=c*b;
sum=sum+c;
}
printf("\n\n\n\t\tThe result is........ %f ",p*sum); \
getch();
}
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.
#include <stdio.h>
#include<conio.h>
void main()
{
clrscr();float x,a,b=1,sum=0;
printf("type any number");
scanf("%f",&x);
a=(x-1)/x;
for (int k=1;k<=7;k++)
{
b=b*a;
sum=sum+b;
}
sum=sum-a;
sum=sum/2;
sum=sum+a;
clrscr();
printf ("\n\n\t\t The natural logarithm of %f = %f",x,sum);
getch();
}
Chapter-05 Functions & Pointers

A 5-digit positive integer is entered through the keyboard, write a function to calculate sum of digits of
the 5-digit number:
(1) Without using recursion
(2) Using recursion



Without using recursion


#include<stdio.h>
#include<conio.h>
int add(int);
void main()
{
clrscr(); int a,y;
printf("type any 5 digit number......:");
scanf ("%d",&a);
y=add(a);
printf("%d",y);
getch();
}
int add(int x)
{
int p,t,r,b,c,d,e,f,g,h,i;
p=x/10000;t=x%10000;b=t/1000;c=t%1000;d=c/100;e=c%100;f=e/10;g=e%10;
h=p+b+d+f+g;
return (h);
}
A positive integer is entered through the keyboard, write a program to obtain the prime factors of the
number. Modify the function suitably to obtain the prime factors recursively.


#include<stdio.h>
#include<conio.h>
void factor (int);
void main()
{
int num;
clrscr();
printf("Enter a num: ");
scanf("%d",&num);
clrscr();
printf("\n\n\n\t\tprime factors are: ");
factor(num);
getch();
}
void factor(int n)
{
static int i=2;
if (i<=n)
{
if (n%i==0)
{
printf(" %d, ",i);
n=n/i;
}
else
i++;
factor(n);
}
return;
}
Write a recursive function to obtain the first 25 numbers of a Fibonacci sequence. In a Fibonacci
sequence the sum of two successive terms gives the third term. Following are the first few terms of the
Fibonacci sequence:
1 1 2 3 5 8 13 21 34 55 89...


#include<stdio.h>
#include<conio.h>
void fibo(int,int);
void main()
{
int i,t,old=0,current=1,neu;
clrscr();
printf("%d,",old);
fibo(old,current);
getch();
}
void fibo(int old,int current)
{
static int term=2;
int neu;
if(term<20)
{
neu=old+current;
printf("%d,",neu);
term=term+1;
fibo(current,neu);
}
else
return;
}
A positive integer is entered through the keyboard, write a function to find the binary equivalent of this
number using recursion.

#include<stdio.h>
#include<conio.h>
int binary (int);
void main()
{
int num;
clrscr();
printf("enter the num:");
scanf("%d",&num);
binary(num);
getch();
}
int binary(int n)
{
int r;
r=n%2;
n=n/2;
if(n==0)
{clrscr();
printf("\n\n\n\n\n\n\t\t\tThe binary equiv is %d",r);
return(r);
}
else
binary(n) ;
printf("%d",r);
}
Write a recursive function to obtain the running sum of first 25 natural numbers.



#include<stdio.h>
#include<conio.h>
int getsum(int);
void main()
{
int s;
clrscr();
s=getsum(0);
printf("\nthe sum of 1st 25 natural no is %d",s);
getch();
}
int getsum(int n)
{
int sum=0;
if(n==25)
return sum;
sum=n+getsum(++n);
return (sum);
}
Write a C function to evaluate the series

to five significant digits.




#include<stdio.h>
#include<conio.h>
#include<math.h>
float numerator(float,int);
float denominator(int);
void main()
{
float n,x,a,b,sum=0;
int i,j;
clrscr();
printf("Enter a num x:");
scanf("%f",&x);
for(i=1,j=1;j<=19;j+=2)
{
a=numerator(x,j);
b=denominator(j);
n=a/b;
(i%2==0)?sum=sum-n:(sum=sum+n);
}clrscr();
printf("\n\n\n\n\n\t\tthe sin%f = %f",x,sum);
getch();
}
float numerator(float x1,int j)
{
float k=1;
int m;
for(m=1;m<=j;m++)
k*=x1;
return(k);
}
float denominator(int j)
{
int m;
float h=1.;
for(m=1;m<=j;m++)
h=h*m;
return(h);
}
Given three variables x, y, z write a function to circularly shift their values to right. In other words if x =
5, y = 8, z = 10 after circular shift y = 5, z = 8, x =10 after circular shift y = 5, z = 8 and x = 10. Call the
function with variables a, b, c to circularly shift values.


#include<stdio.h>
#include<conio.h>
void fun(int,int,int);
main()
{
clrscr();
int x,y,z;
printf("Enter the values of x, y, z\n");
scanf("%d%d%d",&x,&y,&z);
printf("\n\n\tValues of x, y, z as entered...:");
getch();
}
void fun(int x,int y, int z)
{
int i,t;
for(i=0;i<=2;i++)
{
t=z;
z=y;
y=x;
x=t;
printf("\n\n\nAfter right shifting values %d time(s)..",i+1);
printf("\nx=%d\ty=%d\tz=5d",x,y,z);
getch();
}
}
Write a function to find the binary equivalent of a given decimal integer and display it.


#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int num, bin;
clrscr();
printf("enter the num:");
scanf("%d",&num);
bin=binary(num);
printf("binary equv of %d is %2.0lf",num,bin);
printf("\nany key to exit");
getch();
}
int binary(int b)
{
int n=b,i=0,r, bin;
while(n!=0)
{
r=n%2;
n=n/2;
bin=(pow(10,i)*r)+bin;
i++;
}
return(bin);
}
If the lengths of the sides of a triangle are denoted by a, b, and c, then area of triangle is given by

where, S = ( a + b + c ) / 2




#include<stdio.h>
#include<conio.h>
#include<math.h>
float area(float a,float b,float c);
void main()
{
float a,b,c,z;
clrscr();
printf("enter 3 side of triangle:");
scanf("%f%f%f",&a,&b,&c);
z=area(a,b,c);
clrscr();
printf("\n\n\n\n\n\n\t\tarea of triangle = %.3f sq.units",z);
getch();
}
float area(float a,float b,float c)
{
float s,m,x;
s=(a+b+c)/2;
m=s*(s-a)*(s-b)*(s-c);
x=sqrt(m);
return(x) ;
}
Write a function to compute the greatest common divisor given by Euclids algorithm, exemplified for J =
1980, K = 1617 as follows:
1980 / 1617 = 1 1980 1 * 1617 = 363
1617 / 363 = 4 1617 4 * 363 = 165
363 / 165 = 2 363 2 * 165 = 33
5 / 33 = 5 165 5 * 33 = 0
Thus, the greatest common divisor is 33.




#include<stdio.h>
#include<conio.h>
int fun(int,int);
main()
{
clrscr();
int j,k,t,r,z;
printf("\n\tEnter two numbers...:\n");
scanf("%d%d",&j,&k);
z=fun(j,k);
printf("\n\n\n\tThr greatest common divisor of two numbers is %d",z);
getch();
return z;}
fun(int j,int k)
{
int r=1,m,n=0,t;
if(k>j)
{
t=j;
j=k;
k=t;
}
if(j==k)
return j;
}
while(1)
{
r=j/k;
m=j-(r*k);
if(!(j%k))
n=k;
if(m==0)
break;
j=k;
k=m;
n=m;
}
return n;
}

You might also like