# Coding question

Given the age of 10 couples(husband and wife), print Number of years of age difference for all the 10 people Input Specification First 10 inputs contain the ages of all husbands And the next 10 are the ages of all wives Output Specification Output should be exactly 10 lines giving the number of years of the age difference between them. Note: output is a positive integer Sample Input 50 40 53 24 78 99 63 22 55 15 43 39 55 24 56 89 60 18 56 25 Sample Output 7 1 2 0 22 10 3 4 1 10

Showing Answers 1 - 5 of 5 Answers

#### naina

• May 6th, 2015

Please find the below code

```Code#include<stdio.h>

void main()
{
int arr = {40,54,58,32,64,48,23,30,28,60};
int m, n= 10, i,diff=0;
m=n/2;
for(i=0;i<n/2;i++)
{
diff = arr[i]- arr[m];
printf("%d
",abs(diff));
m++;
}
}```

Was this answer useful?  Yes

#### mcb

• Sep 10th, 2015

This answer is wrong on many levels...
1) No input , just a set array of ages (not matching question by the way)
2) only 10 people, not 20 husbands and wives.
3) need to use /n in printf not an actual New Line.

Was this answer useful?  Yes

#### amrendra

• Oct 6th, 2015

```Codepublic static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Please input male ages");
ArrayList<Integer> maleages = new ArrayList<Integer>();
for (int i = 0; i < 10; i++) {
int age = sc.nextInt();
maleages.add(age);
}
System.out.println("Please input female ages");
ArrayList<Integer> femaleages = new ArrayList<Integer>();
for (int i = 0; i < 10; i++) {
int age = sc.nextInt();
femaleages.add(age);
}
Iterator<Integer> it = maleages.iterator();
int i = 0;
while (it.hasNext()) {
System.out.print(it.next() - femaleages.get(i++)+", ");
}
}```

Was this answer useful?  Yes

#### Ashwini

• Jun 1st, 2016

```Codeimport java.util.*;

class Test{

public static void main(String []args){

Scanner sc=new Scanner(System.in);
int []h=new int;
int []w=new int;
int []a=new int;

for(int i=0;i<10;i++)
{
h[i]=sc.nextInt();
}

for(int i=0;i<10;i++)
{
w[i]=sc.nextInt();
}

for(int i=0;i<10;i++)
{
a[i]=h[i]-w[i];
}

for(int i=0;i<10;i++)
{
if(a[i]<0)
a[i]=a[i]*-1;
System.out.println(a[i]);
}
}
}```

Was this answer useful?  Yes

#### leela

• Jun 1st, 2016

By giving array the size 10 I am getting ArrayIndexOutOfBoundsException what should I do now

Was this answer useful?  Yes

### Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.  