programming chapter no 3

22
Let Us C / Chapter 3 (The Loop Control Structure) Exercise [B] (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. Solution: #include<stdio.h> #include<conio.h> main() { int wrkh,hr,emp,pay,r=12; clrscr(); printf("Please enter working hours of 10 employees: \n\n"); emp=1; while(emp<=10) { printf("\n\nEnter working hours of employee: ",emp); scanf("%d",&wrkh); if(wrkh>40) { hr=wrkh-40; pay=hr*r; printf("\n%d\tOver-time pay %d rs\n",emp,pay); } else {

Upload: no-company

Post on 21-Aug-2015

118 views

Category:

Education


2 download

TRANSCRIPT

Page 1: Programming chapter no 3

Let Us C / Chapter 3 (The Loop Control Structure)                                Exercise [B]

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

Solution:

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

main() {

int wrkh,hr,emp,pay,r=12;clrscr();

printf("Please enter working hours of 10 employees: \n\n");

emp=1;

while(emp<=10) {

printf("\n\nEnter working hours of employee: ",emp);

scanf("%d",&wrkh);

if(wrkh>40) {

hr=wrkh-40;pay=hr*r;

printf("\n%d\tOver-time pay %d rs\n",emp,pay);

}

else {

printf("\n%d\tNo over time pay\n",emp);

}

emp++;

  }

getch();return 0;

Page 2: Programming chapter no 3

}

------------------------------------------------------------------------------------------------------------

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

Solution:

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

main() {

long i,j,fact=1;clrscr();

printf("Please enter any number: ");scanf("%ld",&i);

for(j=i;j>=1;j--) {

fact=fact*j;

}

printf("Factorial value = %ld ",fact);

getch();return 0;

}

------------------------------------------------------------------------------------------------------------

(c) Two numbers are entered through the keyboard. Write a program to find the value of one number raised to the power of another.

Solution:

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

main() {

int num1,num2,i,rslt=1;clrscr();

printf("Enter two numbers: \n");scanf("%d%d",&num1,&num2);

Page 3: Programming chapter no 3

for(i=1;i<=num2;i++) {rslt=rslt*num1;

if(i==num2) {break;}

  }

printf("\n\nvalue of %d raised to the power of %d is = %d\n",num1,num2,rslt);

getch();return 0;

}

------------------------------------------------------------------------------------------------------------

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

Solution:

#include<stdio.h>#include<conio.h>main() {

int i=0;clrscr();

while(i<=255) {

printf(" %d %c \n",i,i);i++;

}

getch();return 0;

}

------------------------------------------------------------------------------------------------------------

(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

Page 4: Programming chapter no 3

number. For example, 153 = ( 1 * 1 * 1 ) + ( 5 * 5 * 5 ) + ( 3 * 3 * 3 )

Solution:

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

void main() {

int i=1, num, r, ar=0;

clrscr();

while(i<=500) {

num = i;

while(num!=0) {

r = num % 10;

ar = ar + ( r*r*r );

num = num / 10;

}

if(ar==i)printf("%3d ",i);

ar=0;i++;}

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.

Page 5: Programming chapter no 3

− Whoever is forced to pick up the last matchstick loses the game.

Solution:

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

main() {

int tms=21,lms=0,user,cpu,pms=0;char another;

clrscr();

do {

printf("\nPick up 1,2,3 or 4 matchsticks: ");scanf("%d",&user);    /* Matchsticks picked by you */

cpu=user;            /* Matchsticks picked by computer */

if(pms<20) {printf("\nyou picked = %d\tcomputer picked = %d\n",user,cpu);}else {printf("\nyou picked =%d\n",user);}

pms=pms+(cpu+user);   /* Total picked Matchsticks */

lms=tms-pms;          /* Total left matchsticks */

if(pms<=21) {printf("\nleft matchsticks = %d\n",lms);}else {break;}

printf("\nPick again Y/N: ");scanf(" %c",&another);

}while(another=='y');

printf("\n\n\n");printf("\n==============\n");printf("\nComputer wins!\n");printf("\n==============\n");

getch();return 0;

}

Page 6: Programming chapter no 3

------------------------------------------------------------------------------------------------------------

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

Solution:

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

main() {

int num,tnum=0,tp=0,tn=0,tz=0;char another;

clrscr();

do {printf("\nEnter any number: ");scanf("%d",&num);

tnum=tnum+1;      /* count of total numbers entered by you */

if(num==0)         /* count of total zeros */tz=tz+1;

if(num>0)         /* count of total positive numbers */tp=tp+1;

if(num<0)         /* count of total negative number */tn=tn+1;

printf("\nEnter another number Y/N: ");scanf(" %c",&another);

}while(another=='y');

clrscr();

printf("\n\n\nTotal numbers entered = %d\n",tnum);printf("\nTotal zeros = %d\n",tz);printf("\nTotal positive numbers = %d\n",tp);printf("\nTotal negative numbers = %d\n",tn);

getch();return 0;

}

------------------------------------------------------------------------------------------------------------

Page 7: Programming chapter no 3

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

Solution:

#include<stdio.h>#include<conio.h>main() {

long num,n;clrscr();

printf("Please enter the number:  ");scanf("%ld",&num);printf("\n\nOctal equivalent:  ");

while(num!=0) {

n=num%8;

printf("%ld",n);

num=num/8;        /* devision by 8 */

}

gotoxy(20,6);printf("<%c%c%c%c%c read from right to left",196,196,196,196,196);

getch();return 0;}

------------------------------------------------------------------------------------------------------------

(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

Solution:#include<stdio.h>#include<conio.h>main() {

int min,max,num,range,flag=0;char ch;

clrscr();

do {

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

Page 8: Programming chapter no 3

if(flag==0) {

min=num;max=num;}

flag++;       /* counting total numbers */

if(min>num)  {min=num;}

if(max<num)max=num;

printf("\n\nEnter another number(Y/N): ");scanf(" %c",&ch);

clrscr();}while(ch=='Y' || ch=='y');

range=max-min;

printf("\nTotal number = %d\n",flag);printf("\nLargest number = %d\n",max);printf("\nSmallest number = %d\n",min);printf("\nRange = %d\n",range);

getch();return 0;}______________________________________________________________________

                         Exercise [E]

(a) Write a program to print all prime numbers from 1 to 300. (Hint: Use nested loops, break and continue)

Solution:#include<stdio.h>#include<conio.h>main() {

int i=1,j;

clrscr();

printf("\nPrime numbers between 1 to 300 are:\n\n");

while(i!=300) {

j=2;

Page 9: Programming chapter no 3

while(j<i) {

if(i%j==0) {break;}

j++;

}

if(j==i) {

printf(" %d",i);

}

i++;}

getch();return 0;

}

------------------------------------------------------------------------------------------------------------

(b) Write a program to fill the entire screen with a smiling face. The smiling face has an ASCII value 1.

Solution:

#include<stdio.h>#include<conio.h>main() {

int i;clrscr();

for(i=1;i<=10000;i++) {printf("%c",1);}

getch();return 0;

}------------------------------------------------------------------------------------------------------------

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

Page 10: Programming chapter no 3

1/1! + 2/2! + 3/3! + .............

Solution:

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

main() {

float i,j,k,fact=1,factsum,numsum;clrscr();

printf("\tTerm\t\tFactorial\n\n");

for(i=1;i<=7;i++) {

for(j=i;j>=2;j--) {

fact=fact*j;factsum=factsum+fact;break;}numsum=numsum+i;printf("\t%.0f\t\t%.0f\n",i,fact);}

k=numsum/factsum;

printf("\n\nsum of 7 terms = %.0f ",numsum);printf("\nsum of all factorials = %.0f",factsum);printf("\n\naddition of all terms / sum of factorials: %f",k);

getch();return 0;}

------------------------------------------------------------------------------------------------------------

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

Solution:

#include<stdio.h>#include<conio.h>main() {

int i,j,k;clrscr();

printf("All combinations of 1,2,3 are:\n\n");for(i=1;i<=3;i++) {for(j=1;j<=3;j++) {for(k=1;k<=3;k++) {

Page 11: Programming chapter no 3

printf(" %d%d%d ",i,j,k);} }  }

getch();return 0;}

------------------------------------------------------------------------------------------------------------

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

Solution:

#include<stdio.h>#include<conio.h>main() {

float i=1,x,y;clrscr();

for(y=1.0;y<=6.0;y++) {

for(x=5.5;x<=12.5;x+=0.5) {

i=2+(y+(0.5*0.5));

printf("%.2f\t%.2f\t%.2f\n",i,y,x);

}

 }

getch();return 0;

}

------------------------------------------------------------------------------------------------------------

(f) Write a program to produce the following

Page 12: Programming chapter no 3

output:

A B C D E F G F E D C B AA B C D E F     F E D C B AA B C D E           E D C B AA B C D                 D C B AA B C                        C B AA B                               B AA                                      A

Solution:

#include<stdio.h>#include<conio.h>main() {

int i,j,k,l;clrscr();

for(i=71;i>=65;i--) {

/* loop for printing ascending letters */

for(j=65;j<=i;j++) {

printf("%c ",j);

}

/* loop for making a space between patterns */

for(k=i+1;k<=71;k++) {

if(k==71)printf("  ");

if(k<71)printf("    ");}

/* loop to print descending letters */

for(l=i;l>=65;l--) {

if(l==71) {     /* to skip printing 'G' twice */continue;}

printf("%c ",l);

}printf("\n");

Page 13: Programming chapter no 3

}

getch();

return 0;}

------------------------------------------------------------------------------------------------------------

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

Solution:

#include<stdio.h>#include<conio.h>main() {

int i,j;clrscr();

for(i=j=1;i<=10000,j<=10000;i++,j++) {printf("%c%c",4,3);}

getch();return 0;

}

------------------------------------------------------------------------------------------------------------

(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 = 2929 * 2 = 58…

Solution:

#include<stdio.h>#include<conio.h>main() {

int i,j,k;clrscr();

printf("Please enter the number:\n");scanf("%d",&i);

Page 14: Programming chapter no 3

for(j=1;j<=10;j++) {k=i*j;printf("\n%d*%d = %d\n",i,j,k);}

getch();return 0;

}

------------------------------------------------------------------------------------------------------------

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

Solution:

#include<stdio.h>#include<conio.h>void main() {

int i,num=0,k,j;clrscr();

for(i=1;i<=4;i++) {

for(j=4;j>=i;j--) {      /* loop for making a space from left corner */

printf("  ");

}

for(k=1;k<=i;k++) {

num=num+1;              /* incrementing external number for printing */

printf("  %d ",num);

}

printf("\n\n");}

getch();

}

Page 15: Programming chapter no 3

-----------------------------------------------------------------------------------------------------------

(j) Write a program to produce the following output:

            1         1    1        1  2  1      1  3  3  1    1  4  6  4  1

Solution:

#include<stdio.h>#include<conio.h>main() {

int i,j,k;clrscr();

printf("      1\n\n\n");

for(i=1;i<=4;i++) {

for(k=5;k>=i;k--) {printf(" ");}printf("1 ");

for(j=1;j<=i;j++) {

if(i==4 && j==3)  {printf("%d ",i+j-1);continue;}

if(j>1)printf("%d ",i);

}

printf("1\n\n\n");

}

getch();return 0;

}

-----------------------------------------------------------------------------------------------------------

Page 16: Programming chapter no 3

(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?

Solution:

#include <stdio.h>#include <conio.h>int main(){int year=0,inv,altn;

while(altn>inv) {

year+=1;altn=120*year;inv=(1000*year)-(6000-2000);

}

printf("Minimum life of the machine would be = %d",year);  getch();  return 0;}------------------------------------------------------------------------------------------------------------

(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 formulaa = p ( 1 + r / q ) nqWrite a program to read 10 sets of p, r, n & q and calculate the corresponding as.

Solution:

#include<stdio.h>#include<conio.h>main() {

int i,p,r,n,q,j,pw;float a;

Page 17: Programming chapter no 3

clrscr();

for(i=0;i<10;i++) {

printf("Set number %d:\n\n\n\n",i+1);

printf("Enter p:  ");scanf("%d",&p);

printf("\n\nEnter r:  ");scanf("%d",&r);

printf("\n\nEnter n:  ");scanf("%d",&n);

printf("\n\nEnter q: ");scanf("%d",&q);

pw=n*q;

for(j=0;j<pw;j++) {

a = a + p *(1+r/1);

}printf("\n\n\nA = %f\n",a);

printf("\n\n\n\npress any key to proceed...");getch();

clrscr();}

getch();return 0;}

------------------------------------------------------------------------------------------------------------

(m) The natural logarithm can be approximated by the following series.x-1/x + 1/2(x-1/x)2 + 1/2(x-1/x)3 + 1/2(x-1/x)4 + --------If x is input through the keyboard, write a program to calculate the sum of first seven terms of this series.

Solution:

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

Page 18: Programming chapter no 3

main() {

float x,nl,x_1,t1;int i;

clrscr();

printf("\n\nEnter the value of X:  ");scanf("%f",&x);

t1 = (x-1)/x;    /* fist term */

for(i=2;i<=7;i++) {

nl = (1.0/2.0)  * pow((x_1),i);   /* terms from 2 to 7 */

}

nl = nl + t1;        /* adding all the terms */

printf("\n\nApproximated natural log = %f\n",nl);

getch();return 0;}