This Java program takes input from the user to add a series of numbers. The program uses a for loop to iterate through the specified number of inputs and add each number to a running total.
Overall, this program is a simple way to add a series of numbers in Java using a for loop. However, it assumes that the user will always enter valid integers, and it does not perform any error handling or input validation. For a more robust program, it may be necessary to include additional code to handle errors or invalid input.
import java.util.Scanner; class Add_Numbers { public static void main(String[] args) { int n,num,sum = 0; Scanner sc = new Scanner(System.in); System.out.print("Enter the Limit :"); n = sc.nextInt(); for(int i=1; i<=n ; ++i) { System.out.printf("Enter Number %d :",i); num = sc.nextInt(); sum = sum + num; } System.out.println("Sum of given Numbers : " + sum); } }
Enter the Limit :3 Enter Number 1 :23 Enter Number 2 :18 Enter Number 3 :9 Sum of given Numbers : 50
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions