Breaking News

LET US C SOLUTIONS (CHAPTER 3) SOLUTIONS




1.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>
int main()
{
int i,payment,time;
i=1;
while(i<=10)
{
printf("Enter the number of hours you worked\n");
scanf("%d",&time);
payment=(time-41)*12;
printf("Payment=%d\n",payment);
i++;
}
return 0;
}




2. Write a program to find the factorial value of any number
entered through the keyboard.

 



#include<stdio.h>
int main()
{
int i=1,factorial=1,num=5;
printf("Enter any number\n");
scanf("%d",&num);
while(i<=num) /* while loop is good , but for loop is best */
{
factorial=factorial*i;
i++;
}
printf("Factorial Value=%d\n",factorial);
return 0;
}

 


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

int main()
{
int a,b,i=1,ans=1;
printf("Enter any number and it's power\n");
scanf("%d",&a,&b);
while(i<=b)
{
ans=ans*a;
i++;
}
printf("Answer=%d\n",ans);
return 0;
}

 


4. 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>
int main()
{
int ch=1;
while(ch<=255)
{
printf("AscII value %d =%c\n",ch,ch);
ch++;
}
return 0;
} 

 


 

5. 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>
int main()
{
int ch=1;
while(ch<=255)
{
printf("AscII value %d =%c\n",ch,ch);
ch++;
}
return 0;
} 

 



6. 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>
int main()
{
int a,b,c,i,k;
for(i=2;i<=500;i++) 
k=i;
a=k%10;
k=k/10;
b=k%10;
k=k/10;
c=k%10;
if((a*a*a)+(b*b*b)+(c*c*c)==i)
printf("%d ",i);
}
return 0;
}



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


#include<stdio.h>
int main()
{
int n,a,b;
n=21;
while(n>1)
{
printf("Enter numbers of matchsticks you want to pick up\n");
scanf("%d",&a);
n=n-a;
printf("Left matchsticks=%d\n",n);
b=5-a;     /* This is the trick you have to figure out,this will make eventually computer win*/
printf("Computer picks=%d\n",b);
n=n-b;
printf("Left matchsticks=%d\n",n);
}
if(n==1)
printf("You lost,Better luck next time :p\n");
return 0;
}



8.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>
int main()
{
int n,num,p=0,neg=0,z=0,i;
printf("Enter how many numbers you want to enter\n");
scanf("%d",&n);
printf("Enter %d numbers\n",n);
i=1;
while(i<=n)
{
scanf("%d",&num);
if(num>0)
p=p+1;
if(num<0)
neg=neg+1;
if(num==0)
z=z+1;
i++;
}
printf("Postive numbers=%d\nNegative Numbers=%d\nZeros=%d\n",p,neg,z);
return 0;
}



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

 
#include<stdio.h>

int main()
{
int i,j;
for(i=0;i<=300;i++)
{
for(j=0;j<i;j++)
{
if (i%j==0)
break;
}
if(j==i)
printf("%d  ",&i);
}
return 0;
}

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




#include<stdio.h>
int main()
{
char ch=1;
for(i=1;i<=2000;i++)
printf("%c",ch);
return 0;
}





11. Write a program to add first seven terms of the following
series using a for loop:
1/1! + 2/2! + 3/3!……




#include<stdio.h>

int main()
{
int fact=1,i;
float ans=0.0;

for(i=1;i<=7;i++)
{
fact=fact*i;
ans=ans+(i/fact);
}
printf("Answer=%f\n",ans);
return 0;
}





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




#include<stdio.h>

int main()
{ 
int i,j,k;
for(i=1;i<=3;i++)
{
for(j=1;j<=3;j++)
{
for(k=1;k<=3;k++)
printf("%d%d%d ",&i,&j,&k);
}
}

return 0;
}





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






#include<stdio.h>
int main()
{
int x;
float y,i;
for(y=5.5;y<=12.5;y+=0.5)
{
for(x=1;x<=6;x++)
{
i=2+(y+(0.5*x));
printf("i=%f  x=%d y=%f\n",i,y,x);
}
}
return 0;
}




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




nclude<stdio.h>
int main()
{
int ch=3,c=4,i;
for(i=1;i<2000;i++)
printf("%c%c",ch,c);
return 0;
} 






15. 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>
int main()
{
int n,i,ans;
printf("Enter any number\n");
scanf("%d",&n);
for(i=1;i<=10;i++)
{
ans=n*i;
printf("%d*%d=%d\n",n,i,ans);
}
return 0;
}



16. When interest compounds qtimes per year at an annual rate of
r % for nyears, the principle p compounds to an amount a as per
the following formula
a = p ( 1 + r / q ) ^nq
Write a program to read 10 sets of p,r, n& q and calculate
the corresponding as. 




#include<stdio.h>
#include<math.h>
int main()
{
int p,n,i,q;
float a,r;
printf("Enter p,n,r,q\n");
for(i=1;i<=10;i++)
{
scanf("%d%d%f%d",&p,&n,&r,&q);
 a=pow(p*(1+r/q),n*q);
printf("P=%d ||N=%d ||R=%f ||Q=%d|| a=%f\n",p,n,r,q,a);
}
return 0;
}






17 . 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<math.h>

int main()
{

    float x,ans=0,i;
    printf("Enter the value of x\n");
    scanf("%f",&x);
    for(i=1;i<=7;i++)
    {
        ans=ans+(1/i)*pow((x-1)/x,i);
    }
    printf("Answer=%f\n",ans);
    return 0;

}

   



18.  Write a program to produce the following output:

ABCDEFGFEDCBA
ABCDEF   FEDCBA
ABCDE         EDCBA
ABCD              DCBA
ABC                    CBA
AB                         BA
A                              A 






#include <stdio.h>
int main()
{
int i,k,blnks,l,p;

for(p=71;p>=65;p--)
{
for(i=65;i<=p;i++)
{
if(i==71)
printf(" %c ",i);
else
printf("%c ",i);
}
l=1;
blnks=2*(72-p)-1;
while(l<blnks)
{
printf("  ");
l++;
}
for(k=p;k>=65;k--)
{
if(k==71)
printf(" ");
else
printf("%c ",k);

}
printf(" \n");
}
return 0;
}





18.  Write a program to produce the following output:
         1
      2    3
   4    5    6
7    8    9    10 





int main()
{
  int a,b,n=6;
  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-1;
      }
  }
 printf("%d ",a);
}
return ();

}

No comments