Q:

Java Program to find sum of natural numbers by getting input from user

0

Java Program to find sum of natural numbers by getting input from user using for loop.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

import java.util.Scanner;
public class demo1 {

    public static void main(String[] args) {

        int num, count, total = 0;

        System.out.println("Enter the value of n:");
        Scanner n = new Scanner(System.in);
        num = n.nextInt();
        n.close();
        for(count = 1; count <= num; count++){
            total = total + count;
        }

        System.out.println("Sum of first "+num+" natural numbers is: "+total);
    }
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now