Program Control Structures #Tutorial 09

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



Before doing the tutorial 09, watch this videos to get an idea about Nested-if , else if ladder . switch statement 

Here you can find the tutorial 09 questions pdf to practice Program Control Structures-Desicion making and branching  ;

https://www.thecn.com/uploads/646c14/646c145e5f21dccd3a0622e4/s3/NjMzNWEyM2JhOGE1NWUxZjk5NWIzNTcxLzY0NmMxNDU5YWE5MzAwZGZjMTBlNzVkYS9JQ1QxNDAyX1R1dG9yaWFsXzA5LnBkZg/fa4128edc9912e0863031318dabc31ff.pdf

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

  • Defining the operation of if, if-else, nested if-else, switch, and conditional operators.
  • Justifying the control flow of the program under the aforementioned C language constructs.
  • Applying taught concepts for writing programs.
2. Swap two values stored in two different variables.

Answer

#include<stdio.h>
int main()

{
      int a,b,c;

      printf("Enter value of a :");
      scanf("%i",&a);

      printf("Enter value of b :");
      scanf("%i",&b);

      c = a;
      a = b;
      b = c;

     printf("value of a = %i\n",a);
     printf("value of b = %i\n",b);

     return 0;

}

03.Check whether an entered number is negative, positive or zero

Answer

#include <stdio.h>
int main()
{
int x;
printf("Enter the number:");
scanf("%i",&x);

printf("\n");
if (x>0)
printf("%i is a positive number",x);
else if (x==0)
printf("number is zero");
else
printf("%i is a negative number",x);

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

4.    Check whether an entered year is leap year or not.

Answer

#include<stdio.h>
int main()
{
int year;
printf("Enter the year:");
scanf("%i",&year);
printf("\n");

if ((year%4)==0)
printf("%i is a leap year",year);
else 
printf("%i is not a leap year",year);

return 0;
}

5. Write a program that asks the user to type in two integer values at the terminal. Test these two numbers to determine if the first is evenly divisible by the second, and then display an appropriate message at the terminal.

Answer

#include<stdio.h>
int main ()
    {
     int number1,number2;

     printf("Enter Number 1->");
     scanf("%i",&number1);

     printf("Enter Number 2->");
     scanf("%i",&number2);

     if (number1 % number2 == 0)
        printf("The first number is evenly divisible by the second number");
     else
        printf("The first number is not evenly divisible by the second number");

  return 0;
}

6.Write a program that accepts two integer values typed in by the user. Display the result of dividing the first integer by the second, to three-decimal-place accuracy. Remember to have the program check for division by zero.  

    Answer

#include <stdio.h>
int main() 
{
      int num1, num2;
      float result;
      printf("Enter two integer numbers: ");
      scanf("%d %d", &num1, &num2);
      if (num2 == 0) 
      {
           printf("Error: Division by zero is not allowed.\n");
      } 
      else 
     {
         result = (float)num1 / num2;
         printf("%d divided by %d is %.3f.\n", num1, num2, result);
    }
    return 0;
}

7. Write a program that takes an integer keyed in from the terminal and extracts and displays each digit of the integer in English. So, if the user types in 932, the program should display nine three two. Remember to display “zero” if the user types in just a 0.

Answer

#include <stdio.h>

int main() {
    int num,x; 
    printf("Enter an integer: ");
    scanf("%d", &num);
    if (num == 0) 
        printf("zero\n");
       
    
x=0;
    while (num > 0) {
        int digit = num % 10;
        x = x * 10 + digit;
        num /= 10;
    }

    printf("In English: ");

    
    while (x > 0) {
        int digit = x % 10;

        switch (digit) {
            case 0: 
printf("zero "); 
break;
            case 1: 
printf("one "); 
break;
            case 2: 
printf("two "); 
break;
            case 3: 
printf("three "); 
break;
            case 4: 
printf("four "); 
break;
            case 5: 
printf("five "); 
break;
            case 6: 
printf("six "); 
break;
            case 7: 
printf("seven "); 
break;
            case 8: 
printf("eight "); 
break;
            case 9: 
printf("nine "); 
break;
default:
printf("invalid "); 
break;
        }

        
        x /= 10;
    }

    printf("\n");

    return 0;
}

8. Input marks of five subjects Physics, Chemistry, Biology, Mathematics and Computer. Calculate percentage and grade according to following: 
        a. Percentage >= 90% : Grade A 
        b. Percentage >= 80% : Grade B 
        c. Percentage >= 70% : Grade C 
        d. Percentage >= 60% : Grade D 
        e. Percentage >= 40% : Grade E 
        f. Percentage < 40% : Grade F

Answer

#include<stdio.h>
int main()
{
float phy,che,bio,math,com;
float percentage;
percentage=0;

printf("Enter the physics marks:");
scanf("%f",&phy);

printf("Enter the chemistry marks:");
scanf("%f",&che);

printf("Enter the biology marks:");
scanf("%f",&bio);

printf("Enter the maths marks:");
scanf("%f",&math);

printf("Enter the computer marks:");
scanf("%f",&com);

percentage=(phy+che+bio+math+com)/5;
printf("The percentage is %.2f\n",percentage);

if (percentage>=90)
printf("The grade is A");

else if (percentage>=80)
printf("The grade is B");

else if (percentage>=70)
printf("The grade is C");

else if (percentage>=60)
printf("The grade is D");

else if (percentage >=40)
printf("The grade is E");

else
printf("The grade is F");  

return 0;
}
9.  Input basic salary of an employee and calculate its Gross salary according to following: (note: HRA and DA are allowances) 
            a. Basic Salary <= 10000 : HRA = 20%, DA = 80% 
            b. Basic Salary <= 20000 : HRA = 25%, DA = 90% 
            c. Basic Salary > 20000 : HRA = 30%, DA = 95%

Answer

#include <stdio.h>
int main() {
    int b_salary; 
    float total_salary;

    printf("Enter the Basic salary: ");
    scanf("%i", &b_salary);

    if (b_salary <= 10000) 
        {
            total_salary = b_salary + (20.0 / 100) * b_salary + (80.0 / 100) * b_salary;
            printf("Total salary = %.2f", total_salary);
       } 
        else if (b_salary <= 20000) 
       {
            total_salary = b_salary + (25.0 / 100) * b_salary + (90.0 / 100) * b_salary;
            printf("Total salary = %.2f", total_salary);
       }
        else 
       {
            total_salary = b_salary + (30.0 / 100) * b_salary + (95.0 / 100) * b_salary;
            printf("Total salary = %.2f", total_salary);
      }

    return 0;
}

10.Write a program that acts as a simple “printing” calculator. The program should allow the user to type in expressions of the form number operator: The following operators should be recognized by the program: + - * / S E 
The S operator tells the program to set the “accumulator” to the typed-in number. 
The E operator tells the program that execution is to end. 
The arithmetic operations are performed on the contents of the accumulator with the number that was keyed in acting as the second operand. The following is a “sample run” showing how the program should operate: 

            Begin Calculations 
            10 S Set Accumulator to 10 
            = 10.000000 Contents of Accumulator 
            2 / Divide by 2 
            = 5.000000 Contents of Accumulator 
            55 - Subtract 55 
            -50.000000 
            100.25 S Set Accumulator to 100.25 
            = 100.250000 
            4 * Multiply by 4 
            = 401.000000 
            0 E End of program 
            = 401.000000 
            End of Calculations. 

Make certain that the program detects division by zero and also checks for unknown operators. 

    Answer

#include<stdio.h>

int main(){

float number, value;

char operator;

printf("Type in your expression:\n ");

scanf("%f %c", &number,&operator);

switch(operator){

case 'S':

printf("%.6f Contents of Accumulator\n", number);

break;

case 'E':

printf("%.6f End\n", number);

break;

case '+':

scanf("%f", &value);

printf("%.6f\n", number+value);

break;

case '-':

scanf("%f", &value);

printf("%.6f\n", number-value);

break;

case '*':

scanf("%f", &value);

printf("%.6f\n", number*value);

break;

case '/':

if(number==0)

printf("Division by zero\n");

else

scanf("%f", &value);

printf("\n%.6f Contents of Accumulator\n", number/value);

break;

default:

printf("Unkown Operator\n");

break;

}

return 0;

}


11. Input electricity unit charges and calculate total electricity bill according to the given condition:
             a. For first 50 units Rs. 0.50/unit 
             b. For next 100 units Rs. 0.75/unit
             c. For next 100 units Rs. 1.20/unit
             d. For unit above 250 Rs. 1.50/unit 
             e. An additional surcharge of 20% is added to the bill

Answer

#include<stdio.h>
int main()
{
float units,e_bill,total_e_bill;
e_bill=0;
total_e_bill=0;
printf("Enter the units:");
scanf("%f",&units);
if (units<=50){
e_bill=units*0.50;}
else if (50<units && units<=150){
e_bill=(50*0.50)+(units-50)*0.75;}
else if (151<units && units<=250){
e_bill=(50*0.50)+(100*0.75)+(units-150)*1.20;}
else {
e_bill=(50*0.50)+(100*0.75)+(100*1.20)+(units-250)*1.50;
}
total_e_bill=e_bill+(e_bill*20/100);
printf("Total electricity bill is  %f",total_e_bill);

return 0;
}

12.An envelope manufacturing company hires people to make envelopes. They provide all the raw material needed and pay at the following rates. Write a program to input the no of envelopes made and to calculate and print the amount due 

            Envelopes                 Rate 
            1-1000                       75 cents 
            1001-1500                 1 rupee 
            1501-2000                 1 rupee and 15 cents 
            2001-                         1 rupee and 25 cents 

Answer

            #include <stdio.h>
            int main() 
            {
                int num_envelopes;
                float amount_due = 0;

                printf("Enter the number of envelopes made: ");
                scanf("%d", &num_envelopes);

                if (num_envelopes <= 1000) 
                {
                    amount_due = num_envelopes * 0.75;
                } 
                else if (num_envelopes <= 1500) 
                {
                    amount_due = 1000 * 0.75 + (num_envelopes - 1000) * 1.0;
                } 
                else if (num_envelopes <= 2000) 
                {
                    amount_due = 1000 * 0.75 + 500 * 1.0 + (num_envelopes - 1500) * 1.15;
                } 
                else 
                {
                    amount_due = 1000 * 0.75 + 500 * 1.0 + 500 * 1.15 + (num_envelopes - 2000) * 1.25;
               }
                printf("Amount due: %.2f\n", amount_due);
                return 0;
            }


13.Find the number of separate Notes and coins required to represent a given monetary value. E,g, 2700 required 1 ➡ 2000 note, 1 500 note and 2100 notes.

    Answer

#include <stdio.h>
int main(){
int money;
int note5000=0, note2000=0, note1000=0, note500=0, note100=0, note50=0, note20=0;
printf("Enter monetary value: ");
scanf("%i", &money);
note5000=money/5000;
money%=5000; 
note2000=money/2000;
money%=2000;
note1000=money/1000;
money%=1000;
note500=money/500;
money%=500;
note100=money/100;
money%=100;
note50=money/50;
money%=50;
note20=money/20;
money%=20;
printf("Number of 5000 notes: %i\n", note5000);
printf("Number of 2000 notes: %i\n", note2000);
printf("Number of 1000 notes: %i\n", note1000);
printf("Number of 500 notes: %i\n", note500);
printf("Number of 100 notes: %i\n", note100);
printf("Number of 50 notes: %i\n", note50);
printf("Number of 20 notes: %i", note20);
return 0;
}

14.Display Age, Birthday, and Gender using a given National Identity Card number. 

    Answer
#include <stdio.h>
int main(){
int age=24; 
int birthday=12;
char birth_month[]="January";
int National_ID_Number, ID=2000012208;
char gender[]="Male";
printf("Enter National ID Card Number: ");
scanf("%i", &National_ID_Number);
if(National_ID_Number==ID){
printf("Age : ");
printf("%i\n", age);
printf("Birthday : ");
printf("%s %i\n", birth_month,birthday);
printf("Gender : ");
printf("%s", gender);
}else
printf("Input Valid ID Number...");
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