18. Write a program input 10 number in two array and display all common number
Answers
Answer:
ABCDEFGHIJKLMNOPQRSTUVWXYZ
Answer:
#include <stdio.h>
int main()
{
int i,arr[10],sum=0;
printf("Enter 10 elements:");
for(i=0;i<10;++i)
scanf("%d",&arr[i]);
for(i=0;i<10;++i)
sum=sum+arr[i];
printf("Sum of numbers is:%d",sum);
return 0;
}
In C++
#include <iostream>
using namespace std;
int main()
{
int i,arr[10],sum=0;
cout<<"Enter 10 elements:";
for(i=0;i<10;++i)
cin>>arr[i];
for(i=0;i<10;++i)
sum=sum+arr[i];
cout<<"Sum of numbers is:"<<sum;
return 0;
}
In java
import java.util.Scanner;
public class Example12 {
public static void main(String[] args) {
int i,sum=0;
int arr[]=new int[10];
System.out.println("Enter 10 numbers");
Scanner sc=new Scanner(System.in);
for(i=0;i<10;++i)
arr[i]=sc.nextInt();
for(i=0;i<10;++i)
sum=sum+arr[i];
System.out.println("Sum of numbers is:"+sum);
}
}
Here i use two loops, one for reading and other for finding sum.
You can combine reading and summing into same loop as
for(i=0;i<10;++i)
{
scanf("%d",&arr[i]);
sum=sum+arr[i];
}
Explanation: