In: Computer Science
Branching Program write a program to do the following: A university needs to compute the tuition of its students. They will ask for the name, the number of units the student is taking, and their residency status. R=resident , N = non- resident. The tuition to be computed as follows: non-resident students pay $100 per unit; resident students pay $50 per unit if they take 12 or units or more and $75 per unit if they take less than 12 units Users may enter upper or lower case letters (R/r or N/n) for residency. Error check the user input and output an appropriate message if the input is invalid. If the input is valid, determine the unit price, and calculate the tuition. Output student name, residency, status, number of units, part-time(<12 units) or full-time ( 12 and over units), the price per unit, and the total cost. Do not use loops.
Here is the solution,
Language : C++ & Java
here is the C++ code:-
// your code starts here
#include <iostream>
using namespace std;
int main()
{
// variable declarations
string name;
int units;
char status;
// accepting student name
cout<<"Enter Student Name:";
getline(cin,name);
cin.sync(); // for clearing the input buffer
cout<<"Enter no. of Units:";
cin>>units;
// for validation
if (units <= 0 )
{
cout<<"Sorry invalid input...";
exit(0);
}
cout<<"Enter Student Status:";
cin>>status;
// for validation
if (!(status =='R' || status =='r' || status =='N' || status
=='R'))
{
cout<<"Sorry invalid input...";
exit(0);
}
float unit_charges=0;
if (status == 'N' || status == 'n')
{
// for non resident units * 100
unit_charges = units * 100;
}
else if(status == 'R' || status =='r')
{
if(units >= 12)
{
// if units >=12 then per unit charge 50
unit_charges = units * 50;
}
else if(units < 12)
{
// if units < 12 then per unit charge 75
unit_charges = units * 75;
}
}
// displaying the output
cout<<endl;
// to display name
cout<<"Student Name : " << name<<endl;
// to display status
cout<<"Residence : " <<status<<endl;
// to display units
cout<<"No. of Units : " <<units<<endl;
// to display part time/ full time
if(units < 12)
cout<<"Type : Part-Time"<<endl;
else if(units >= 12)
cout<<"Type : Full-Time"<<endl;
// to display charges per unit
if(status =='N' || status == 'n')
cout<<"Price Per unit:$"<<100<<endl;
else if(status =='R' || status == 'r')
{
if(units >=12)
cout<<"Price Per unit:$"<<50<<endl;
else if(units <12)
cout<<"Price Per unit:$"<< 75<<endl;
}
// to display the total cost
cout<<"Total Cost :$"<<unit_charges;
cout<<endl;
cout<<endl;
return 0;
}
// code ends here
Here is the sample output:-
Here is the screenshot of c++ code:-
// Java code
// your code starts here
import java.util.*;
class BranchingDemo {
public static void main(String[] args) {
String name;
int units;
char status;
Scanner input = new Scanner(System.in);
System.out.println("Branching Demo Program");
// accepting name
System.out.println("Enter student name:");
name = input.nextLine(); // to read name
System.out.println("Enter number of units:");
units = input.nextInt(); // to read units
// check for valid input of unit
if (units <= 0 )
{
System.out.println("Sorry invalid input...");
System.exit(0);
}
System.out.println("Enter Status (R or N) :");
status = input.next().charAt(0); // to read status
// check for valid input of status
if (!(status =='R' || status =='r' || status =='N' || status
=='R'))
{
System.out.println("Sorry invalid input...");
System.exit(0);
}
// calculations
float unit_charges=0;
if (status == 'N' || status == 'n')
{
// for non resident units * 100
unit_charges = units * 100;
}
else if(status == 'R' || status =='r')
{
if(units >= 12)
{
// if units >=12 then per unit charge 50
unit_charges = units * 50;
}
else if(units < 12)
{
// if units < 12 then per unit charge 75
unit_charges = units * 75;
}
}
// displaying the output
System.out.println();
// to display name
System.out.println("Student Name : " + name);
// to display status
System.out.println("Residence : " + status);
// to display units
System.out.println("No. of Units : " + units);
// to display part time/ full time
if(units < 12)
System.out.println("Type : Part-Time");
else if(units >= 12)
System.out.println("Type : Full-Time");
// to display charges per unit
if(status =='N' || status == 'n')
System.out.println("Price Per unit:$"+ 100);
else if(status =='R' || status == 'r')
{
if(units >=12)
System.out.println("Price Per unit:$"+ 50);
else if(units <12)
System.out.println("Price Per unit:$"+ 75);
}
// to display the total cost
System.out.println("Total Cost :$"+ unit_charges);
}
}
// code ends here
Here is the sample output:-
here is the screenshot of the code:-
Thank You.