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
  1. #include<stdio.h>

  2.  

  3. void main()

  4. {

  5.  int arr[10] = {40,54,58,32,64,48,23,30,28,60};

  6.   int m, n= 10, i,diff=0;

  7. m=n/2;

  8. for(i=0;i<n/2;i++)

  9. {

  10.   diff = arr[i]- arr[m];

  11. printf("%d

  12. ",abs(diff));

  13. m++;

  14. }

  15. }

  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
 

Code
  1. public static void main(String[] args) {

  2.                 Scanner sc = new Scanner(System.in);

  3.                 System.out.println("Please input male ages");

  4.                 ArrayList<Integer> maleages = new ArrayList<Integer>();

  5.                 for (int i = 0; i < 10; i++) {

  6.                         int age = sc.nextInt();

  7.                         maleages.add(age);

  8.                 }

  9.                 System.out.println("Please input female ages");

  10.                 ArrayList<Integer> femaleages = new ArrayList<Integer>();

  11.                 for (int i = 0; i < 10; i++) {

  12.                         int age = sc.nextInt();

  13.                         femaleages.add(age);

  14.                 }

  15.                 Iterator<Integer> it = maleages.iterator();

  16.                 int i = 0;

  17.                 while (it.hasNext()) {

  18.                         System.out.print(it.next() - femaleages.get(i++)+", ");

  19.                 }

  20.         }

  Was this answer useful?  Yes

Ashwini

  • Jun 1st, 2016
 

Code
  1. import java.util.*;

  2.  

  3. class Test{

  4.    

  5. public static void main(String []args){

  6.    

  7. Scanner sc=new Scanner(System.in);

  8. int []h=new int[10];

  9. int []w=new int[10];

  10. int []a=new int[10];

  11.  

  12. for(int i=0;i<10;i++)

  13. {

  14.     h[i]=sc.nextInt();

  15. }

  16.  

  17. for(int i=0;i<10;i++)

  18. {

  19.     w[i]=sc.nextInt();

  20. }

  21.  

  22.  

  23. for(int i=0;i<10;i++)

  24. {

  25.     a[i]=h[i]-w[i];

  26. }

  27.  

  28. for(int i=0;i<10;i++)

  29. {

  30.     if(a[i]<0)

  31.      a[i]=a[i]*-1;

  32.      System.out.println(a[i]);

  33. }

  34. }    

  35. }

  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.

 

Related Answered Questions

 

Related Open Questions