Showing posts with label sum of numbers using while statement in Java. Show all posts
Showing posts with label sum of numbers using while statement in Java. Show all posts

Saturday, July 25, 2015

Sum of Numbers Using While Loop

A simple program that will sum up the  series of number using while looping statement in Java.

If you have some questions about programming, about my work please send mu an email at jakerpomperada@gmail.comand jakerpomperada@yahoo.com. People here in the Philippines can contact me at  my mobile number 09173084360.

Thank you very much and Happy Programming.



Sample Program Output

Program Listing

/*
while_loops.java
Programmer By: Mr. Jake R. Pomperada, MAED-IT
Date : July 21, 2015

Problem No. 1:
  
Design a program that calculates the sum of the input given
number of N using while loop statement.

*/


import java.util.Scanner;

class while_loops {
    
public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    int sum=0,b=0;
    System.out.print("\t Sum of Numbers Using While Loop");
    System.out.println();
    System.out.print("Enter your desired looping number : ");
    int value = input.nextInt();
    while (b<=value) {
      System.out.print(" " + b + " ");
        sum+=b;
        b++;
    }   
    System.out.println();
    System.out.println("The total sum is  " + sum +".");
    System.out.println();
    System.out.println(" END OF PROGRAM"); 
    System.out.println();
  }
}