Computer Science, asked by chaudhurytapasi1984, 2 months ago

WAP to input a number and find its odd factors.​

Answers

Answered by anindyaadhikari13
3

\texttt{\textsf{\large{\underline{Solution}:}}}

The given 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+" ");

       }

   }

}

\texttt{\textsf{\large{\underline{Explanation}:}}}

  • 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.

Attachments:

anindyaadhikari13: Thanks for the Brainliest :)
Similar questions