Ques: Write a Java program if input array is like {10,20,30,40} then output should be like {90,80,70,60}.
Output :
package com.tutorial4u;
import java.util.Scanner;
public class ArrayDemo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int sum=0;
System.out.println("Enter size of an array");
int num = sc.nextInt();
System.out.println("Enter Element of an
array");
int[] a = new int[num];
int[] b = new int[num];
for(int i =0;i<num;i++){
a[i] = sc.nextInt();
}
for(int i=0;i<num;i++){
sum+= a[i];
}
for(int i=0;i<num;i++){
b[i]= sum-a[i];
}
System.out.println("Output is:");
for(int number:b){
System.out.println(number);
}
}
}
Output :
Enter
size of an array
4
Enter
Element of an array
10
20
30
40
Output
is:
90
80
70
60
No comments:
Post a Comment