Write a C# program. Declare and initialize the variable number to 20. Provide a
ID: 3575183 • Letter: W
Question
Write a C# program.
Declare and initialize the variable number to 20. Provide a menu with 7 items to allow the user to:
Add to the value of number a value that the user enters.
Subtract from the value of number a value that the user enters
Multiply the value of number by a value that the user enters
Divide the value of number by a value that the user enters
Display the value of number
Set the value of number to a new value
Exit.
Your program should use a switch structure to perform all the operations in the menu. Your program should not terminate unless option 7 is selected by the user.
Explanation / Answer
using System;
public class A
{
public static void Main()
{
int n1=20,n2,ch;
Console.Write(" ");
Console.Write("program: ");
Console.Write("Enter the second Integer :");
n2 = Convert.ToInt32(Console.ReadLine());
Console.Write(" menu : ");
Console.Write("1-Add 2-Subtract. 3-Multiply. 4-Divide. 5-display 6-Exit. ");
Console.Write(" enter the input you want :");
ch = Convert.ToInt32(Console.ReadLine());
switch(ch) {
case 1:
Console.Write(" Add {0} and {1} is: {2} ",n1,n2,n1+n2);
break;
case 2:
Console.Write("Substract of {0} and {1} is: {2} ",n1,n2,n1-n2);
break;
case 3:
Console.Write("The Multiply of {0} and {1} is: {2} ",n1,n2,n1*n2);
break;
case 4:
if(n2==0) {
Console.Write("Devide by zero is not infinity. ");
} else {
Console.Write(" Divideof {0} and {1} is : {2} ",n1,n2,n1/n2);
}
break;
case 5: Console.Write(" n1 is : {1} ",n1);
break;
case 6:
break;
default:
Console.Write("Input correct choice ");
break;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.