Tutorial 10 - Program Control Structures – Repetition and Loops

1. Print the following shapes using loop construct of C programming.

i) *

    * *

    * * *

    * * * *

    * * * * *

    * * * * * *

    Answer :

#include <stdio.h>

int main ()

{

               int x, y;

               for (x=1; x<=6; x++)

               {

                              for (y=1; y<=x; y++)

                              {

                                             printf("*");

                              }

                              printf("\n");

               }

               return 0;

}

ii) * * * * * * *

    * * * * * * *

    * * * * * * *

    * * * * * * *

    * * * * * * *

    Answer :

#include <stdio.h>

int main ()

{

               int x, y;

               for (x=1; x<=6; x++)

               {

                              for (y=1; y<=6; y++)

                              {

                                             printf("*");

                              }

                              printf("\n");

               }

               return 0;

}.

iii.    * * * * * *
        * * * * *
        * * * *
        * * *
        * *
        *

#include <stdio.h>

int main() {
    int i, c, n;
    for (i = 1; i <= 6; i++) {
        for (c = 6; c >= i; c--) {
            printf("*");
        }
        for (n = 1; n < i; n++) {
            printf(" ");
        }
        printf("\n"); // Add a newline after each row
    }
    return 0;
}

iv.    *
        * *
        * * *
        * * * *
        * * * * *
        * * * * * *

#include<stdio.h>

int main()
{
int c,i,n;
for(c=1;c<=6;c++)
{
for(i=5;i>=c;i--)
{
printf(" ");
}
for(n=1;n<=c;n++)
{
printf("*");
}
printf("\n");
}
return 0;
}

v.     * * * * * *
        * * * * *
        * * * *
        * * *
        * *
        *

#include<stdio.h>

int main()
{
int c,i,n;
for(c=1;c<=6;c++)
{
for(i=5;i>=c;i--)
{
printf(" ");
}
for(n=1;n<=c;n++)
{
printf("*");
}
printf("\n");
}
return 0;
}

vi,.        1
            1 2
            1 2 3
            1 2 3 4
            1 2 3 4 5

#include<stdio.h>

int main()
{
int c,i;
for(c=1;c<6;c++)
{
for(i=1;i<=c;i++)
{
printf("%i",i);
}
printf("\n");
}
return 0;
}

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

#include<stdio.h>

int main()
{
int num,max,min,c;
printf("input your number= ");
scanf("%i",&num);
min=num;
max=num;
for(c=1;c<=10;c++)
{
printf("input your number;");
scanf("%i",&num);
if (max<num){
max=num;
}
else 
if(min>num){
min=num;
}
}
printf("max= %i min=%i",max,min);
return 0;
}

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

#include<stdio.h>

int main()
{
int i;
float ave,tot,num;
for(i=1;i<=10;i++)
{
printf("Enter your number= ");
scanf("%f",&num);
tot+=num;
}
ave=tot/10;
printf("Total= %.2f Avarage= %.2f",tot,ave);
return 0;
}

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

#include<stdio.h>

int main()
{
int i;
printf("n\tn^2\n");
for(i=1;i<=10;i++)
{
printf("%i\t%i",i,i*i);
printf("\n");
}
return 0;
}


5. A triangular number can also be generated by the formula
triangularNumber = n (n + 1) / 2
for any integer value of n. For example, the 10th triangular number, 55, can be generated
by substituting 10 as the value for n in the preceding formula. Write a program that
generates a table of triangular numbers using the preceding formula.

#include<stdio.h>

int main()
{
int triangularNum,n,i;
printf("Enter number= ");
scanf("%i",&n);
for(i=1;i<=n;i++)
{
triangularNum=i*(i+1)/2;
printf("%i\t%i\n",i,triangularNum);
}
return 0;
}

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

#include<stdio.h>

int main()
{
int c,num,value;
value=1;
printf("Enter number= ");
scanf("%i",&num);
for(c=1;c<=num;c++)
{
value=value*c;
}
printf("Factorial number= %i",value);
return 0;
}

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

#include<stdio.h>

int main()
{
int i,facValue=1;
for(i=1;i<=10;i++)
{
facValue*=i;
printf("%i\t%i\n",i,facValue);
}
return 0;
}

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

#include<stdio.h>
int main()
{
int i, num;
float sum = 0 ;
printf("Enter your number= ");
scanf("%i", &num);
for (i = 1;i <= num;i++)
{
sum = sum + (1.0 / i);
printf("1/%i+", i);

}
printf("\nSum of Series upto %i terms :%f", num, sum);
return 0;
}


9. Write a program to generate students repot as shown below.
Index Number Name Mathes Physics Chemistry Total Average Grade
In this program, for each student you have to input marks of three subjects. Students’
grade is determined according to following criteria:
a. Average >= 90% : Grade A
b. Average >= 80% : Grade B
c. Average >= 70% : Grade C
d. Average >= 60% : Grade D
e. Average >= 40% : Grade E
f. Average < 40% : Grade F

#include <stdio.h>
int main()
{
int numStudents, i;
int indexNumber, maths, physics, chemistry, total;
float average;
char grade;
char name[50];
printf("Enter the number of students: ");
scanf("%d", &numStudents);
for (i = 0; i < numStudents; i++)
{
printf("Enter details for student %d:\n", i + 1);
printf("Index Number: ");
scanf("%d", &indexNumber);
printf("Name: ");
scanf("%s", name);
printf("Maths: ");
scanf("%d", &maths);
printf("Physics: ");
scanf("%d", &physics);
printf("Chemistry: ");
scanf("%d", &chemistry);
total = maths + physics + chemistry;
average = total / 3.0;
if (average >= 90)
{
grade = 'A';
}
else if (average >= 80)
{
grade = 'B';
}
else if (average >= 70)
{
grade = 'C';
}
else if (average >= 60)
{
grade = 'D';
}
else if (average >= 40)
{
grade = 'E';
}
else
{
grade = 'F';
}
printf("Index Number\tName\tMaths\tPhysics\tChemistry\tTotal\tAverage\tGrade\n");
printf("%d\t%s\t%d\t%d\t%d\t%d\t%.2f\t%c\n", indexNumber, name, maths, physics, chemistry, total, average, grade);
}
return 0;
}

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

#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;
}

11.Develop a program to a cashier system for a shop.
Your system should display following prompt for the cashier and the entire program is
controlled according to the command given by the cashier.
Once you press S: System will display welcome message and ready for processing a bill
by initializing required variables.
Once you press Q: You can shut down the cashier system.
Once you press P: Process the current customer’s shopping cart. Here, you can enter the
item code, qty, unit price of the items in shopping cart and calculate and display the total
Once you finished serving to the current customer, you can press N to move to the
prompt.
    Welcome to <shop name>
    Press S  Start
    Press Q  Quit
    Press P  Process Bill
    Your option : _

#include <stdio.h>

int main() {
    char option;
    char shopName[100];
    int itemCode, qty;
    float unitPrice, total = 0.0;

    printf("Enter the name of the shop: ");
    scanf("%s", shopName);

    printf("Welcome to %s\n", shopName);
    printf("Press S -> Start\n");
    printf("Press Q -> Quit\n");
    printf("Press P -> Process Bill\n");

    while (1) {
        printf("Your option: ");
        scanf(" %c", &option);

        switch(option) {
            case 'S':
            case 's':
                printf("Welcome message displayed. Ready for processing a bill.\n");
                total = 0.0;
                break;
            case 'Q':
            case 'q':
                printf("Shutting down the cashier system.\n");
                return 0;
            case 'P':
            case 'p':
                printf("Enter item code, quantity, and unit price of the items in the shopping cart:\n");
                printf("Item code: ");
                scanf("%d", &itemCode);
                printf("Quantity: ");
                scanf("%d", &qty);
                printf("Unit price: ");
                scanf("%f", &unitPrice);

                total += qty * unitPrice;

                printf("Total: %.2f\n", total);
                break;
            default:
                printf("Invalid option. Please try again.\n");
        }
    }

    return 0;
}




Post a Comment

Previous Post Next Post