0% found this document useful (0 votes)
25 views

If-Else Programming

The document contains code for several C# programs: 1) A program that takes two integers as input and checks if they are equal or not. 2) A program that takes a single integer as input and checks if it is even or odd. 3) A program that takes three integers as input and finds the largest number. 4) A program that converts a dollar amount to other currencies like Canadian dollars, Euros, etc. based on exchange rates. 5) Pseudocode for a program to calculate phone bills for regular and premium plans based on usage and rates.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

If-Else Programming

The document contains code for several C# programs: 1) A program that takes two integers as input and checks if they are equal or not. 2) A program that takes a single integer as input and checks if it is even or odd. 3) A program that takes three integers as input and finds the largest number. 4) A program that converts a dollar amount to other currencies like Canadian dollars, Euros, etc. based on exchange rates. 5) Pseudocode for a program to calculate phone bills for regular and premium plans based on usage and rates.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

//Write a C# Sharp program to accept two integers

//and check whether they are equal or not

int num1, num2;

Console.Write("Enter first number: ");


num1 = Convert.ToInt16(Console.ReadLine());
Console.WriteLine("Enter second number: ");
num2 = Convert.ToInt16(Console.ReadLine());

if (num1 == num2)
{
Console.WriteLine("The two numbers are equal.");
}
else
{
Console.WriteLine("The numbers are not equal");
}

// Write a C# Sharp program to check whether a given


// number is even or odd

int num1;

Console.Write("Enter number: ");


num1 = Convert.ToInt16(Console.ReadLine());

if (num1 % 2 == 0)
{
Console.WriteLine("The number is even");
}
else
{
Console.WriteLine("The number is odd");

//Write a C# Sharp program to find the largest of three numbers.


int num1, num2, num3;
int largest = -1;

Console.Write("Enter first number: ");


num1 = Convert.ToInt16(Console.ReadLine());
Console.Write("Enter second number: ");
num2 = Convert.ToInt16(Console.ReadLine());
Console.Write("Enter third number: ");
num3 = Convert.ToInt16(Console.ReadLine());

if (num1 >= num2 && num1 >= num3)


{
largest = num1;
}
else if (num2 >= num1 && num2 >= num3)
{
largest = num2;
}
else if (num3 >= num1 && num3 >= num2)
{
largest = num3;
}

Console.WriteLine("Largest number is {0}", largest);

// Create a program that converts U.S dollars to a different currency.


//The input value should always be
// greater than zero.The exchange rates are listed below.
//The user should be allowed to choose the
// currency to convert to. Hint: Designate a code for each currency
//and display a menu that lists each code
// and its corresponding currency.Round output to 2 decimal places.

//Canadian Dollar = 0.9813


//Euro = 0.757
//Indian Rupee = 52.53
//PHP = 49.80

double dollars, convertedValue = -1;


string choice;
bool isInputValid = true;

Console.WriteLine("Thank you for using this program. This will convert any value
in dollars to");
Console.WriteLine("Canadian Dollars, Euro, Indian Rupee and Philippine Peso");
Console.WriteLine("-----------------------------------");
Console.Write("Enter the amount in dollars: $");
dollars = Convert.ToDouble(Console.ReadLine());
Console.WriteLine("-----------------------------------");

Console.WriteLine();
Console.WriteLine("-----------------------------------");
Console.WriteLine("CD - for Canadian Dollars ");
Console.WriteLine("E - for Euro");
Console.WriteLine("IR - for Indian Rupee");
Console.WriteLine("PHP - for Philippine Peso ");
Console.WriteLine("-----------------------------------");
Console.Write("Please enter the desired code for currency you want to convert to:
");
choice = Console.ReadLine();
Console.WriteLine();
Console.WriteLine();
Console.WriteLine();

switch (choice)
{
case "CD":
convertedValue = dollars * 0.9813;
break;
case "E":
convertedValue = dollars * 0.757;
break;
case "IR":
convertedValue = dollars * 52.53;
break;
case "PHP":
convertedValue = dollars * 49.80;
break;
default:
Console.WriteLine("Invalid input.");
isInputValid = false;
break;
}

if (isInputValid)
{
Console.WriteLine("-----------------------------------");
Console.WriteLine("${0} = {1} in {2}", dollars, convertedValue, choice);
Console.WriteLine("-----------------------------------");

/*
Write a program that calculates and prints the bill for a cellular telephone company. The company
offers two types of service: regular and premium.
Its rates vary depending on what type of service: regular(code R) and premium (code P).

The rates are computed as follows:


Regular service:
$10.00 (base payment) plus first 50 minutes are free.
Charges for over 50 minutes are $0.2 per minute.
Premium service:$25.00 plus
a)If calls are made during the day, rate is $0.1 per minute
b)If calls are made during night, rate is $0.05 per minute
The program should ask the type of service by entering either r or R for regular and p or P for
premium.
or regular users, the program should ask for the total number of minutes.
For premium, the program should ask for the number of minutes used in the day and in the night
respectively. */

//FOR THE SKELETON OF THE PROGRAM, HERE IS THE CODE. THIS HAS NO COMPUTATIONS YET. JUST THE OVERALL
//ORDER OF OPERATIONS.

string serviceType;
double regularRate, regularFreeMinutes, premiumBaseDollarValue, premiumDayRate, premiumNightRate,
totalBill;

//set the values based on the problem


regularRate = 0.2;
regularFreeMinutes = 50;
premiumBaseDollarValue = 25;
premiumDayRate = 0.1;
premiumNightRate = 0.05;
totalBill = 0;

Console.WriteLine("What service type did you avail?");


Console.WriteLine("Key in [p] or [P] for Premium");
Console.WriteLine("Key in [r] or [R] for Regular");
Console.Write("Enter Service Type:");
serviceType = Console.ReadLine();

//check if the user placed the correct versions


if( serviceType == "P" || serviceType == "p" || serviceType == "R" || serviceType == "r" )
{
//successful serviceType was keyed in.
if( serviceType == "R" || serviceType == "r" )
{
//regular services code goes here

}
else
{
//premium services code goes here

}
//After calculation, print output here.
Console.WriteLine( "Thank you for using our services." );
Console.WriteLine( "Your total bill is $" + totalBill );

}
else
{
//wrong serviceType was keyed in.
Console.WriteLine("Service type is invalid.");
}
//END OF PROGRAM

//THIS IS THE COMPLETE SOLUTION. NOTICE THE NEW VARIABLES ADDED AND ARE USED FOR THE SOLUTION.
//NOTICE AS WELL HOW I NAME MY VARUABLES.

string serviceType;
double regularBasePayment, regularRate, regularFreeMinutes, premiumBasePayment, premiumDayRate,
premiumNightRate, totalBill, minutesToCharge;

//temporary string variables, these strings will hold the input values from the user through
Console.ReadLine
string regularMinuteString, premiumDayMinuteString, premiumNightMinuteString;
//number values to store the converted values of the temporary strings
double regularMinute, premiumDayMinute, premiumNightMinute;

//set the values based on the problem


regularBasePayment = 10;
regularRate = 0.2;
regularFreeMinutes = 50;
premiumBasePayment = 25;
premiumDayRate = 0.1;
premiumNightRate = 0.05;
totalBill = 0;

Console.WriteLine("What service type did you avail?");


Console.WriteLine("Key in [p] or [P] for Premium");
Console.WriteLine("Key in [r] or [R] for Regular");
Console.Write("Enter Service Type:");
serviceType = Console.ReadLine();

//check if the user placed the correct versions


if( serviceType == "P" || serviceType == "p" || serviceType == "R" || serviceType == "r" )
{
//successful serviceType was keyed in.
if( serviceType == "r" || serviceType == "R" )
{
//regular services code goes here
Console.Write("Enter total number of minutes used for calling: ");
regularMinuteString = Console.ReadLine();
regularMinute = Convert.ToDouble( regularMinuteString );

//calculate minutes to charge from the user aside from the Free 50 minutes;
minutesToCharge = regularMinute - regularFreeMinutes;

//in case the user used less than 50 minutes,


//nothing should be charged so we reset the value of minutesToCharge to zero
if( regularMinute <= regularFreeMinutes )
{
minutesToCharge = 0;
}
//calculate total bill
totalBill = regularBasePayment + ( minutesToCharge * regularRate );
}
else
{
//premium services code goes here
Console.Write("Enter total number of minutes used for calling during the DAY: ");
premiumDayMinuteString = Console.ReadLine();
premiumDayMinute = Convert.ToDouble(premiumDayMinuteString);

Console.Write("Enter total number of minutes used for calling during the NIGHT: ");
premiumNightMinuteString = Console.ReadLine();
premiumNightMinute = Convert.ToDouble(premiumNightMinuteString);

//Now that we have the night and day minutes, its time to calculate the total bill
totalBill = premiumBasePayment + (premiumDayMinute * premiumDayRate) +
(premiumNightMinute * premiumNightRate);
}
//After calculation, print output here.
Console.WriteLine( "Thank you for using our services." );
Console.WriteLine( "Your total bill is $" + totalBill );

}
else
{
//wrong serviceType was keyed in.
Console.WriteLine("Service type is invalid.");
}

Console.ReadLine();

You might also like