let us c solution

27
Let us C Solution (www.iuptu.in) (a) Ramesh’s 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<conio.h> #include<stdio.h> main() { int bs; // bs=Basic Salary float a1,a2,gs; // a1= Dearness Allowance, a2= House rent Allowance, gs=Gross Salary clrscr(); printf("Enter Ramesh's Basic Salary : "); scanf("%d",&bs); a1=bs*0.4; a2=bs*0.2; gs=bs-a1-a2; printf("Gross Salary of Ramesh is %f",gs); getch(); } (b) 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> main() { float dkm,dm,df,di,dc; /* dkm=kilometer distance,dm=meter distance,df=feet distance,di=inch distance,dc=centimeter distance */ clrscr(); printf("Enter distance between two cities : "); scanf("%f",&dkm); dm=dkm*1000; // 1km=1000 meters df=dkm*3280.8399; // 1km=3280.8399 feets di=dkm*39370.078; // 1km=39370.078 inches dc=dkm*100000; // 1km=100000 meters printf("\nDistance in\nMeters = %f\nFeets = %f\nInches = %f\nCentimeters = %f",dm,df,di,dc); getch(); } (c) 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> main() {

Upload: amit-birwal

Post on 28-Dec-2015

946 views

Category:

Documents


6 download

DESCRIPTION

let us c Solution

TRANSCRIPT

Page 1: let us c Solution

Let us C Solution (www.iuptu.in)

(a) Ramesh’s 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<conio.h>

#include<stdio.h>

main()

{

int bs; // bs=Basic Salary

float a1,a2,gs; // a1= Dearness Allowance, a2= House rent Allowance, gs=Gross Salary

clrscr();

printf("Enter Ramesh's Basic Salary : ");

scanf("%d",&bs);

a1=bs*0.4;

a2=bs*0.2;

gs=bs-a1-a2;

printf("Gross Salary of Ramesh is %f",gs);

getch();

}

(b) 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>

main()

{

float dkm,dm,df,di,dc; /* dkm=kilometer distance,dm=meter distance,df=feet distance,di=inch distance,dc=centimeter distance */ clrscr();

printf("Enter distance between two cities : ");

scanf("%f",&dkm);

dm=dkm*1000; // 1km=1000 meters

df=dkm*3280.8399; // 1km=3280.8399 feets

di=dkm*39370.078; // 1km=39370.078 inches

dc=dkm*100000; // 1km=100000 meters

printf("\nDistance in\nMeters = %f\nFeets = %f\nInches = %f\nCentimeters =

%f",dm,df,di,dc);

getch();

}

(c) 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>

main()

{

Page 2: let us c Solution

Let us C Solution (www.iuptu.in)

int m1,m2,m3,m4,m5,agg;

float per; // m1=Marks of 1st subjects,agg=Aggregate marks,per=Percentage

clrscr();

printf("Input marks of 5 subjects obtained by student:\n"); // "\n" is used for new line

scanf("%d%d%d%d%d",&m1,&m2,&m3,&m4,&m5);

agg=m1+m2+m3+m4+m5;

per=agg*(100.0/500.0);

printf("Aggregate marks obtained by student = %d\n",agg);

printf("Percentage of student = %f%",per);

getch();

}

(d) 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>

main()

{

float ftemp, ctemp; //ftemp=Fahrenheit Temperature,ctemp=Centigrade Temperature

clrscr();

printf("Enter temperature of a city in Fahrenheit : ");

scanf("%f", &ftemp);

ctemp=(5.0/9.0)*(ftemp-32);

printf("The given temperature in Centigrade is %f",ctemp);

getch();}

(e) 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>

#define pi 3.14

main()

{

float rl,rb,ra,rp,cr,ca,cc;

/* rl=Rectangle Length,rb=Rectange Breadth,ra=Rectange Area,rp=Rectangle Perimeter*/

/* cr=Circle Radius,ca=Circle Area,cc=Circle Circumference*/ clrscr();

printf("Enter Length of Rectangle:");

scanf("%f",&rl);

printf("Enter Breadth of Rectangle:");

scanf("%f",&rb);

printf("Enter Radius of Cirlce:");

scanf("%f",&cr);

ra=rl*rb;

rp=2*(rl+rb);

ca=pi*cr*cr;

cc=2*pi*cr;

Page 3: let us c Solution

Let us C Solution (www.iuptu.in)

printf("\nArea of Rectangle = %f\n", ra);

printf("Perimeter of Rectangle = %f\n", rp);

printf("Area of Cirlce = %f\n", ca);

printf("Circumference of Circle = %f\n", cc);

getch();

}

(f) 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>

main()

{

int C,D;

clrscr();

printf("Enter value of C : ");

scanf("%d",&C);

printf("Enter value of D : ");

scanf("%d",&D);

printf("C = %d\nD = %d",D,C);

getch();

}

(g) 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>

main()

{

int num,sum,a,b,c,d,e,d1,d2,d3,d4,d5;

/*num=entered number,sum=sum of all digits,d1=1st digit,d2=2nd digit,d3=3rd digit,d4=4th digit,d5=5th digit*/

clrscr();

printf("Enter any five digit number : ");

scanf("%d",&num);

a=num/10;

d5=num%10;

b=a/10;

d4=a%10;

c=b/10;

d3=b%10;

d=c/10;

d2=c%10;

e=d/10;

d1=d%10;

sum=d1+d2+d3+d4+d5;

printf("\nSum of digits of given number is %d",sum);

Page 4: let us c Solution

Let us C Solution (www.iuptu.in)

getch();

}

(h) If a five-digit number is input through the keyboard, write a program to reverse the

number

#include<stdio.h>

#include<conio.h>

main()

{

int num,a,b,c,d,e,d1,d2,d3,d4,d5;

//num=entered number,d1=1st digit,d2=2nd digit,d3=3rd digit,d4=4th digit,d5=5th digit

clrscr();

printf("Enter any five digit number : ");

scanf("%d",&num);

a=num/10;

d5=num%10;

b=a/10;

d4=a%10;

c=b/10;

d3=b%10;

d=c/10;

d2=c%10;

e=d/10;

d1=d%10;

printf("\nReverse number = %d%d%d%d%d",d5,d4,d3,d2,d1);

getch();

}

(i) 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>

main()

{

int num,sum,a,b,c,d,d1,d2,d3,d4;

/* num=entered number,sum=sum of 1st & last digit,d1=1st digit,d2=2nd digit,d3=3rd digit,d4=4th digit*/

clrscr();

printf("Enter any four digit number : ");

scanf("%d",&num);

a=num/10;

d4=num%10;

b=a/10;

d3=a%10;

c=b/10;

d2=b%10;

d=c/10;

d1=c%10;

sum=d1+d4;

Page 5: let us c Solution

Let us C Solution (www.iuptu.in)

printf("Sum of 1st and 4th digit = %d",sum);

getch();

}

(j) 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<stdio.h>

#include<conio.h>

main()

{

long double mp,wp,tlp,mlp,im,iw,timw;

/*mp=men %,tlp=Total literacy %,mlp=man literacy %,wp=women %,

im=no of illeterate men,iw=no of illeterate women*/ long int pop=80000;

//pop=Total population

clrscr();

mp=pop*(52.0/100.0);

wp=pop-mp;

tlp=pop*(48.0/100.0);

mlp=pop*(35.0/100.0);

im=mp-mlp;

iw=wp-(tlp-mlp);

printf("%.1Lf %.1Lf",im,iw);

getch();

}

(k) 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<stdio.h>

#include<conio.h>

main()

{

int amt,ten,fif,hun;

/* amt=amount,ten=currency notes of ten,

fif=currency notes of fifty,hun=current notes of hundred*/ clrscr();

printf("Enter amount to be withdrawn : ");

scanf("%d",&amt);

ten=amt/10;

fif=amt/50;

hun=amt/100;

printf("The cashier will give you\n%d Ten notes\nOR\n%d Fifty notes\nOR\n%d Hundred

notes",ten,fif,hun);

getch();

}

Page 6: let us c Solution

Let us C Solution (www.iuptu.in)

(l) 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<stdio.h>

#include<conio.h>

main()

{

int sp,tp,cp,cp1; /*sp=selling price of 15 items,tp=total profit,

cp=cost price of 1 item,cp1=cost price of 1 item*/ clrscr();

printf("Enter total selling price of 15 items : ");

scanf("%d",&sp);

printf("Enter profit on selling 15 items : ");

scanf("%d",&tp);

cp=sp-tp;

cp1=cp/15;

printf("Cost price of one item is %d",cp1);

getch();

}

(m) 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 23502.

#include<stdio.h>

#include<conio.h>

main()

{

int num,res; //num=entered number,res=result clrscr();

printf("Enter any five digit number : ");

scanf("%d",&num);

res=num+11111;

printf("Output is %d",res);

getch();

}

(a) 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<stdio.h>

#include<conio.h>

main()

{

int cp,sp,l,p; //cp=cost price,sp=selling price,l=loss,p=profit

clrscr();

printf("Enter Cost Price of an item : RS ");

Page 7: let us c Solution

Let us C Solution (www.iuptu.in)

scanf("%d",&cp);

printf("Enter Selling Price of an item : RS ");

scanf("%d",&sp);

if(cp>sp) // Loop for Loss

{

l=cp-sp;

printf("You have made LOSS. Your Loss is RS %d",l);

}

else if(sp>cp) // Loop for Profit

{

p=sp-cp;

printf("You have gain PROFIT. Your Profit is RS %d",p);

}

else if(sp=cp) // Loop for no Loss no Profit

{

printf("You have neither Loss nor Profit");

}

getch();

}

(b) Any integer is input through the keyboard. Write a program to find out whether it is an odd number or even number.

#include<stdio.h>

#include<conio.h>

main()

{

int num; // num=number

clrscr();

printf("Enter any integer to know weather its is Even or Odd : ");

scanf("%d",&num);

if(num%2==0)

{

printf("%d is Even number",num);

}

else

{

printf("%d is Odd number",num);

}

getch();

}

(c) Any year is input through the keyboard. Write a program to determine whether the year is a leap year or not. (Hint: Use the % (modulus) operator)

#include<stdio.h>

#include<conio.h>

main()

{

int year;

clrscr();

printf("Enter any year : ");

scanf("%d",&year);

if(year%4==0)

printf("%d is a leap year.",year);

else

Page 8: let us c Solution

Let us C Solution (www.iuptu.in)

printf("%d is not a leap year.",year);

getch();

}

(d) 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.

Coming Soon...

(e) 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.

main()

{

int a,b,c,d,e,f,g,i,j;

clrscr();

printf("Enter the five digit number\n");

scanf("%d",&a);

b=a%10;

c=a/10;

d=c%10;

e=c/10;

f=e%10;

g=e/10;

i=g%10;

j=g/10;

printf("The reverse number is %d%d%d%d%d",b,d,f,i,j);

printf("\nThe original and reverse number is not equal");

getch();

}

(f) If the ages of Ram, Shyam and Ajay are input through the keyboard, write a program to determine the youngest of the three.

main()

{

int ram, shyam, ajay;

clrscr();

printf("Enter ages of Ram, Shayam, Ajay\n");

scanf("%d %d %d", &ram, &shyam, &ajay);

if (ram<shyam)

{

if (ram<ajay)

printf("Ram is younger.");

else

printf("Ajay is younger");

}

else

{

if (shyam<ajay)

printf("Shayam is younger");

else

printf("Ajay is younger");

}

Page 9: let us c Solution

Let us C Solution (www.iuptu.in)

getch();

}

(g) 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>

main()

{

float a1,a2,a3,sum; //a1=angle1,a2=angle2,a3=angle3,sum=sum of all angles

clrscr();

printf("Enter three angle of a triangle : ");

scanf("%f%f%f",&a1,&a2,&a3);

sum=a1+a2+a3;

if(sum==180)

{

printf("Triangle is Valid.");

}

else

{

printf("Triangle is invalid.");

}

getch();

}

(h) Find the absolute value of a number entered through the keyboard.

Submitted by Rahul Raina

void main( )

{

int i, j;

printf ("Enter number : ");

scanf ("%d", &i);

if(i<0)

j = -i;

else

j = i;

printf ("%d", j);

getch();

}

(i) 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>

main()

{

int l,b,a,p; // l=length,b=breadth,a=area,p=perimeter of rectangle

clrscr();

printf("Enter Length of a rectangle : ");

Page 10: let us c Solution

Let us C Solution (www.iuptu.in)

scanf("%d",&l);

printf("Enter Breadth of a rectangle : ");

scanf("%d",&b);

p=2*(l+b);

a=l*b;

if(a>p)

{ // "\n" is use for new line

printf("\nYes! Area[%d] is greater that its perimeter[%d]",a,p);

}

else

{

printf("\nNo! Area[%d] is not greater that its perimeter[%d]",a,p);

}

getch();

}

(j) 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>

main()

{

int x1,y1,x2,y2,x3,y3,m1,m2; // m1=slope 1,m2=slope 2

clrscr();

printf("Enter coordinates of 1st point (x1,y1) : ");

scanf("%d%d",&x1,&y1);

printf("Enter coordinates of 2nd point (x2,y2) : ");

scanf("%d%d",&x2,&y2);

printf("Enter coordinates of 3rd point (x3,y3) : ");

scanf("%d%d",&x3,&y3);

m1=(y2-y1)/(x2-x1);

m2=(y3-y2)/(x3-x2);

if(m1==m2)

{

printf("The given point fall on one straight line.");

}

else

{

printf("The given point does not fall on one straight line.");

}

getch();

}

(k) 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)

Coming Soon...

(l) 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>

Page 11: let us c Solution

Let us C Solution (www.iuptu.in)

main()

{

int x,y;

clrscr();

printf("Enter coordinates of a point (x,y) = ");

scanf("%d%d",&x,&y);

if(x==0&&y!=0)

{

printf("The point lies on y axis.");

}

else if(y==0&&x!=0)

{

printf("The point lies on x axis.");

}

else if(x==0&&y==0)

{

printf("The point lies on origin.");

}

getch();

}

[G] Attempt the following:

(a) 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<stdio.h>

#include<conio.h>

main()

{

int year;

clrscr();

printf("Enter any year : ");

scanf("%d",&year);

if((year%4==0&&year%100!=0)||year%400==0)

{

printf("%d is a Leap Year.");

}

else

{

printf("%d is not a Leap Year.");

}

getch();

}

(b) Any character is entered through the keyboard, write a program to determine whether the character entered is a capital letter, a small case letter, a digit or a special symbol. The following table shows the range of ASCII values for various characters. Characters ASCII Values for various characters.

Page 12: let us c Solution

Let us C Solution (www.iuptu.in)

#include<stdio.h>

#include<conio.h>

main()

{

char a;

clrscr();

printf("Enter any single letter, digit or special symbol : ");

scanf("%c",&a);

if(a>=65&&a<=90)

{

printf("You entered a CAPITAL LETTER.\n");

}

if(a>=97&&a<=122)

{

printf("You entered a SMALL LETTER.\n");

}

if(a>=48&&a<=57)

{

printf("You entered a DIGIT.\n");

}

if((a>=0&&a<=47)||(a>=58&&a<=64)||(a>=91&&a<=96)||(a>=123&&a<=127))

{

printf("You entered an SPECIAL SYMBOL.\n");

}

getch();

}

(c) 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<conio.h>

main()

{

float hard,carbon,tensile;

clrscr();

printf("Enter Hardness of steel : ");

scanf("%f",&hard);

printf("Enter Carbon content of steel : ");

scanf("%f",&carbon);

Page 13: let us c Solution

Let us C Solution (www.iuptu.in)

printf("Enter Trnsile strength of steel : ");

scanf("%f",&tensile);

if(hard>50&&carbon<0.7&&tensile>5600)

printf("\nThe grade of steel is 10");

else if(hard>50&&carbon<0.7)

printf("\nThe grade of steel is 9");

else if(carbon<0.7&&tensile>5600)

printf("\nThe grade of steel is 8");

else if(hard>50&&tensile>5600)

printf("\nThe grade of steel is 7");

else if(hard>50||carbon<0.7||tensile>5600)

printf("\nThe grade of steel is 6");

else

printf("\nThe grade of steel is 5");

getch();

}

(d) A library charges a fine for every book returned late. For first 5 days the fine is 50 paise, for 6-10 days fine is one rupee and above 10 days fine is 5 rupees. If you return the book after 30 days your membership will be cancelled. Write a program to accept the number of days the member is late to return the book and display the fine or the appropriate message.

#include<stdio.h>

#include<conio.h>

main()

{

int days;

clrscr();

printf("Enter the number of days the member is late to return the book : ");

scanf("%d",&days);

if(days<=5)

printf("\nYou must pay 50 paisa fine..."); // '\n' is used for new line

else if(days>=6&&days<=10)

printf("\nYou must pay 1 rupee fine...");

else if(days>10&&days<30)

printf("\nYou must pay 5 rupees fine...");

else if(days>=30)

printf("\nYour membership is cancelled...");

getch();

}

(e) If the three sides of a triangle are entered through the keyboard, write a program to check whether the triangle is valid or not. The triangle is valid if the sum of two sides is greater than the largest of the three sides.

#include<stdio.h>

#include<conio.h>

main()

{

float s1,s2,s3; //s1=side1,s2=side2,s3=side3

clrscr();

printf("Enter three sides of triangle in ascending order:\n");

scanf("%f%f%f",&s1,&s2,&s3);

if(s1+s2>s3)

Page 14: let us c Solution

Let us C Solution (www.iuptu.in)

printf("\nThe triangle is valid.");

else

printf("\nThe triangle is invalid.");

getch();

}

(f) If the three sides of a triangle are entered through the keyboard, write a program to check whether the triangle is isosceles, equilateral, scalene or right angled triangle.

#include<stdio.h>

#include<conio.h>

main()

{

float a1,a2,a3; //a1=angle1,a2=angle2,a3=angle3

clrscr();

printf("Enter three angles of a triangle:\n");

scanf("%f%f%f",&a1,&a2,&a3);

if((a1+a2+a3)==180)

{

if(a1==a2&&a1==a3&&a2==a3)

printf("\nThe triangle is Equilateral.");

else if(a1==a2||a1==a3||a2==a3)

printf("\nThe triangle is Isosceles.");

else if(a1==90||a2==90||a3==90)

printf("\nThe triangle is Right Angled.");

else if(a1!=a2&&a1!=a3&&a2!=a3)

printf("\nThe triangle is Scalene");

}

else

printf("The triangle is not valid");

getch();

}

(g) In a company, worker efficiency is determined on the basis of the time required for a worker to complete a particular job. If the time taken by the worker is between 2 – 3 hours, then the worker is said to be highly efficient. If the time required by the worker is between 3 – 4 hours, then the worker is ordered to improve speed. If the time taken is between 4 – 5 hours, the worker is given training to improve his speed, and if the time taken by the worker is more than 5 hours, then the worker has to leave the company. If the time taken by the worker is input through the keyboard, find the efficiency of the worker.

#include<stdio.h>

#include<conio.h>

main()

{

float h; //h=hours

clrscr();

printf("Enter the time taken by the worker (In Hours) : ");

scanf("%f",&h);

if(h>=2&&h<=3)

printf("\nWorker is highly efficient.");

else if(h>=3&&h<=4)

printf("\nWorker should improve his speed.");

Page 15: let us c Solution

Let us C Solution (www.iuptu.in)

else if(h>=4&&h<=5)

printf("\nWorker should take training to improve the speed.");

else if(h>=5)

printf("\nWorker has to leave the company.");

getch();

}

(h) The policy followed by a company to process customer orders is given by the following rules: (a) If a customer order is less than or equal to that in stock and has credit is OK, supply has requirement. (b) If has credit is not OK do not supply. Send him intimation. (c) If has credit is Ok but the item in stock is less than has order, supply what is in stock. Intimate to him data the balance will be shipped. Write a C program to implement the company policy.

Coming Soon...

[K] Attempt the following:

(a) Using conditional operators determine: (1) Whether the character entered through the keyboard is a lower case alphabet or not.

#include<stdio.h>

#include<conio.h>

main()

{

char ch; //ch=character

clrscr();

printf("Enter any charcter to find out weather it is lower case or not : ");

scanf("%c",&ch);

(ch>=97&&ch<=122?printf("\nThe input character is lower case."):printf("\nThe input character is not lower case."));

getch();

}

(2) Whether a character entered through the keyboard is a special symbol or not.

#include<stdio.h>

#include<conio.h>

main()

{

char ch; //ch=character

clrscr();

printf("Enter any charcter to find out weather it is special symbol or not : ");

scanf("%c",&ch);

((ch>=0&&ch<=47)||(ch>=58&&ch<=64)||(ch>=91&&ch<=96)||(ch>=123&&ch<=127)? printf("\nThe input character is

special symbol."):printf("\nThe input character is not special symbol."));

getch();

}

(b) Write a program using conditional operators to determine whether a year entered through the keyboard is a leap year or not.

Page 16: let us c Solution

Let us C Solution (www.iuptu.in)

#include<stdio.h>

#include<conio.h>

main()

{

int year;

clrscr();

printf("Enter any year : ");

scanf("%d",&year);

((year%4==0&&year%100!=0)||(year%400==0)?printf("\n%d is a leap year.",year):printf("\n%d is not a leap year.",year));

getch();

}

(c) Write a program to find the greatest of the three numbers entered through the keyboard using conditional operators.

#include<stdio.h>

#include<conio.h>

main()

{

int n1,n2,n3; //n1=1st number,n2=2nd number,n3=3rd number

clrscr();

printf("Enter three numbers:\n");

scanf("%d%d%d",&n1,&n2,&n3);

(n1>n2&&n1>n3?printf("\n%d is greater.",n1):(n2>n3&&n2>n1?printf("\n%d is greater.",n2):printf("\n%d is greater.",n3)));

getch();

}

(a) 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.

#include<stdio.h>

#include<conio.h>

main()

{

int employ,otime,opay,hours;

clrscr();

for(employ=1;employ<=10;employ++)

{

printf("\nEnter number of hours worked by %d employee : ",employ);

scanf("%d",&hours);

if(hours>40)

{

otime=hours-40;

opay=otime*12;

printf("The overtime pay of employee is %d",opay);

}

else if(hours<40)

{

printf("The is no overtime pay for employee");

}

}

getch();

}

Page 17: let us c Solution

Let us C Solution (www.iuptu.in)

(b) Write a program to find the factorial value of any number entered through the keyboard.

#include<stdio.h>

#include<conio.h>

main()

{

int num;

float fact=1; //num=number,fact=factorial value

clrscr();

printf("Enter any number to find its factorial value : ");

scanf("%d",&num);

while(num>0)

{

fact=fact*num;

num=num-1; //decrement loop counter

}

printf("\nFactorial value = %.3f",fact); // '%.3f' will show only 3 digits after decimal

getch();

}

(c) 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>

main()

{

int a,b,n1,n2,res=1; // n1=a=1st number,n2=b=2nd number,res=result

clrscr();

printf("Enter any two numbers:\n");

scanf("%d%d",&n1,&n2);

a=n1;

b=n2;

while(n2>0)

{

res=res*n1;

n2- -; // n2=n2-1 can also be written as n2--

}

printf("\n%d raised to the power %d is %d",a,b,res);

getch();

}

(d) 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>

main()

{

int a=0;

clrscr();

while(a<255)

{

printf("%d=%c ",a,a);

a++; // increment statement

Page 18: let us c Solution

Let us C Solution (www.iuptu.in)

}

getch();

}

(e) 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>

main()

{

int a,b,c,d,e,f,res,num=1;

clrscr();

printf("Armstrong Numbers from 1 to 500\n");

while(num<=500)

{

a=num%10;

b=num/10;

c=b%10;

d=b/10;

e=d%10;

f=d/10;

if(num==(a*a*a)+(c*c*c)+(e*e*e))

{

printf("\n%d",num);

}

num++;

}

getch();

}

(f) 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.

Submitted by Rahul Raina

void main( )

{

int x, y, n = 21;

clrscr();

printf ("The Total Amount Of Matchsticks is 21");

while(n > 1)

{

printf ("\nEnter Your Choice : ");

scanf ("%d", &x);

if((1 <= x) && (x <= 4))

{

n = n - x;

printf ("\nThe user chose %d, the matchsticks left %d", x, n);

Page 19: let us c Solution

Let us C Solution (www.iuptu.in)

}

else

{

printf ("\nWrong Entry");

break;

}

y = 5 - x;

n = n - y;

printf ("\nThe computer chose %d, the matchsticks left %d", y, n);

}

if(n == 1)

printf ("\nLast Match Stick Left, You Lose");

getch();

}

(g) 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 num,x,p=0,n=0,z=0;

/* num=number entered,x=Total numbers user want to enter

p=positive numbers,n=negative numbers,z=zeros */

clrscr();

printf("How many numbers do you want to enter ? ");

scanf("%d",&x);

while(x>0)

{

scanf("%d",&num);

if(num>0)

p++;

if(num<0)

n++;

if(num==0)

z++;

x--;

}

printf("\nYou Entered:\n%d Positive Numbers\n%d Negative Numbers\n%d Zeros",p,n,z);

getch();

}

(h) Write a program to find the octal equivalent of the entered number.

#include<stdio.h>

#include<conio.h>

main()

{

long int i,num,arr[100]; // i=loop variable,num=given number,arr[100]=array

clrscr();

printf("Enter any number to convert into Octal : ");

scanf("%ld",&num);

Page 20: let us c Solution

Let us C Solution (www.iuptu.in)

for(i=0;num>8;i++)

{

arr[i]=num%8;

num=num/8;

}

printf("Octal Conversion is\n");

printf("%d",num);

while(i>0)

{

printf("%d",arr[i-1]);

i--;

}

getch();

}

(i) 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.

Coming Soon...

[E] Attempt the following:

(a) 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()

{

int num,i,n=300; // num=prime numbers,n=total no. of prime numbers

clrscr();

for(num=1;num<=300;num++)

{

i=2;

while(i<n-1)

{

if(num%i==0)

break;

i++;

}

if(i==num)

printf("%d ",num);

}

getch();

}

(b) 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>

main()

{

int x=1,a=1;

clrscr();

Page 21: let us c Solution

Let us C Solution (www.iuptu.in)

while(x<80*50)

{

printf("%c",a);

x++;

}

getch();

}

(c) Write a program to add first seven terms of the following series using a for loop:

#include<stdio.h>

#include<conio.h>

main()

{

long double num,res,fres=0, fact=1.0;

/* num=number,res=result,fres=final result,fact=factorial */

clrscr();

for(num=1.0;num<=7.0;num++)

{

fact=fact*num;

res=num/fact;

fres=fres+res;

}

printf("The sum of given series is %.9Lf", fres);

getch();

}

(d) Write a program to generate all combinations of 1, 2 and 3 using for loop.

#include<stdio.h>

#include<conio.h>

main()

{

int a,b,c;

clrscr();

for(a=1;a<=3;a++)

{

for(b=1;b<=3;b++)

{

for(c=1;c<=3;c++)

{

if(a==b||b==c||a==c)

continue;

else

printf("%d %d %d\n",a,b,c);

}

}

}

getch();

}

Page 22: let us c Solution

Let us C Solution (www.iuptu.in)

(e) 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.

Coming Soon...

(f) Write a program to produce the following output: A B C D E F G F E D C B A A B C D E F F E D C B A A B C D E E D C B A A B C D D C B A A B C C B A A B B A A A

#include<stdio.h>

#include<conio.h>

main()

{

int a,x,n=71,o=70,y=1,c;

clrscr();

for(x=1;x<=7;x++)

{

for(a=65;a<=n;a++) // loop for printing ABCDEFG

printf("%c",a);

if(x==2)

o=70;

for(c=2;c<y;c++) //space loop

printf(" ");

for(a=o;a>=65;a--) // loop for printing FEDCBA

printf("%c",a);

printf("\n"); // to sta

n--;

o--;

y=y+2;

}

getch();

}

(g) 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>

main()

{

int a, b,c,d;

clrscr();

for(c=1;c<=37;c++)

{

for(d=1;d<=49;d++)

{

Page 23: let us c Solution

Let us C Solution (www.iuptu.in)

for(a=4;a<=4;a++)

{

for(b=3;b<=3;b++)

printf("%c%c ",a,b);

}

}

}

getch();

}

(h) 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>

main()

{

int res, no, a=1; // res=result,no=number,a=loop variable

clrscr();

printf("Enter any number to know its table till 12 : ");

scanf("%d", &no);

while(a<=12)

{

res=a*no;

printf("%d X %d = %d\n",no,a,res);

a++;

}

getch();

}

(i) Write a program to produce the following output: 1 2 3 4 5 6 7 8 9 10

#include<stdio.h>

#include<conio.h>

main()

{

int a,b,n=6;

clrscr();

for(a=1;a<=10;a++)

{

if(a==1||a==2||a==4||a==7)

{

printf("\n");

for(b=1;b<=n;b++)

printf(" ");

n=n-2;

}

printf("%4d",a);

Page 24: let us c Solution

Let us C Solution (www.iuptu.in)

}

getch();

}

(j) Write a program to produce the following output: 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1

#include<stdio.h>

#include<conio.h>

main()

{

int l,a=8,i=1,s,x,y=1;

clrscr();

for(l=1;l<=5;l++)

{

for(s=1;s<=a;s++)

printf(" ");

printf("%4d",i);

if(l>=3)

{

for(x=1;x<=y;x++)

{

if(x==2&&y==3)

printf("%4d",l+1);

else

printf("%4d",l-1);

}

y++;

}

if(l>=2)

printf("%4d",i);

a=a-2;

printf("\n");

}

getch();

}

(k) A machine is purchased which will produce earning of Rs. 1000 per year while it lasts. The machine costs Rs. 6000 and will have a salvage of Rs. 2000 when it is condemned. If 12 percent per annum can be earned on alternate investments what would be the minimum life of the machine to make it a more attractive investment compared to alternative investment?

Coming Soon...

(l) 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

Page 25: let us c Solution

Let us C Solution (www.iuptu.in)

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

Coming Soon...

(m) 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>

main()

{

int a,b,x,y,z=1;

float res,r1=1;

clrscr();

printf("Enter value of x : ");

scanf("%d",&x);

for(a=1;a<=7;a++)

{

for(y=1,r1=1;y<=z;y++)

{

r1=r1*(x-1)/x;

}

z++;

if(a>=2)

res=res+(0.5*r1);

else

res=res+r1;

}

printf("Sum of first seven terms of given series : %f",res);

getch();

}

[C] Write a menu driven program which has following options: 1. Factorial of a number. 2. Prime or not 3. Odd or even 4. Exit Make use of switch statement.

#include<stdio.h>

#include<conio.h>

Page 26: let us c Solution

Let us C Solution (www.iuptu.in)

main()

{

int choice,fact,num,i;

clrscr();

printf("Choose one of the following option:\n1. Factorial\n2. Prime or Composite\n3. Odd or Even\n4. Exit\n");

scanf("%d",&choice);

switch(choice)

{

case 1:

printf("\nEnter any integer: ");

scanf("%d",&num);

for(i=num,fact=1;num>1;num--)

fact= fact * num;

printf("\n%d! = %d",i,fact);

break;

case 2:

printf("\nEnter any integer: ");

scanf("%d",&num);

for(i=num-1;i>0;--i)

{

if(num%i==0 && i!=1)

{

printf("\nComposite Number");

break;

}

else

{

if(i==1)

printf("\nPrime Number");

else

continue;

}

}

break;

case 3:

printf("\nEnter any integer: ");

scanf("%d",&num);

((num%2==0)?printf("\nEven Number"):printf("\nOdd Number"));

break;

case 4:

break;

default:

printf("\nInvalid Selection!! Please try again.");

}

getch();

}

[D] 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. -If the student gets first class and the number of subjects he failed in is greater than 3, then he does not get any grace. If the number of subjects he failed in is less than or equal to 3 then the grace is of 5 marks per subject. - If the student gets second class and the number of subjects he failed in is greater than 2, then he does not get any grace. If the number of

Page 27: let us c Solution

Let us C Solution (www.iuptu.in)

subjects he failed in is less than or equal to 2 then the grace is of 4 marks per subject. - If the student gets third class and the number of subjects he failed in is greater than 1, then he does not get any grace. If the number of subjects he failed in is equal to 1 then the grace is of 5 marks per subject

#include<stdio.h>

#include<conio.h>

main()

{

int clas,n_o_s; //clas=class,n_o_s=no of subjects in which student fails

clrscr();

printf("Enter the class obtained by the student : ");

scanf("%d",&clas);

printf("Enter the number of subjects he has failed in : ");

scanf("%d",&n_o_s);

switch(clas)

{

case(1):

if(n_o_s>3)

printf("\nStudent does not get any grace marks.");

else if(n_o_s<=3)

printf("\nStudent got 5 grace marks.");

break;

case(2):

if(n_o_s>2)

printf("\nStudent does not get any grace marks.");

else if(n_o_s<=2)

printf("\nStudent got 4 grace marks.");

break;

case(3):

if(n_o_s>1)

printf("\nStudent does not get any grace marks.");

else if(n_o_s==1)

printf("\nStudent got 5 grace marks.");

break;

default:

printf("\nPlease enter class between 1 to 3");

}

getch();

}