`
C Programming lab
SUBJECT CODE: 210
NAME: bhumika bhateja
B.TECH(COMPUTER SCIENCE) II SEM
TEACHER: MR. JAWED AHMed
DATE OF SUBMISSION: 08 april 2013
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.
2. If his basic salary is less than Rs. 1500, then HRA=10% of basic salary and DA=25% of basic. If
his salary is either equal to or above Rs. 1500, then HRA=Rs. 500 and DA=50% of basic. If the
employees salary is input through the keyboard write a program to find his gross salary.
3. 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 75 Honors
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.
4. If a number 972 is entered through the keyboard, your program should print Nine Seven
Two. Write a program such that it does this for any positive integer.
5. Write a program that, for all positive integers i,j,k, and l from 1 through 500, finds and prints
all possible combinations of i , j,k and l such that i+j+k=l and i
12. 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.
13. Write a program to receive an integer and find its octal, hexadecimal and binary equivalent.
14. Write a program to generate all combinations of 1, 2 and 3 using for loop.
15. Write a program to print all prime numbers from 1 to 300. (Hint: Use nested loops, break
and continue).
16. Write a program to print the multiplication table of the number entered by the user.
17. Write a program to check whether number entered through keyboard is palindrome or not.
18. Write a program to print Fibonacci Series from 1 to 100.
19. Write a program to compute and display the sum of all integers that are divisible by 6 but
not divisible by 4 and lie between 0 to 100. The program should also count and display the
numbers of such values.
20. Write a program to compute the real roots of a quadratic equation
ax2 +bx +c =0
The roots are given by the equations
x1= -b + (sqrt(b2-4ac)/2a)
x2= -b - (sqrt(b2-4ac)/2a)
The program should request for the values of the constants a, b and c and print the values of
x1 and x2. Use the following rules:
(a) No solution, if both a and b are zero
(b) There is only one root, if a=0 (x=-c/b)
(c) There are no real roots, if b2-4ac is negative
(d) Otherwise, there are two real roots
Test your program with appropriate data so that all logical paths are working as per your
design. Incorporate appropriate output message.
21. Given a number, write a program using while loop to reverse the digits of the number. For
example, the number 12345 should be written as 54321. Also find the sum of all digits, even
digits and odd digits.
22. An electric power distribution company charges its domestic consumers as follows:
Computation Units Rate of Charge
0-200 Rs. 0.50 per unit
201-400 Rs. 100 plus Rs. 0.65 per unit excess of 200
401-600 Rs. 230 plus Rs. 0.80 per unit excess of 400
601 and above Rs. 390 plus Rs. 1.00 per unit excess of 600
The program reads the customer number and power consumed and prints the amount to be
paid by the customer.
23. Write a program to input ten numbers from keyboard and find the sum of all even numbers
and odd numbers.
24. Write a program to find HCF (Highest Common Factors) of two numbers.
25. Write a program to compute the value of Eulers number (E) that is used as the base of
natural logarithms. Use the following formula.
E=(1/0!)+(1/1!)+(1/2!)+..+1/n!
1. #include #include
main() // Made By Bhumika Bhateja
{
int qty,price,total=0,dis=0;
clrscr();
printf("Enter Price: Rs. ");
scanf("%d",&price);
printf("\nEnter Quantity: ");
scanf("%d",&qty);
if(qty>1000)
dis=0.1*price*qty;
total=(qty*price)-dis;
printf("\nDiscount: Rs. %d",dis);
printf("\n\nTotal Expense: Rs. %d",total);
getch();
}
Enter Price: Rs. 700
Enter Quantity: 1200
Discount: Rs. 84000
Total Expense: Rs. 756000
2. #include #include
main() // Made By Bhumika Bhateja
{
clrscr();
int salary;
float gross,hra,da;
printf("Enter your basic salary: Rs. ");
scanf("%d",&salary);
if(salary
3. #include #include
main() // Made By Bhumika Bhateja
{
clrscr();
float m1,m2,m3,m4,m5,per,tot;
printf("Enter marks in 1st subject: ");
scanf("%f",&m1);
printf("Enter marks in 2nd subject: ");
scanf("%f",&m2);
printf("Enter marks in 3rd subject: ");
scanf("%f",&m3);
printf("Enter marks in 4th subject: ");
scanf("%f",&m4);
printf("Enter marks in 5th subject: ");
scanf("%f",&m5);
tot=m1+m2+m3+m4+m5;
per=tot/5;
printf("Percentage: %f%\n",per);
if(per>=75)
printf("Division: Honors ");
if(per=60)
printf("Division: First Division ");
if(per=50)
printf("Division: Second Division ");
if(per=40)
printf("Division: Third Division ");
if(per
Enter marks in 1st subject: 84 Enter marks in 2nd subject: 89 Enter marks in 3rd subject: 92 Enter marks in 4th subject: 78 Enter marks in 5th subject: 81 Percentage: 84.800003% Division: Honors
(Screen Clears)
Enter marks in 1st subject: 68 Enter marks in 2nd subject: 45 Enter marks in 3rd subject: 56 Enter marks in 4th subject: 48 Enter marks in 5th subject: 34 Percentage: 50.200001% Division: Second Division
4. #include #include
main() // Made By Bhumika Bhateja
{
clrscr();
int number,rev=0,d;
printf("Enter number: ");
scanf("%d",&number);
while(number>0)
{
d=number%10;
rev=d+rev*10;
number/=10;
}
while(rev>0)
{
d=rev%10;
rev/=10;
switch(d)
{
case 1: printf("One "); break;
case 2: printf("Two "); break;
case 3: printf("Three "); break;
case 4: printf("Four "); break;
case 5: printf("Five "); break;
case 6: printf("Six "); break;
case 7: printf("Seven "); break;
case 8: printf("Eight "); break;
case 9: printf("Nine "); break;
case 0: printf("Zero "); break;
default: printf("Wrong choice");
}
}
getch();
}
Enter number: 972 Nine Seven Two (Screen Clears) Enter number: 842 Eight Four Two
5. #include #include
main() // Made By Bhumika Bhateja
{
clrscr();
int i,j,k,l;
printf("Combinations are: ");
for(i=1;i
6. #include #include
main() // Made By Bhumika Bhateja
{
clrscr();
int a,b,c,d,i=0,k=0,l=0,base;
int A[30],ar[30],arr[30];
printf("\nEnter any Decimal Number: ");
scanf("%d",&a);
d=c=a;
printf("\n Enter Base: ");
scanf("%d",&base);
if(base==2)
{
while(a>0)
{
b=a%2;
A[i]=b;
++i;
a=a/2;
}
printf("\nBinary Conversion: ");
for(int j=i-1; j>=0; --j)
printf("%d",A[j]);
}
if(base==8)
{
while(c>0)
{
b=c%8;
ar[k]=b;
++k;
c=c/8;
}
printf("\nOctal Conversion: ");
for(j=k-1; j>=0; --j)
printf("%d",ar[j]);
}
if(base==16)
{
while(d>0)
{
b=d%16;
arr[l]=b;
++l;
d=d/16;
}
printf("\nHexadecimal Conversion: ");
for(j=l-1; j>=0; --j)
{
switch(arr[j])
{
case 10: printf("%c",65);
break;
case 11: printf("%c",66);
break;
case 12: printf("%c",67);
break;
case 13: printf("%c",68);
break;
case 14: printf("%c",69);
break;
case 15: printf("%c",70);
break;
default: printf("%d",arr[j]);
}
}
getch();
}
Enter any Decimal Number: 64
Enter base: 2
Binary Conversion: 1000000
(Screen Clears)
Enter any Decimal Number: 64
Enter base: 8
Octal Conversion: 100
(Screen Clears)
Enter any Decimal Number: 64
Enter base: 16
Hexadecimal Conversion: 40
7. #include #include
main() // Made By Bhumika Bhateja
{
clrscr();
char i;
printf("Enter character: ");
scanf("%c",&i);
if(i>=65&&i=97&&i=48&&i=0&&i=58&&i=91&&i=123&&i
8. #include #include
main() // Made By Bhumika Bhateja
{
clrscr();
int f=1,num,i;
printf("Enter number: ");
scanf("%d",&num);
for(i=1;i
9. #include #include
main() // Made By Bhumika Bhateja
{
clrscr();
int a,b,i,res=1;
printf("Enter 1st number: ");
scanf("%d",&a);
printf("Enter 2nd number: ");
scanf("%d",&b);
for(i=0;i
10. #include
#include
main() // Made By Bhumika Bhateja
{
clrscr();
int i=0;
while(i
11. #include
#include
main() // Made By Bhumika Bhateja
{
clrscr();
int i,sum,d,j;
printf("Armstrong numbers are: ");
for(i=1;i0)
{
d=j%10;
sum=sum+(d*d*d);
j/=10;
}
if(sum==i)
{
printf("%d ",i);
}
}
getch();
}
Armstrong numbers are: 1 153 370 371 407
12. #include
#include
main() // Made By Bhumika Bhateja
{
clrscr();
int a[100],i=0,j,cntp=0,cntn=0,cntz=0;
char ans;
do
{
printf("\nEnter number: ");
scanf("%d",&a[i]);
i++;
printf("\nDo you want to continue (y/n): ");
scanf("%s",&ans);
}while(ans=='y');
for(j=0;j0)
cntp++;
if(a[j]
Enter number: 10
Do you want to continue (y/n): y
Enter number: -90
Do you want to continue (y/n): y
Enter number: 0
Do you want to continue (y/n): y
Enter number: 78
Do you want to continue (y/n): y
Enter number: -39
Do you want to continue (y/n): n
No. of postivie nos: 2
No. of negative nos: 2
No. of zeros: 1
13. #include
#include
main() // Made By Bhumika Bhateja
{
clrscr();
int a,b,c,d,i=0,k=0,l=0;
int A[30],ar[30],arr[30];
printf("\nEnter any Decimal Number: ");
scanf("%d",&a);
d=c=a;
while(a>0)
{
b=a%2;
A[i]=b;
++i;
a=a/2;
}
printf("\nBinary Conversion: ");
for(int j=i-1; j>=0; --j)
printf("%d",A[j]);
while(c>0)
{
b=c%8;
ar[k]=b;
++k;
c=c/8;
}
printf("\n\nOctal Conversion: ");
for(j=k-1; j>=0; --j)
printf("%d",ar[j]);
while(d>0)
{
b=d%16;
arr[l]=b;
++l;
d=d/16;
}
printf("\n\nHexadecimal Conversion: ");
for(j=l-1; j>=0; --j)
{
switch(arr[j])
{
case 10: printf("%c",65);
break;
case 11: printf("%c",66);
break;
case 12: printf("%c",67);
break;
case 13: printf("%c",68);
break;
case 14: printf("%c",69);
break;
case 15: printf("%c",70);
break;
default: printf("%d",arr[j]);
}
}
getch();
}
Enter any Decimal Number: 64
Binary Conversion: 1000000
Octal Conversion: 100
Hexadecimal Conversion: 40
(Screen Clears)
Enter any Decimal Number: 32
Binary Conversion: 100000
Octal Conversion: 40
Hexadecimal Conversion: 20
14. #include
#include
main() // Made By Bhumika Bhateja
{
clrscr();
int i,j,k;
printf("All combinations are: ");
for(i=1;i
15. #include
#include
main() // Made By Bhumika Bhateja
{
clrscr();
int i,j;
printf("Prime numbers are: ");
for(i=1;i
16. #include
#include
main() // Made By Bhumika Bhateja
{
clrscr();
int i,num,m;
printf("Enter number: ");
scanf("%d",&num);
printf("Multiplication table: ");
for(i=1;i
17. #include
#include
main() // Made By Bhumika Bhateja
{
clrscr();
int number,rev=0,d,temp;
printf("Enter number: ");
scanf("%d",&number);
temp=number;
while(number>0)
{
d=number%10;
rev=d+rev*10;
number/=10;
}
if(temp==rev)
printf("Palindrome ");
else
printf("Not a Palindrome ");
getch();
}
Enter number: 121
Palindrome
(Screen Clears)
Enter number: 123
Not a Palindrome
18. #include
#include
main() // Made By Bhumika Bhateja
{
clrscr();
int a=1,b=2,i,c;
printf("Fibonacci series: ");
printf("%d %d ",a,b);
for(i=3;i
19. #include
#include
main() // Made By Bhumika Bhateja
{
clrscr();
int i,sum=0,cnt=0;
printf("Numbers are: ");
for(i=0;i
20. #include
#include
#include
main() // Made By Bhumika Bhateja
{
clrscr();
int a,b,c,d;
float x1,x2;
printf("\nEnter the value of a: ");
scanf("%d",&a);
printf("\nEnter the value of b: ");
scanf("%d",&b);
printf("\nEnter the value of c: ");
scanf("%d",&c);
d=b*b-4*a*c;
if(d==0)
{
printf("\nBoth roots are equal");
x1=-b/(2*a);
x2=x1;
printf("\n\nFirst Root x1= %f",x1);
printf("\n\nSecond Root x2= %f",x2);
}
if(d>0)
{
printf("\nBoth roots are real and different");
x1=(-b+sqrt(d))/(2*a);
x2=(-b-sqrt(d))/(2*a);
printf("\n\nFirst Root x1= %f",x1);
printf("\n\nSecond Root x2= %f",x2);
}
else
printf("\nNo Solution");
getch();
}
Enter the value of a: 1
Enter the value of b: 3
Enter the value of c: 2
Both roots are real and different
First Root x1= -1.000000
Second Root x2= -2.000000
(Screen Clears)
Enter the value of a: 1
Enter the value of b: 1
Enter the value of c: 1
No Solution
21. #include
#include
main() // Made By Bhumika Bhateja
{
clrscr();
int number,rev=0,d,sum=0,sume=0,sumo=0;
printf("Enter number: ");
scanf("%d",&number);
while(number>0)
{
d=number%10;
sum=sum+d;
if(d%2==0)
sume=sume+d;
else
sumo=sumo+d;
rev=d+rev*10;
number/=10;
}
printf("Reversed Number is: ");
printf("%d",rev);
printf("\nSum of digits is: ");
printf("%d",sum);
printf("\nSum of even digits is: ");
printf("%d",sume);
printf("\nSum of odd digits is: ");
printf("%d",sumo);
getch();
}
Enter number: 4326
Reversed Number is: 6234
Sum of digits is: 15
Sum of even digits is: 12
Sum of odd digits is: 3
22. #include
#include
main() // Made By Bhumika Bhateja
{
clrscr();
int custno,powercons;
float amount;
printf("Enter customer number: ");
scanf("%d",&custno);
printf("Enter power consumed: ");
scanf("%d",&powercons);
if(powercons=201 && powercons=401 && powercons=601)
amount=390+(powercons-600);
printf("Amount to be paid is: Rs. ");
printf("%f",amount);
getch();
}
Enter customer number: 201
Enter power consumed: 250
Amount to be paid is: Rs. 132.500000
(Screen Clears)
Enter customer number: 104
Enter power consumed: 560
Amount to be paid is: Rs. 358.000000
23. #include
#include
main() // Made By Bhumika Bhateja
{
clrscr();
int a[10],i,sume=0,sumo=0;
for(i=0;i
24. #include
#include
main() // Made By Bhumika Bhateja
{
clrscr();
int a, b, x, y, t, hcf;
printf("Enter 1st number: ");
scanf("%d",&x);
printf("Enter 2nd number: ");
scanf("%d",&y);
a = x;
b = y;
while (b != 0)
{
t = b;
b = a % b;
a = t;
}
hcf = a;
printf("HCF of %d and %d = %d\n", x, y, hcf);
getch();
}
Enter 1st number: 10
Enter 2nd number: 5
HCF of 10 and 5 = 5
(screen Clears)
Enter 1st number: 12
Enter 2nd number: 36
HCF of 12 and 36 = 12
25. #include
#include
main() // Made By Bhumika Bhateja
{
clrscr();
int n,i,f,j;
float sum=1;
printf("Enter the value of n: ");
scanf("%d",&n);
for(i=1;i0;j--)
f=f*j;
sum=sum+(1.0/f);
}
printf("Sum is: %f",sum);
getch();
}
Enter the value of n: 5
Sum is: 2.716667
(Screen Clears)
Enter the value of n: 3
Sum is: 2.666667