In: Computer Science
Define a java problem with user input, user output, Do While Statement and some mathematical computation. Write the pseudocode, code and display output.
Here i have, provided the sum of user entered numbers using Do while loop
Pseudocode:
// initialize required variables
int sum = 0;
int num = 0;
// take user input
Scanner input = new Scanner(System.in);
// do while loop for continuos input for positive number
do {
// it will add the positive numbers
sum += num;
System.out.println("Enter positive number");
num = input.nextInt();
} while(num >= 0);
// print the sum
System.out.println("Sum is : " + sum);
Program screenshot:
Code to copy:
// required library
import java.util.Scanner;
// create a main class
class Main {
// main method
public static void main(String[] args) {
// required variables initialization
int sum = 0;
int num = 0;
// scanner class object for user input
Scanner input = new Scanner(System.in);
// do loop will iterate continuosly for positive num
do {
// addition of numbers
sum += num;
// display message
System.out.println("Enter positive number");
// take input as a number
num = input.nextInt();
// iterate for positive numbers
} while(num >= 0);
// display sum
System.out.println("Sum is : " + sum);
input.close();
}
}
--------------------------------------------------------PLEASE UPVOTE----------------------------------------------------