You are on page 1of 38

Assignment I

Q1. WAP which will format console output using '\n', '\t', '\b' within printf statement.

Solution

#include<stdio.h>

int main()

printf("\tHello \tWorld\n" );

printf("\tHello \bWorld\n" );

printf("\tHello \tWorld\n" );

return 0;

OUTPUT

Q 2. WAP to print ‘See C is Sea’ five times on the console.

Solution

#include<stdio.h>

int main()

int a;

for(a=0;a<5;a++)

printf("See C is Sea\n");

}
return 0;

OUTPUT

Q 3. WAP which will accept an integer, a decimal number, a character and a string from the keyboard
and display them one per line.

Solution

#include<stdio.h>

int main()

{int a;float b;char ch,d[250];

printf("Enter the integer");

scanf("%d",&a);

printf("Enter the decimal no.");

scanf("%f",&b);

printf("Enter the character");

scanf(" %c",&ch);

printf("\n%c",ch);

fflush(stdin);

printf("\nEnter the string\n");

scanf(" %s",d);

puts(d);

return 0;

}
OUTPUT

Q 4. WAP that will print your mailing address in the following format

First line : Your Name

Second line : House No, Street

Third line : City

Fourth line : State, Pin code

Solution

#include<stdio.h>

main()

printf("Enter your mailing address");

printf("\nNAME= Dhairya Mahajan");

printf("\nHouse no & Street= Hno.120 Randahawa Colony");

printf("\nCity= Pathankot");
printf("\nState= Punjab");

printf("\nPincode= 145001");

return 0;

OUTPUT

5. WAP which will accepts two integers (a and b) from the input device and display the results of their
sum, difference, product, division and mod on the console by assuming a simple arithmetic calculator.
Repeat the same for two decimal numbers.

Solution

#include<stdio.h>

main()

{int a,b;float c,d;

printf("Enter the integers");

scanf("\n%d%d",&a,&b);

printf("Sum is= %d",a+b);

printf("\nDifference is= %d",a-b);

printf("\nProduct is= %d",a*b);

printf("\nDivision is= %d",a/b);


printf("\nRemainder is= %d",a%b);

printf("\n\nEnter the decimal no.");

scanf("\n%f%f",&c,&d);

printf("\nSum is= %f",c+d);

printf("\nDifference is= %f",c-d);

printf("\nProduct is= %f",c*d);

printf("\nDivision is= %f",c/d);

return 0;

OUTPUT

6. WAP which will compute the simple and compound interest and display them on the console.

Solution

#include<stdio.h>

#include<math.h>

main()

{
int p,n,r,si,Amount,ci;

printf("Enter the principal, rate of interest, time period");

scanf("\n%d%d%d",&p,&r,&n);

printf(" Simple Interest\n");

si=(p*n*r)/100;

printf(" Simple Interest is= %d",si);

printf("\n Compound Interest");

Amount=p*pow((1+r/n),n);

ci=Amount-p;

printf("\n Compound Interest=%d",ci);

return 0;

OUTPUT

7. WAP that reads the values of three variables a, b and c from the input device and then compute and
display the value of d, where d = (a-b)/(b+c).

Solution

#include<stdio.h>

main()

float a,b,c,d;

printf("Enter three numbers:");

scanf("\n%f%f%f",&a,&b,&c);

d=(a-b)/(b+c);

printf("ANSWER IS= %f",d);

return 0;

OUTPUT
8. Relationship between Celsius (C) and Fahrenheit (F) is governed by the following formula
F = 9C/5 + 32 WAP to convert the Celsius to Fahrenheit and vice versa.

Solution

#include<stdio.h>

main()

int a,d ; float c,f;

input:

printf("Enter the Conversion Method\n1.C->F\t2.F->C\n");

scanf("%d",&d);

if(d==1)

printf("Enter the temperature value C->F\n");

scanf("%f",&c);

f=(9*c)/5+32;

printf("%f\n",f);

else if(d==2)

printf("Enter the temperature value F->C\n");

scanf("%f",&f);
c=(f-32)*5/9;

printf("%f\n",c);

else {

printf("Not a valid option\n");

goto input;}

return 0;}

OUTPUT

Q9. Area (A) of a triangle is given by the formula Where a, b and c


are sides of the triangle and 2S = a + b + c. WAP to compute the area of the triangle given the values of
a, b and c.

Solution

#include<stdio.h>

#include<math.h>
main()

float a,b,c,s,area;

printf("Enter the sides of triangle");

scanf("%f%f%f",&a,&b,&c);

printf("Sides are=%f\t%f\t%f",a,b,c);

s=(a+b+c)/2;

printf("\nSemiPerimeter=%f",s);

area=sqrt(s*(s-a)*(s-b)*(s-c));

printf("\nArea=%f",area);

return 0;

OUTPUT

Q 10 The line joining the points (2, 2) and (5, 6) which lie on the circumference of a circle is the diameter
of the circle. WAP to compute the area and perimeter of the circle.

Solution

#include<stdio.h>

main()

{float d,r;

d=sqrt(pow(3,2)+pow(4,2));

r=d/2;
printf("\tDiameter is=%f ",d);

printf("\n\n\tArea of Circle= %f",(22*r*r)/7);

printf("\n\n\tArea of Perimeter= %f",(22*2*r)/7);

return 0;

OUTPUT

ASSIGNMENT- II

Q1 WAP to compute the gross salary of an employee whose details are given below: Basic pay:
Rs.50, 000, dearness allowance: 50% of Basic pay, house rent allowance: 20% of Basic pay, vehicle
allowance: 10% of the Basic pay.

Solution

#include<stdio.h>
main()
{
int bp=50000;
int hr,gs,va,da;
printf("Basic pay is= %d",bp);
hr=(bp*.2);
printf("\n House rent is =%d",hr);
da=(.5*bp);
printf("\nDearness allowance=%d",da);
va=(.1*bp);
printf("\nVehicle Allowance=%d",va);
gs=hr+bp+va+da;
printf("\nTotal Gross Salary is=%d",gs);
return 0;
}

OUTPUT

Q2 The price of one pen is Rs.10, one pencil is Rs. 5, and one sharpener is Rs. 2. You purchased 2
pens, 3 pencils and 1 sharpener. Compute the total price as per the following format.

Solution

#include<stdio.h>
main()
{int pen,p,s;
printf("Enter the quantity of item buyed \nPen\nPencil\nSharpner\n");
scanf("%d%d%d",&pen,&p,&s);
printf(" ****** LIST OF ITEMS ******\n");
printf(" ___________________________________\n\n");
printf("Item\t\t\tPrice(Rs.)\t\t\tTotal(Rs.)\n");
printf("Pencil\t\t\t5\t\t\t\t%d",(5*p));
printf("\nPen\t\t\t10\t\t\t\t%d",(10*pen));
printf("\nSharpner\t\t3\t\t\t\t%d",(3*s));
printf("\nTotal items= %d\t\t\t\tTotal Amount=%d",p+pen+s,(5*p+10*pen+3*s));
return 0;

OUTPUT
Q3 WAP which will print the ‘$’ symbol in the following format. (Hint: No need to use any loops)

Solution

#include<stdio.h>
main()
{
printf("$\n$\t$\n$\t$\t$\n$\t$\t$\n$\t$\t$");
return 0;
}

OUTPUT
Q4. WAP which will accept two floating point numbers, assign their sum to an integer variable and
then output the values of all three variables.

Solution

#include<stdio.h>

main()

{int a;

float b,c;

printf("Enter the decimal numbers\n");

scanf("%f%f",&b,&c);

a=b+c;

printf("%d",a);

return 0;

OUTPUT

Q5. WAP to swap two numbers using and without using a third variable.

Solution

#include<stdio.h>

main()

int a,b;

printf("Enter the integers\n");


scanf("\n%d%d",&a,&b);

a=a+b;

b=a-b;

a=a-b;

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

return 0;

OUTPUT

Q6. Extend the above program for three variables a, b, and c such that a holds the value of c, b holds the
value of a, and c holds the value of b using and without using a third variable.

Solution

#include<stdio.h>

main()

int a,b,c;

printf("Enter the integers");

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

a=a+b+c;

b=a-(b+c);
c=a-(b+c);

a=a-(b+c);

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

return 0;

OUTPUT

Q7. WAP which will accept a three digits integer number and display the sum and product of all the
digits of that number. (Hint: Use / and % operators)

Solution

#include<stdio.h>

main()

int a,rem,pro=1,sum=0;

printf("Enter the three digit integer");

scanf("%d",&a);

while(a>1)

rem=a%10;

sum+=rem;
pro*=rem;

a=a/10;

printf("\nSum of all digits is:%d",sum);

printf("\nProduct of no is:%d",pro);

OUTPUT

Q8. WAP to find the size of various primitive data types used in C such as int, float, double and char.

Solution

#include<stdio.h>

main()

int a;

float b;

double c;

char d;

printf("Size of integer %ld bytes\n",sizeof(a));

printf("Size of float %ld bytes\n",sizeof(b));

printf("Size of double %ld bytes\n",sizeof(c));

printf("Size of character%ld byte\n",sizeof(d));


return 0;

OUTPUT
ASSIGNMENT III

Q1 WAP to accept a three digits integer number, and display the number as follows: First line: all
digits Second line: all except the first digit Third line: all except the first two digits

Example: 123

Output:

123

23

Solution

#include<stdio.h>

main()

{int num,i,k;

printf("Enter a three digit integer");

scanf("%d",&num);

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

i=num%100;

k=num%10;

printf("\n%d\n%d ",i,k);

return 0;

OUTPUT
Q 2. WAP which will accept a three digits integer number and do the followings:

i) Check it is an Armstrong number or not. (Hint: A number is an Armstrong number when the
sum of nth power of each digit is equal to the number itself. Here ‘n’ is the number of digits in the given
number. For example, 371 is an Armstrong number since 3^3 + 7^3 + 1^3 = 371. (Hint: use conditional
operator “?:” to avoid if statement. For example, r=2, n=10; printf("%s", (r==n)? "Yes": "No"))

ii) Check it is an automorphic number or not (Hint: An automorphic number is a number whose
square ends with the number itself. For example, 5 is an automorphic number, 5*5 =25. The last digit is
5 which same as the given number. (Hint: use conditional operator “?:” to avoid if statement. For
example, r=2, n=10; printf("%s", (r==n)? "Yes": "No"))

Solution

#include<stdio.h>

int main()

int num,b,c,arm=0,sqr;

printf("i) ");

printf("Enter the three digit no.");

scanf("%d",&num);

b=num;

while(num!=0)

c=num%10;

arm=arm+(c*c*c);

num=num/10;

printf("%d",arm);

if(arm==b)

printf("\n%d,It's an armstrong number",arm);

else
printf("\n%d,It's not anarmstrong number",arm);

printf("\n\nii) ");

sqr=b*b;

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

if(sqr%1000==b)

printf("\n%d,is an automorphic number",b);

else

printf("\n%d,is not an automorphic number",b);

OUTPUT
Q 3. WAP a program which will accept a number (x) in degree and print its sin(x) and cos(x)
function values. (Hint: convert the degree to radians first. Test it for 0, 30, 45, 60, and 90 and print the
results up to 2 decimals.)

Solution

#include<stdio.h>

#include<math.h>

main()

float deg,rad;

printf("Enter the angle in degrees");

scanf("%f",&deg);

rad=(3.14159/180)*deg;

printf("RADIAN ANGLE= %f",rad);

printf("\nsine value is= %f",sin(rad));

printf("\ncosine value is= %f",cos(rad));

return 0;

OUTPUT
Q 4. WAP which will accept three numbers and find the largest and smallest among them using
conditional operator.

Solution

#include<stdio.h>

main()

int num1,num2,num3;char i;

scanf("%d%d%d",&num1,&num2,&num3);

printf("Entered numbers are \n%d\n%d\n%d",num1,num2,num3);

i=(num1>num2?(num1>num3?printf("\n%d is big",num1):printf("\n%d is
big",num3)):printf("\n%d is big",num2));

return 0;

OUTPUT

Q 5. WAP which will accept a three digits integer number and print it in a reverse order. For
example 123 in reverse order is 321. (Hint: Do not print digit by digit; instead print the whole number as
321.)

Solution
#include<stdio.h>

main()

int num,i,j,k;

printf("Enter a number");

scanf("%d",&num);

while(num>0)

i=num%10;

printf("%d",i);

num=num/10;

printf(" is the reversed number");

return 0;

OUTPUT

Q 6. WAP which will accept a floating point number which have three digits before the decimal
number (i.e., three integral part ) and then display the following: First line: the right most digits of the
integral part of the number Second line: the two right most digits of the integral part of the number
Third line: all the digits of the integral part of the number

Example: 356.87

Output:
6

56

356

Solution

#include<stdio.h>

main()

{float num;

int a,b,c;

printf("Enter the number");

scanf("\n%f",&num);

printf("%f",num);

c = (int) num;

a = c % 10;

b = c % 100;

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

return 0;

OUTPUT
ASSIGNMENT IV

Q1 a) WAP to find the largest among three numbers using

(i) nested if statements, (ii) else if statements, (iii) conditional operator ?:

b) WAP to check whether a character is an alphabet, digit. If it is an alphabet then check it is in


upper case or lower case. If it is in lower case then check it is vowel or consonant. If it is a digit
then check whether it is divisible by 2 and 5 or not.

Solution

#include<stdio.h>

main()

{int num1,num2,num3,k;

printf("Enter the three number");

scanf("%d%d%d",&num1,&num2,&num3);

printf("\nPROGRAM BY NESTED IF");

if(num1>num2||num2>num3||num3>num1)

if(num2>num3&&num2>num1)

printf("\n%d is the greatest number",num2);

else if(num3>num1&&num3>num2)
printf("\n%d is the greatest number",num3);

else printf("\n%d is the greatest number",num1);}

printf("\n\nPROGRAM BY IF ELSE");

if(num1>num2&&num1>num3)

printf("\n%d is the greatest number",num1);

else if(num3>num2&&num3>num1)

printf("\n%d is the greatest number",num3);

else

printf("\n%d is the greatest number",num2);

printf("\n\nCONDITIONAL STATEMENT");

k=num1>num2?(num1>num3?num1:num3):(num2>num3?num2:num3);

printf("\n%d is the greatest number",k);

return 0;

OUTPUT
Q2. WAP to calculate and print the Electricity bill of a given customer. The customer id., name
and unit consumed by the user should be taken from the keyboard and display the total amount
to pay by the customer. The charges are as follow :

If bill exceeds Rs. 400 then a surcharge of 15% will be charged and the minimum bill should be
of Rs. 100/-. (Hint: Input: 1001, Naresh, 800, Output : Customer IDNO: 1001, Customer name :
Naresh, unit consumed : 800 , Amount charges @Rs. 2.00 per unit : 1600.00 , Surcharge
amount : 240.00, Net amount paid by the customer: 1840.00)

Solution

#include<stdio.h>

main()

int c_id,unit,net;

float surchrge,tbill;

char name;

printf("Enter the \nCustomer id\nName\nNo.of units consumed");

scanf("%d%s%d",&c_id,&name,&unit);

if (unit<=199)

printf("Your amount per unit is @1.20 & net amount is %d.Rs\n",net=unit*6/5);

else if (unit>=200&&unit<400)

printf("Your amount per unit is @1.50 & net amount is %d.Rs\n",net=unit*3/2);

else if (unit>=400&&unit<600)

printf("Your amount per unit is @1.80 & net amount is %d.Rs\n",net=unit*9/5);

else if (unit>600)
printf("Your amount per unit is @2.0 & net amount is %d.Rs\n",net=unit*2);

if(net>400)

{ surchrge=net*15/100;

printf("\nYour surcharge amount is %f",surchrge);

printf("\n\nTotal bill=%f",surchrge+net);

return 0;

OUTPUT

Q3. WAP to accept a grade and declare the equivalent description

(Hint: Input: Input the Grade: V, Output: You have chosen: Very Good.)

Solution

#include<stdio.h>
int main()

{ char grde;

printf("Enter any of the Grade From\nE\tV\tG\tA\tF\n");

fflush(stdin);

grde=getch();

printf("%c",grde);

if (grde=='E' || grde=='e')

printf("\nGrade description is EXCELLENT");

else if (grde=='V' || grde=='v')

printf("\nGrade description is VERY GOOD");

else if (grde=='G' || grde=='g')

printf("\nGrade description is GOOD");

else if (grde=='A' || grde=='a')

{printf("\nGrade description is AVERAGE");}

else if (grde=='F' || grde=='e')

printf("\nGrade description is FAIL");

else printf("\nINVALID GRADE");

return 0;

OUTPUT

Q4. WAP to read temperature in centigrade and display a suitable message according to
temperature as mentioned below : Temp < 0 then Freezing weather Temp 0-10 then Very Cold weather
Temp 10-20 then Cold weather Temp 20-30 then Normal temperature Temp 30-40 then Its Hot Temp
>=40 then Its Very Hot (Hint: Input : 28 Output : Normal temperature.)
Solution

#include<stdio.h>

main()

float temp;

printf("Enter the Temperature in centigrade ");

scanf("%f",&temp);

if(temp<0)

printf("Freezing Weather");

else if(temp>0&&temp<10)

printf("Very Cold Weather");

else if(temp>=10&&temp<=20)

printf("Cold Weather");

else if(temp>20&&temp<=30)

printf("Normal Temperature");

else if(temp>30&&temp<=40)

printf("Its Hot");

else if(temp>=40)

printf("Its Very Hot");

return 0;

OUTPUT
Q5. WAP to find the eligibility of admission of a student for a professional institute based on the
following criteria: Marks in Mathematics >=65, Marks in Physics >=55,

Marks in Chemistry >=60, Total in all three subjects >=190 or Total in Mathematics and
Chemistry >=130 (Hint: Input: marks obtained in Physics: 55, marks obtained in Chemistry: 61 marks
obtained in Mathematics: 72, Output: The candidate is eligible for admission.)

Solution

#include<stdio.h>

main()

float a,b,c;

printf("Enter the marks in \nMaths:\nPhysics:\nChemistry:");

scanf("%f%f%f",&a,&b,&c);

if(a>=65);

if(b>=55);

if(c>=60);

if((a+b+c>=190)||(a+c>=130))

printf("The candidate is eligible of admission");

else

printf("The candidate is Not eligible of admission");

return 0;

OUTPUT
Q6. WAP a read a three digits number from the keyboard and check whether it is a palindrome
or not. (Hint: If we reverse a number and the reverse number is same as the original number then it is
palindrome. For example, the number 121 is a palindrome.)

Solution

#include<stdio.h>

main()

int num,pal=0,c,dig=0,n1,n2;

printf("Enter a three digit number");

scanf("%d",&num);

n1=num;n2=num;

while(n1!=0)

n1/=10;

dig++;

printf("Number of digits= %d\n",dig);

for(c=0;c<=dig;c++)

pal+=n2%10;

pal*=10;

n2/=10;

pal/=100;

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

if(pal==num)
printf("%d a palindrome",num);

else

printf("%d a not a palindrome",num);

OUTPUT

Q7. WAP to input basic salary of an employee and calculate its Gross salary according to
following: Basic Salary <= 10000 : HRA = 20%, DA = 80% Basic Salary <= 20000 : HRA = 25%, DA = 90%
Basic Salary > 20000 : HRA = 30%, DA = 95%

Solution

#include<stdio.h>

main()

{ int bs,hra,da,gs;

printf("Enter the base salary of the employee");

scanf("%d",&bs);

printf("Salary is %d\n",bs);

if(bs<=10000)

printf("Salary less than 10000\n");

hra=bs*0.2;

da=bs*0.8;

gs=bs+hra+da;
printf("\nGROSS SALARY OF EMPLOYEE IS %d",gs);

else if(bs<=20000)

printf("Salary less than 20000\n");

hra=bs*0.25;

da=bs*0.9;

gs=bs+hra+da;

printf("\nGROSS SALARY OF EMPLOYEE IS %d",gs);

else if(bs>20000)

printf("Salary greater than 20000\n");

hra=bs*0.3;

da=bs*0.95;

gs=bs+hra+da;

printf("\nGROSS SALARY OF EMPLOYEE IS %d",gs);

}}

OUTPUT
Q8. WAP to do the following tasks on a triangle:

a) input angles of a triangle and check whether triangle is valid or not. (Hint: Input all three
angles of triangle in some variable say angle1, angle2 and angle3. Find sum of all three angles, store sum
in some variable say sum = angle1 + angle2 + angle3. Check if (sum = = 180) then, triangle can be formed
otherwise not. In addition, make sure angles are greater than 0 i.e., check condition for angles if
(angle1 != 0 && angle2 != 0 && angle3 != 0).

b) input all sides of a triangle and check whether triangle is valid or not. (Hint: A triangle is valid
if sum of its two sides is greater than the third side. Means if a, b, c are three sides of a triangle.
Then the triangle is valid if all three conditions are satisfied. a + b > c, a + c > b and b + c > a)

c) whether the triangle is equilateral, isosceles or scalene triangle. (Hint: Input sides of a
triangle from user. Store it in some variables say side1, side2 and side3. Check if (side1 = = side2 &&
side2 = = side3), then the triangle is equilateral. If it is not an equilateral triangle then it may be
isosceles. Check if (side1 = = side2 || side1 = = side3 || side2 = = side3), then triangle is isosceles.
If it is neither equilateral nor isosceles then it scalene triangle.)

Solution

a) #include<stdio.h>

main()

int a1,a2,a3,sum;

printf("Enter the three angles of the triangle");

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

sum=a1+a2+a3;

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

printf("Triangle not possible");

else if(sum==180)

printf("Triangle is possible ");

else

printf("Triangle not possible");

return 0;

OUTPUT
b)

#include<stdio.h>

main()

int s1,s2,s3;

printf("Enter the sides of triangle");

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

if((s1+s2>s3)&&(s3+s2>s1)&&(s1+s3>s2))

printf("Triangle is valid");

else

printf("Triangle not possible");

return 0;

OUTPUT

c)

#include<stdio.h>

main()

{
int s1,s2,s3;

printf("Enter the sides of triangle");

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

if(s1==s2&&s2==s3)

printf("It is a equilateral triangle");

else if(s1==s2||s1==s3||s3==s2)

printf("It is a isosceles triangle");

else

printf("It is a scalene triangle");

return 0;

OUTPUT
ASSIGNMENT V

You might also like