Program Control Structures #Tutorial 10

  In this article, we are gonna to discuss questions to expert Program Control Structures







Before doing the tutorial 10, watch this videos to get an idea about for , do-while , while , while vs do-while , break & continue statements

Here you can find the tutorial 10 questions pdf to practice Program Control Structures-Repetition and Loops ;

https://www.thecn.com/uploads/646ecb/646ecb5625dbe2382a003471/s3/NjMzNWEyM2JhOGE1NWUxZjk5NWIzNTcxLzY0NmVjYjUzYTc1MzA2MjY1ZDAzMjkyNS9JQ1QxNDAyX1R1dG9yaWFsXzEwLnBkZg/b5fb49b68f2910cf2f16ac47b8d74779.pdf


01. Type in and run the all programs presented in lecture note 10. Briefly describe the theory/ concept you learned from these programs separately.

  • looping structures in C programming language.
  • Practicing the control flow of different looping structures in C programming language.
  • Practicing the variants in the control flow of different looping structures in the C programming language.
  • Applying taught concepts for writing programs.

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

* * 

* * * 

* * * * 

* * * * * 

* * * * * *

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;

}


1 2 

1 2 3 

1 2 3 4 

1 2 3 4 5 

Answer

#include <stdio.h>

int main()

{

int x,y;

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

{

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

                {

printf("%i",y);

}  

printf("\n");

}

return 0;

}


* * * * * * * 

* * * * * * *

* * * * * * * 

* * * * * * * 

* * * * * * *  

Answer

#include <stdio.h>

int main(){

int x,y;

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

{

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

                {

printf("*");

}  

printf("\n");

}

return 0;

}


* * * * * * 

* * * * * 

* * * * 

* * * 

* * 

*

Answer

#include <stdio.h>

int main(){

int x,y;

for (x=6;x>=1;x--)

{

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

               {

printf("*");

}  

printf("\n");

}

return 0;

}

  * * * * * * 

    * * * * * 

      * * * *

         * * * 

            * * 

               *

Answer

#include <stdio.h>

int main() {

    int rows, i, j;

    rows=6;

    for (i = 1; i <= rows; i++) 

   { 

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

       {

            printf("  ");

        }

        for (j = i; j <= rows; j++)

       {

            printf("* ");

        }

        printf("\n");

    }

    return 0;

}


             *

           * *

        * * *

      * * * *

   * * * * *

* * * * * *

 Answer

#include <stdio.h>

int main() {

    int rows = 6; 

    int i, j, k;

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

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

printf(" ");

}

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

            printf("*");

        }

        printf("\n"); 

    }

    return 0;

}

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

Answer

#include <stdio.h>

int main() {

    int numbers[10];

    int minimum, maximum;

    int i;

    printf("Enter 10 numbers:\n");

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

    {

        scanf("%d", &numbers[i]);

    }

    minimum = maximum = numbers[0]; 

    for (i = 1; i < 10; i++) 

    {

        if (numbers[i] < minimum) 

        {

            minimum = numbers[i];

        }

        if (numbers[i] > maximum) 

       {

            maximum = numbers[i];

        }

    }

  printf("Minimum number : %d\nMaximum number : %d\n", minimum, maximum);

  return 0;

}

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

    Answer

#include <stdio.h> int main() { int numbers[10]; int i; int total = 0; float average; printf("Enter 10 numbers:\n"); for (i = 0; i < 10; i++) { printf("Number %d: ", i + 1); scanf("%d", &numbers[i]); total += numbers[i]; } average = (float)total / 10; printf("Total: %d\n", total); printf("Average: %.2f\n", average); 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.

    Answer

#include <stdio.h> int main() { int n; printf(" n n^2\n"); printf("----------\n"); for (n = 1; n <= 10; n++) { printf("%2d %2d\n", n, n * n); } return 0; }

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

Answer

#include <stdio.h>

int main(){

int triangular_number,n,x;

triangular_number;

printf("Enter the number:");

scanf("%i",&n);

printf("\n");

printf("The first %i triangular numbers",n);

printf("\n");

for(x=1;x!=(n+1);x++)

       {

triangular_number=x*(x+1)/2;

printf(" %3i\n",triangular_number);

}

return 0;

}


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

Answer

#include <stdio.h>

int main(){

int n,fact;

fact=1;

printf("Enter the number:");

scanf("%i",&n);

printf("\n");


for (n;n!=0;n=n-1)

fact=fact*n;

printf("%i",fact);

return 0;

}


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

    Answer
#include <stdio.h>
int factorial(int n) {
    int result = 1;
    for (int i = 1; i <= n; ++i) {
        result *= i;
    }
    return result;
}

int main() {
    printf("Factorial Table:\n");
    printf(" n   |  Factorial\n");
    printf("-----|------------\n");
    
    for (int n = 0; n <= 9; ++n) {
        printf("%2d   |  %d\n", n, factorial(n));
    }

    return 0;
}


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


    Answer

#include <stdio.h>

int main() {

    int i,n;

    float sum = 0;


    printf("Input the number of terms: ");

    scanf("%d", &n);

    printf("1/1");


    for (i = 2; i <= n; i++) 

    {

        printf(" + 1/%d", i);

        sum += 1.0 / i;

    }

    printf("\n");

    printf("Sum of Series upto %d terms: %f\n", n, sum + 1);

    return 0;

}

10.Write a program to generate students repot as shown below.

Index Number     Name     Maths     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  

    Answer
#include <stdio.h> int main() { int num_students; printf("Enter the number of students: "); scanf("%d", &num_students); printf("Index Number\tName\tMaths\tPhysics\tChemistry\tTotal\tAverage\tGrade\n"); for (int i = 0; i < num_students; ++i) { int index_number, number, maths, physics, chemistry, total; double average; char name[50], grade; printf("\nStudent %d:\n", i + 1); printf("Index Number: "); scanf("%d", &index_number); printf("Student Number: "); scanf("%d", &number); printf("Name: "); scanf("%s", name); printf("Maths Marks: "); scanf("%d", &maths); printf("Physics Marks: "); scanf("%d", &physics); printf("Chemistry Marks: "); scanf("%d", &chemistry); total = maths + physics + chemistry; average = (double) 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("%d\t%d\t%s\t%d\t%d\t%d\t%d\t%.2f\t%c\n", index_number, number, name, maths, physics, chemistry, total, average, grade); } return 0; }

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

Answer

#include <stdio.h>

int main() {
    int no_of_Houses = 20;
    int unitsPerHouse, i;
    float totalBill = 0.0;

    printf("Enter the number of units consumed by each house:\n");

    for (i = 0; i < no_of_Houses; i++) 
    {
        printf("House %d: ", i + 1);
        scanf("%d", &unitsPerHouse);

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

    totalBill += totalBill * (20.0 / 100.0); 

    printf("Total electricity bill for the village:  %.2f\n", totalBill);

    return 0;
}

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

    Answer
#include <stdio.h>
#define MAX_ITEMS 100

int main() {
    char option;
    int item_code, qty;
    float unit_price, total_price = 0.00;

    // Display the prompt and get the cashier's option
    do {
        printf("\nWelcome to shop name>\n");
        printf("\nPress S to Start\n");
        printf("Press Q to Quit\n");
        printf("Press P to Process Bill\n");
        printf("Your option: ");
        scanf(" %c", &option);

        switch (option) {
            case 'S':
                // Initialize variables for a new bill
                total_price = 0.00;
                printf("\nWelcome! System is ready for processing a bill.\n");
                break;
            case 'Q':
                // Quit the program
                printf("\nShutting down cashier system...\n");
                exit(0);
            case 'P':
                // Process the current customer's bill
                printf("\nEnter item code, quantity, and unit price for each item:\n");
                for (int i = 0; i < MAX_ITEMS; i++) {
                    printf("Item %d: ", i + 1);
                    scanf("%d %d %f", &item_code, &qty, &unit_price);
                    if (item_code == 0) {
                        // End of items
                        break;
                    }
                    total_price += qty * unit_price;
                }

                // Display the bill
                printf("\nTotal price: %.2f\n", total_price);
                break;
            default:
                printf("\nInvalid option. Please try again.\n");
        }
    } while (option != 'Q');

    return 0;
}


BMS Group Members :

·    01)M.G. Janeesha Sinty

Registration number-ICT/21/22/043

CN- JG1539


·    02)R.D. Malsha Nethmini Premathilaka

Registration number- ICT/21/22/085

CN- MN1032


·    03)S. Basuru Rupasinghe

Registration number- ICT/21/22/132

CN- SR1777


Previous Post Next Post

Contact Form