Program Control Structures – Repetition and Loops(Tutorial)

 Tutorial 10

01.



(I)

#include <stdio.h>
int main()
{
int i,j;
for(i=1;i<7;i++)
{
for(j=1;j<=i;j++)
{
printf("*");
}
printf("\n");
}
return 0;
}

(II)
#include<stdio.h>  
int main()
{  
int i,j;
for (i = 1; i < 7; i++) 
{
        printf("* * * * * * *\n");
    }
printf("\n");
return 0;
}

(III)

#include<stdio.h>
int main()
{
int i,j;
for(i=6;i>0;i--)
{
for(j=1;j<=i;j++)
{
printf("*");
}
printf("\n");
}
printf("\n");
return 0;
}

(IV)
#include<stdio.h>
int main()
{
int i,j;
for(i=7;i>0;i--)
{
for(j=1;j<7;j++)
{
if(i<=j)
printf("*");
else
printf("  ");
}
printf("\n");
}
return 0;
}

(V)
#include<stdio.h>
int main()
{
int i,j;
for(i=6;i>0;i--)
{
for(j=1;j<7;j++)
{
if(i>=j)
printf("*");
else
printf("  ");
}
printf("\n");
}
return 0;
}

(VI)
#include<stdio.h>
int main()
{
for(i=1;i<6;i++)
{
for(j=1;j<=i;j++)
{
printf("%i ",j);
}
printf("\n");
}
printf("\n");           
return 0;
}


02. Find the minimum and maximum of sequence of 10 numbers.



03.  Find the total and average of a sequence of 10 numbers.


04.  Write a program to generate and display a table of n and n2, for integer values of n
ranging from 1 to 10. Be certain to print appropriate column headings.

    

05. A triangular number can also be generated by the formula
triangularNumber = n (n + 1) / 2



06. The factorial of an integer n, written n!, is the product of the consecutive integers 1
through n. For example, 5 factorial is calculated as
5! = 5 x 4 x 3 x 2 x 1 = 120


07.  Write a program to generate and print a table of the first 10 factorials.


08.  Display the n terms of harmonic series and their sum.

1 + 1/2 + 1/3 + 1/4 + 1/5 ... 1/n terms
Test Data :
Input the number of terms : 5
Expected Output :
1/1 + 1/2 + 1/3 + 1/4 + 1/5 +
Sum of Series upto 5 terms : 2.283334


09. 

10.Assume a village has 20 houses. Input electricity unit charges and calculate total
electricity bill according to the following criteria:
  •  For first 50 units Rs. 0.50/unit
  •  For next 100 units Rs. 0.75/unit
  •  For next 100 units Rs. 1.20/unit
  •  For unit above 250 Rs. 1.50/unit
  • An additional surcharge of 20% is added to the bill
The output should be as follows:
Serial Number    House Address     Units     Surcharge     Amount to be paid

#include <stdio.h>

int main() 
{
    int numHouses = 20;
    float unitCharges, totalBill, surcharge, amountToBePaid;
    int units;

    printf("Enter electricity unit charges: ");
    scanf("%f", &unitCharges);

    printf("Serial Number\tHouse Address\tUnits\tSurcharge\tAmount to be paid\n");
    for (int i = 1; i <= numHouses; i++)
    {
        printf("%d\t", i);
        printf("House %d\t", i);

        printf("Enter units consumed for House %d: ", i);
        scanf("%d", &units);

        if (units <= 50) {
            totalBill = units * 0.50;
        } else if (units <= 150) {
            totalBill = 50 * 0.50 + (units - 50) * 0.75;
        } else if (units <= 250) {
            totalBill = 50 * 0.50 + 100 * 0.75 + (units - 150) * 1.20;
        } else {
            totalBill = 50 * 0.50 + 100 * 0.75 + 100 * 1.20 + (units - 250) * 1.50;
        }

        surcharge = 0.20 * totalBill;
        amountToBePaid = totalBill + surcharge;

        printf("%d\t%.2f\t%.2f\n", units, surcharge, amountToBePaid);
    }

    return 0;
}

10. 

Click Here to Get The Code!





Post a Comment

Previous Post Next Post