Chapter 3: The Loop Control Structure (Let Us C)

(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.
// Let Us C (Chapter 3 : The Loop Control Structure) : Program-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 i,T,OT=0,Total_OT,OT_Pay;
void main()
{
    clrscr();
    for(i=1;i<=10;i++)
    {
        printf("\nEnter Total hours worked by Employee-%d: ",i);
        scanf("%d",&T);
        if(T>40)
        {
            OT = T-40;
            printf("OT pay for employee %d is Rs.%d/-",i,OT*12);
        }
        else
        {
            OT=0;
            printf("OT pay for employee %d is Rs.%d/-",i,OT*12);
        }
        Total_OT = Total_OT + OT;
    }
    OT_Pay = Total_OT * 12;
    printf("\nTotal Overtime pay for 10 employees = Rs.%d/-",OT_Pay);
}

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

// Let Us C (Chapter 3 : The Loop Control Structure) : Program-2
/* Write a program to find the factorial value of any number entered through the keyboard */
#include<stdio.h>
int Num,temp,Fact=1;
void main()
{
    clrscr();
    printf("Enter any Number: ");
    scanf("%d",&Num);
    temp=Num;
    while(temp>1)
    {
        Fact = Fact * temp;
        temp--;
    }
    printf("\nFactorial of %d is %d",Num,Fact);
}

(c) Two numbers are entered through the keyboard. Write a program to find the value of one number raised to the power of another.
// Let Us C (Chapter 3 : The Loop Control Structure) : Program-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 Num1,Num2,temp1,temp2,mult1=1,mult2=1;
void main()
{
    clrscr();
    printf("Enter 1st Number: ");
    scanf("%d",&Num1);
    printf("Enter 2nd Number: ");
    scanf("%d",&Num2);
//----< 2nd number raised to power of 1st>-----
    temp1=Num1;
    temp2=Num2;
    while(temp1>0)
    {
        mult1=mult1*temp2;
        temp1--;
    }
    printf("\n%d power %d = %d",Num2,Num1,mult1);
//----< 1st number raised to power of 2nd>-----
    temp1=Num1;
    temp2=Num2;
    while(temp2>0)
    {
        mult2=mult2*temp1;
        temp2--;
    }
    printf("\n%d power %d = %d",Num1,Num2,mult2);
}

(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.
// Let Us C (Chapter 3 : The Loop Control Structure) : Program-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>
#include<conio.h>
unsigned int i=0;
void main()
{
    clrscr();
    while(i<=255)
    {
        printf("%d = %c\t",i,i);
        i++;
    }
}

(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)
// Let Us C (Chapter 3 : The Loop Control Structure) : Program-5
/* 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 */
#include<stdio.h>
int n,i,temp,rem,sum;
void main()
{
    clrscr();
    printf("Enter Maximum range: ");
    scanf("%ld",&n);
    for(i=1;i<=n;i++)
    {
        sum=0;
        temp=i;
        while(temp)
        {
            rem=temp%10;
            sum=sum+(rem*rem*rem);
            temp=temp/10;
            if(sum>i)
                break;    // break out of while() loop
        }
        if(sum==i)
        {
            printf("%ld,",i);
        }
    }
}

(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:
1) There are 21 matchsticks.
2) The computer asks the player to pick 1, 2, 3, or 4 matchsticks.
3) After the person picks, the computer does its picking.
4) Whoever is forced to pick up the last matchstick loses the game.
// Let Us C (Chapter 3 : The Loop Control Structure) : Program-6
/* 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: 
1) There are 21 matchsticks.
2) The computer asks the player to pick 1, 2, 3, or 4 matchsticks.
3) After the person picks, the computer does its picking.
4) Whoever is forced to pick up the last matchstick loses the game.*/
#include<stdio.h>
unsigned int n=21,a,b;
void main()
{
    clrscr();
    printf("There are 21 match-sticks. ");
    printf("You may Pick 1 or 2 or 3 or 4 sticks");
    while(n>1)
    {
        printf("\nEnter your choice: ");
        scanf("%d",&a);
        if((a>0)&&(a<5))
        {
            n=n-a;
            printf("%d matchsticks left",n);
        }
        else
        {
            printf("You entered wrong choice");
            break;
        }
        b=5-a;
        n=n-b;
        printf("\nComputer picked %d matchsticks",b);
        printf("\n%d matchsticks left",n);
    }
    printf("\nYou lost the game");
}

(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.
// Let Us C (Chapter 3 : The Loop Control Structure) : Program-7
/* 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 num,i,j,Zero,Positive,Negative;
void main()
{
    clrscr();
    printf("How many Numbers You want to enter: ");
    scanf("%d",&num);
    for(i=1;i<=num;i++)
    {
        printf("\nNumber %d: ",i);
        scanf("%d",&j);
        if(j==0)
            Zero++;
        else if(j<0)
            Negative++;
        else if(j>0)
            Positive++;
    }
    printf("\nZeros = %d, Positive = %d, Negative = %d",Zero,Positive,Negative);
}

(h) Write a program to find the octal equivalent of the entered number.
// Let Us C (Chapter 3 : The Loop Control Structure) : Program-8
/* Write a program to find the octal equivalent of the entered number */
#include<stdio.h>
long int num, sum=0, rem,i=1;
void main()
{
    clrscr();
    printf("Enter a no. to get its Octal: ");
    scanf("%ld",&num);
    while(num)
    {
        rem = num % 8;
        sum = sum + rem*i;
        num = num/8;
        i = i*10;       
    }
    printf("Octal : %ld",sum);
}

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

// Let Us C (Chapter 3 : The Loop Control Structure) : Program-9
/* 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 */
#include<stdio.h>
unsigned int N,i,Num,Min,Max;
void main()
{
    clrscr();
    printf("How many numbers are there in the set? ");
    scanf("%d",&N);
    for(i=1;i<=N;i++)
    {
        printf("Enter Number %d: ",i);
        scanf("%d",&Num);
        if(i==1)
        {
            Min=Num;
            Max=Num;
        }
        else if(Num<Min)
            Min=Num;
        else if(Num>Max)
            Max=Num;
    }
    printf("\nLargest Number = %d",Max);
    printf("\nSmallest Number = %d",Min);
    printf("\nRange = %d",Max-Min);
}
(j) Write a program to print all prime numbers from 1 to 300. (Hint: Use nested loops, break and continue)
// Let Us C (Chapter 3 : The Loop Control Structure) : Program-10
/* Write a program to print all prime numbers from 1 to 300. (Hint: Use nested loops, break */
#include<stdio.h>
#define RANGE 300    // Macro for range
unsigned int i,j,n,count=0;
void main()
{
    clrscr();
    printf("Prime Numbers upto %d are\n",RANGE);
    for(i=2;i<=RANGE;i++)
    {
        for(j=2;j<i;j++)
        {
            if(i%j==0)
            {
                break;
            }
        }
        if(j==i)
        {
            printf("%d,",i);
            count++;
        }
    }
    printf("\nTotal Prime No.s = %d",count);
}

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

value 1.
// Let Us C (Chapter 3 : The Loop Control Structure) : Program-11
/* Write a program to fill the entire screen with a smiling face. The smiling face has an ASCII */
#include<stdio.h>
unsigned int i,j,n=1;
void main()
{
    clrscr();
    for(i=0;i<80;i++)    // No. of characters in each line
    {
        for(j=0;j<22;j++)  // No. of lines per page
            printf("%c",n);   
    }
}

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

1/1! + 2/2! + 3/3! ……
// Let Us C (Chapter 3 : The Loop Control Structure) : Program-12
/* Write a program to add first seven terms of the following series using a for loop */
#include<stdio.h>
#define TERMS 7
unsigned int i;
double Fact=1,Div=0,Sum=0;
void main()
{
    clrscr();
    for(i=1;i<=TERMS;i++)
    {
        Fact=Fact*i;    // Factorial value
        Div=i/Fact;     // Division of Number & its factorial
        Sum=Sum+Div;    // Final sum
    }
    printf("\nSum of 1st %d terms of series = %0.12lf",TERMS,Sum);
}

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

// Let Us C (Chapter 3 : The Loop Control Structure) : Program-13
/* Write a program to generate all combinations of 1, 2 and 3 using for loop */
#include<stdio.h>
unsigned int i,j,k;
void main()
{
    clrscr();
    for(i=1;i<=3;i++)
    {
        for(j=1;j<=3;j++)
        {
            for(k=1;k<=3;k++)
            {
//                if(i==j || j==k || i==k)        // If want to avoid repetition, un-comment these lines
//                    continue;
//                else
                    printf("%d%d%d,",i,j,k);
            }
        }
    }
}

(n) 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.
// Let Us C (Chapter 3 : The Loop Control Structure) : Program-14
/* 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>
unsigned int y;
float i,x;
void main()
{
    clrscr();
    for(y=1;y<=6;y++)
    {
        for(x=5.5;x<=12.5;x+=0.5)
        {
            i=2+(y+0.5*x);
            printf("y=%d x=%f i=%f\n",y,x,i);
        }
    }
}

(o) 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
// Let Us C (Chapter 3 : The Loop Control Structure) : Program-15
/* 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>
void main()
{
    clrscr();
    int Row,a,b,Space;
    for(Row=0;Row<=6;Row++)    // To print desired number of rows
    {
        for(a=65;a<=71-Row;a++)  // 1st half
        {
            printf("%c",a);
        }
        for(Space=1;Space<=Row*2-1;Space++)   // space
            printf(" ");
        for(b=71-Row;b>=65;b--) // 2nd half
        {
            if(b==71);   // To avoid printing G twice
            else
                printf("%c",b);
        }
        printf("\n");    //New line
    }
}

(p) 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.
// Let Us C (Chapter 3 : The Loop Control Structure) : Program-16
/* 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>
unsigned int i,j,n=3,m=4;
void main()
{
    clrscr();
    for(i=0;i<80;i++)
    {
        for(j=0;j<22;j++)
            printf("%c%c",n,m);  
    }
}

(q) 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 ...
// Let Us C (Chapter 3 : The Loop Control Structure) : Program-17
/* 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>
unsigned int Num,i;
void main()
{
    clrscr();
    printf("Enter the No. for which you want multiplication table: ");
    scanf("%d",&Num);
    printf("Multiplication Table for %d is\n",Num);
    for(i=1;i<=10;i++)
    {
        printf("\n%d * %2d  = %5d",Num,i,Num*i);
    }
}

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

                                     1
                               2          3
                       4            5          6
               7             8           9         10

// Let Us C (Chapter 3 : The Loop Control Structure) : Program-18
/* Write a program to produce the following output:
      1
    2   3
  4   5   6
7   8   9   10
*/
#include<stdio.h>
unsigned int n,i,j k,Num=1;
void main()
{
    clrscr();
    printf("How many Lines: ");
    scanf("%d",&n);
    for(i=1;i<=n;i++)    // Rows
    {
        for(j=n;j>i;j--)    // loop for space
        {
            printf("  ");    // double space
        }
        for(k=1;k<=i;k++)    // for printing numbers
        {
            printf("%2d",Num++);
            printf("  ");    // double space
        }
        printf("\n");        // New line
    }
}

(s) Write a program to produce the following output:
                                        1
                                    1      1
                                1      2     1
                            1      3     3     1
                        1      4     6     4     1

// Let Us C (Chapter 3 : The Loop Control Structure) : Program-19
/* Write a program to produce the following output:
        1
      1   1
    1   2   1
  1   3   3   1
1   4   6   4   1
*/
// This is known as Pascal's triangle
// Its Cth term in nth row & kth column is given by
// C(n,k) = n!/(k! * (n-k)!)
// Please check http://en.wikipedia.org/wiki/Pascal%27s_triangle

#include<stdio.h>
unsigned int i,n,row,col,space,num;
unsigned int Fact(unsigned int a)
{
    unsigned int temp=1;
    for(i=a;i>1;i--)
    {
        temp=temp*i;
    }
    return temp;
}
void main()
{
    clrscr();
    printf("Enter No. of Rows: ");
    scanf("%d",&n);
    space=n;
    for(row=0;row<n;row++)
    {
        for(i=1;i<space;i++)
        {
            printf("  ");    // double space
        }
        space--;
        for(col=0;col<=row;col++)
        {
            num=(Fact(row)/(Fact(col)*Fact(row-col)));
            printf("%2d",num);
            printf("  ");    // double space
        }
        printf("\n");
    }
}

(t) 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?
// Let Us C (Chapter 3 : The Loop Control Structure) : Program-20
/* 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?*/
#include <stdio.h>
#include <conio.h>
void main()
{
    int year=0,inv=0,altn=0,Yly_Profit,Machine_Cost;
    clrscr();
    Yly_Profit = 720;  //6000*12/100  i.e. 12% of cost
    Machine_Cost = 6000-2000; // Cost-Salvage_value
    while(altn>(inv-Machine_Cost))
    {
        altn=altn+Yly_Profit;
        inv=inv+1000;
        year++;
    }
    printf("\nMinimum life of the machine should be = %d",year);
    printf("\nwhich will yield total profit of %d-4000=%d",inv,inv-4000);
    printf("\nagainst yield on alternate invenstment = %d",altn);
}

(u) 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
a = p ( 1 + r / q ) nq
Write a program to read 10 sets of p, r, n & q and calculate the corresponding as.
// Let Us C (Chapter 3 : The Loop Control Structure) : Program-21
/* 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 a = p ( 1 + r / q ) nq. Write a program to read 10 sets of p, r, n and q 
and calculate the corresponding as.*/
#include<stdio.h>
#include<math.h>
unsigned int i=1;a = p ( 1 + r / q ) nq
double P,R,N,Q,S,A,M,T;
void main()
{
    clrscr();
    while(i<=10)
    {
        printf("\n------------ SET %d ------------\n",i);
        printf("Enter Principal amount(P): ");
        scanf("%lf",&P);
        printf("Enter Annual ROI(R): ");
        scanf("%lf",&R);
        printf("Enter Total No. of years(N): ");
        scanf("%lf",&N);
        printf("Enter No.of times amount compounded(Q): ");
        scanf("%lf",&Q);
        M = N*Q;
        R = R/100;        // % conversion
        S = 1 + R/Q;
        T = pow(S,M);
        A = P * T;
        printf("Annual amount A = %lf",A);
        i++;
    }
}

(v) 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.
// Let Us C (Chapter 3 : The Loop Control Structure) : Program-22
/* 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. */
#include<stdio.h>
#include<math.h>    // For power function
#define TERMS 7
float Sum=0,x,T;
unsigned int i;
void main()
{
    clrscr();
    printf("Enter value of x: ");
    scanf("%f",&x);
    T = (x-1)/x;       // first term
    for(i=2;i<=TERMS;i++)     // 2nd to 7th terms
    {
        Sum = Sum + 0.5 * pow(T,i);
    }
    Sum = Sum + T;    // Sum of all terms
    printf("Sum of 1st %d terms of Natural log = %f",TERMS,Sum);
}
-----------------------------------------------------------------------------------------------------------------------
END OF CHAPTER 3: The Loop Control Structure
-----------------------------------------------------------------------------------------------------------------------

Comments

Post a Comment

Popular posts from this blog

Chapter 5 : Functions & Pointers (Let us C)

Let Us C (Chapter 8 : Arrays) : Program-9