[B] Attempt the following:
(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();
}
#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();
}
(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();
}
#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();
}
#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
}
getch();
}
#include<conio.h>
main()
{
int a=0;
clrscr();
while(a<255)
{
printf("%d=%c ",a,a);
a++; // increment statement
}
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();
}
#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);
}
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();
}
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);
}
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();
}
#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);
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();
}
#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);
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();
}
#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();
while(x<80*50)
{
printf("%c",a);
x++;
}
getch();
}
#include<conio.h>
main()
{
int x=1,a=1;
clrscr();
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();
}
#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();
}
#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();
}
(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();
}
#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++)
{
for(a=4;a<=4;a++)
{
for(b=3;b<=3;b++)
printf("%c%c ",a,b);
}
}
}
getch();
}
#include<conio.h>
main()
{
int a, b,c,d;
clrscr();
for(c=1;c<=37;c++)
{
for(d=1;d<=49;d++)
{
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();
}
#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);
}
getch();
}
#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);
}
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();
}
#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
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();
}
#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();
}