Monday, 14 April 2014

JAVA Program To Implement The Sum Of 'N' Numbers


Write a JAVA Program To Implement The Sum Of 'N' Numbers

Ex:  SIMPLE CLASS DESIGN IN JAVA

Aim:To write a JAVA program to implement the sum of n numbers

Algorithm:

Step 1: Start the program.
Step 2: Declare n=10 and find the sum of 10 numbers.
Step 3: Initialize s=0.
Step 4: For i value from 1 to 10 repeat the process by using for loop.
Step 5: Display the loop.
Step 6: Stop the program.


Program:

import java.io.*;
class simple
{
public static void main(String args[])
{
int i, s=0, n=0;
for (i=1, i<=n; i++)
{
s=s+i;
System.out.println("sum of n numbers:"+s);
}
}
}


Output:

Sum of n number: 1
Sum of n number: 3
Sum of n number: 6
Sum of n number: 10
Sum of n number: 15
Sum of n number: 21
Sum of n number: 28
Sum of n number: 36
Sum of n number: 45
Sum of n number: 55

Result:
Thus the implementation of JAVA program for the sum of n numbers is executed and the output has been verified.

0 comments:

Post a Comment