Home > Mobile >  How can i make my program do a specific calculation in a sequential IF statement
How can i make my program do a specific calculation in a sequential IF statement

Time:11-15

`

#include <stdio.h>



int main()
{
   char ticketType;
   int totalBill, studAmount, ticketType_R= 6500, ticketType_G= 7500;

   printf("\nEnter your ticket type: ");
   scanf("%c", &ticketType);
   printf("\nEnter amount of students: ");
   scanf("%d", &studAmount);


   if(ticketType==ticketType_R)
   {
       totalBill==studAmount*6500;
        printf("\nYour ticket type is R and your total bill is: %d", ticketType_R, totalBill);
   }


   if (ticketType==ticketType_G)
   {
       totalBill==studAmount*7500;
       printf("\nYour ticket type is G and your total bill is: %d", ticketType_G, totalBill);
   }



    printf("\nThe amount of students attending are: %d ", studAmount);


    return 0;
}

`

I tried the code that i made above and i was expecting it to

  1. the type of ticket the user chose
  2. print the amount of students attending
  3. calculate the total amount by multiplying the amount of students by the price for the ticket
  4. print the total amount

CodePudding user response:

In your case, I believe you want to calculate value according to ticket type and the number of students.

That's how we can do it:

#include <stdio.h>

int main()
{
   char ticketType;
   int totalBill, studAmount, ticketType_R= 6500, ticketType_G= 7500;

    printf("Enter the ticket type (R/G): "); // Input ticket type
    scanf("%c", &ticketType);

    // Use of Sequential if-else statement to calculate the total bill
    if (ticketType == 'R')
    {
        printf("Enter the number of students: ");
        scanf("%d", &studAmount);
        totalBill = ticketType_R * studAmount;
        printf("Your ticket type is R and your total bill is: %d", totalBill);
    }
    else if (ticketType == 'G')
    {
        printf("Enter the number of students: ");
        scanf("%d", &studAmount);
        totalBill = ticketType_G * studAmount;
        printf("Your ticket type is G and your total bill is: %d", totalBill);
    }
    else
    {
        printf("Invalid ticket type");
    }

    // Total Students 
    printf("\nTotal number of students: %d", studAmount);

    return 0;
}

As you are interested to do it in Python, that's how you will do it.

ticket_type = input("Enter the ticket type (R/G): ")
stud_amount = int(input("Enter the number of students: "))


if ticket_type == "R":
    total_bill = 6500 * stud_amount
    print("Your ticket type is R and your total bill is: ", total_bill)
elif ticket_type == "G":
    total_bill = 7500 * stud_amount
    print("Your ticket type is G and your total bill is: ", total_bill)
else:
    print("Invalid ticket type")

print("Your Total number of students: ", stud_amount)
  • Related