1 / 2100%
import java.util.Scanner;
public class Lab4 {
public static void main(String[] args) {
Scanner Scan = new Scanner(System.in);
int choice = 0;
int m, n, sum = 0;
int fact = 1;
do{
System.out.println("Please choose from following menu: ");
System.out.println("1) Calculate the sum of integers m to n.");
System.out.println("2) Calculate the factorial of given number.");
System.out.println("3) Find the first digit of a given number.");
System.out.println("4) Quit.");
choice = Scan.nextInt();
// Use scanner to collect user input
switch(choice) {
case 1:
sum=0;
System.out.println(" Enter the starting number (m): ");
m = Scan.nextInt();
System.out.println(" Enter the starting number (n): ");
n = Scan.nextInt();
int count = m; // we will count from m to n
while (count <= n) {
sum = sum + count;
count++;
}
System.out.println(" Enter the sum of the integers:" + sum);
break;
case 2:
System.out.println("Enter a number for n: ");
n = Scan.nextInt();
fact = 1;
count = 1; // we will count from 1 to n
while(count <= n) {
fact = fact * count;
count++;
}
System.out.println("Value of fact:" + fact);
// ------------------------------------------------------------
CSE 110- Lab #4
// AUTHOR: Brandon Evans
// FILENAME: Lab4
// SPECIFICATION: Displaying menu options and performing arithmetic operations
based on user choice
// FOR:
// TIME SPENT: 2 hrs
//-------------------------------------------------------------
break;
case 3:
n= 0;
m= 0;
System.out.println(" Enter the n: ");
n = Scan.nextInt();
while(n != 0) {
m = n % 10;
n = n / 10;
}
System.out.println(" Print the value of m:" + m);
break;
case 4:
System.out.println(" The user is quitting the program.");
break;
default:
System.out.println(" Please enter a valid number.");
break;
}
}
while(choice != 4);
}
}
Powered by TCPDF (www.tcpdf.org)
Students also viewed