write a program to print the factor of 10
Answers
The given problem is solved using language - Java.
public class Factors_Of_10{
public static void main(String s[]){
System.out.print("Factors of 10 are: ");
for(int i=1;i<=10;i++){
if(10%i==0)
System.out.print(i+" ");
}
}
}
- Line 1: Class declaration.
- Line 2: Method declaration. Starting point of execution of this program.
- Line 3: Prints a message.
- Line 4 - 7: Here, a loop iterates in range 1 to 10. If any number in this range divides 10, then it is displayed.
- Line 8: End of main().
- Line 9: End of class.
Factors of 10 are: 1 2 5 10
See attachment for verification.
Answer:
Program In Java:-
Using Do while loop:-
public class Main
{
public static void main(String args[])
{
int n=10,i=1;
System.out.println("Factors of 10 are:");
do
{
if(n%i==0)
System.out.print(i+" ");
i++;
}
while(i<=10);
}
}
Using for loop:-
public class Main
{
public static void main(String args[])
{
int n=10,i=1;
System.out.println("Factors of 10 are:");
for( i=1;i<=10;i++)
{
if(n%i==0)
System.out.print(i+" ");
}
}
}
Using while loop:-
public class Main
{
public static void main(String args[])
{
int n=10,i=1;
System.out.println("Factors of 10 are:");
while(i<=10)
{
if(n%i==0)
System.out.print(i+" ");
i++;
}
}
}
Logic:-
- Run a loop upto 10 if(n%i==0) means if I at any iteration is divisible by i it prints i (means it's factor).