Write a program that allows the user to input a name, surname and address. The program should output the name, surname and address 10 times. Use the For and WHILE LOOP.
2. Write a program using the SWITCH CASE Statement to enable the user to choose from 4 different operations as listed below.
1) Sum (+)
2) Minus (‐)
3) Multiplication (*)
4) Division (/)
5) Exit
The user will input two numbers and will then choose the operation from the menu above. The program will then work the operation chosen by the user and display the result accordingly. Loop the whole program until the user presses ‘5’.
3. Write statements that will read numbers and print out the sum of thenumbers. The program should stop when the user enters -999, or after reading 5 numbers, whichever comes first.
4. Write a program segment using nested loops to create the following
output:
*
**
***
****
*****
******
*******
********
*********
: complete the question in the comments
SOLUTION TO THE ABOVE QUESTION
3. Write statements that will read numbers and print out the sum of thenumbers.
The program should stop when the user enters -999, or after reading 5 numbers,
whichever comes first.
ANSWER.
package com.company;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// write your code here
//propmt the user to enter a positive odd integer
Scanner sc = new Scanner(System.in);
// declare a variable that will store the numbers entere by the user
//and initilaize it to zero
int sum = 0;
for(int i = 0; i< 5; i++)
{
System.out.print("Enter a number : ");
int number = sc.nextInt();
if(number==-999)
{
break;
}
sum = sum + number;
}
//Now print the sum of the numbers
System.out.println("The sum of the numbers = "+sum);
}
}
4. Write a program segment using nested loops to create the following
output:
*
**
***
****
*****
******
*******
********
*********
ANSWER.
package com.company;
public class Main {
public static void main(String[] args) {
for(int i = 0; i<9; i++)
{
for(int j = 0; j<=i; j++)
{
System.out.print("*");
}
//go in a newline
System.out.println();
}
}
}
SAMPLE OUTPUT FOR QUESTION 3
SAMPLE OUTPUT FOR QUESTION 4
Comments
Leave a comment