//Adding 'n' number of input values.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class SumUsingArray
{
public static void main(String args[]) throws IOException
{
String s;
int i,n,sum=0;
int number[]=new int[100];
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("How many number you want to adding?");
s = br.readLine();
n = Integer.valueOf(s).intValue();
for(i=1;i<=n;i++)
{
System.out.print("Enter number "+i+" = ");
number[i]=Integer.valueOf(br.readLine()).intValue();
sum=sum+number[i];
}
System.out.println("Total of the above numbers:");
for(i=1;i<=n;i++)
{
System.out.print(number[i]);
if(i!=n)
{
System.out.print("+");
}
}
System.out.print("="+sum);
}
}
Output =>
How many number you want to adding?
5
Enter number 1 = 10
Enter number 2 = 12
Enter number 3 = 14
Enter number 4 = 15
Enter number 5 = 19
Total of the above numbers:
10+12+14+15+19=70
import java.io.IOException;
import java.io.InputStreamReader;
public class SumUsingArray
{
public static void main(String args[]) throws IOException
{
String s;
int i,n,sum=0;
int number[]=new int[100];
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("How many number you want to adding?");
s = br.readLine();
n = Integer.valueOf(s).intValue();
for(i=1;i<=n;i++)
{
System.out.print("Enter number "+i+" = ");
number[i]=Integer.valueOf(br.readLine()).intValue();
sum=sum+number[i];
}
System.out.println("Total of the above numbers:");
for(i=1;i<=n;i++)
{
System.out.print(number[i]);
if(i!=n)
{
System.out.print("+");
}
}
System.out.print("="+sum);
}
}
Output =>
How many number you want to adding?
5
Enter number 1 = 10
Enter number 2 = 12
Enter number 3 = 14
Enter number 4 = 15
Enter number 5 = 19
Total of the above numbers:
10+12+14+15+19=70
No comments:
Post a Comment