

InterviewSolution
1. |
WAP to input a number and find its odd factors. |
Answer» en problem is solved using language - Java.import java.util.*; PUBLIC class OddFactors{ public static void MAIN(String s[]){ Scanner sc=new Scanner(System.in); int n,i; System.out.print("Enter a number: "); n=sc.nextInt(); sc.close(); System.out.print("The odd factor(s) of the number is/are: "); for(i=1;i<=n;i+=2){ if(n%i==0) System.out.print(i+" "); } } }Here the loop iterates from i = 1 to n. If the number (n) is divisible by 'i', then the value of the VARIABLE 'i' is DISPLAYED. After each iteration, the value of 'i' is incremented by 2 as we have to display only the odd factors of the number.See attachment for output. |
|