Write a program to enter 5 number and print the sum of all number,
Answers
Here comes your code!
import java.util.Scanner;
public class Java{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int n=5,i,sum=0,product=1,diff;
int a[]=new int[n];
System.out.println("Enter 5 numbers...");
for(i=0;i<n;i++){
System.out.print("Enter: ");
a[i]=sc.nextInt();
if(i<3)
product*=a[i];
sum+=a[i];
}
diff=a[0]-a[1];
System.out.println("Sum of all the numbers: "+sum);
System.out.println("Product of first three numbers: "+product);
System.out.println("Subtraction of first and second number: "+diff);
System.out.print("Square of all numbers: ");
for(int x:a)
System.out.print(x*x+" ");
System.out.print("\nCube of first three numbers: ");
i=0;
for(int x:a){
System.out.print(x*x*x+" ");
if(i++==2)
break;
}
}
}
Note: Here, I have used language Java for answering as no language is mentioned.
Enter 5 numbers...
Enter: 5
Enter: 4
Enter: 3
Enter: 2
Enter: 1
Sum of all the numbers: 15
Product of first three numbers: 60
Subtraction of first and second number: 1
Square of all numbers: 25 16 9 4 1
Cube of first three numbers: 125 64 27