package primecdesc;
import java.util.Scanner;
public class Primecdesc {
public static void main(String[] args) {
Scanner myScanner = new Scanner (System.in);
int a,b=0,ctr=0, ktr=1;
System.out.print("Enter a number: ");
a = myScanner.nextInt();
while(b>=1001)
{
if (a%b==0)
b++;
ctr--;
}
/*for(b=1;b<=1001;b++)
{
if(a%b==0)
ctr--;
}*/
if(ctr==1)
{
System.out.println(a + " is a prime number!!!");
System.out.print("Factor of " + a + " is itself only!!!");
System.out.println(" ");
for (a=1; ktr<11; ktr++)
{
if(a%2==1 && a%3!=0 && a%25!=0 || a==1 || a==2 || a==3){
System.out.print(a + " ");
}
a--;
if (a>1) {break;}
}
}
/*while(ktr<11){
if(a%2==1 && a%3!=0 && a%25!=0 || a==1 || a==2 || a==3){
System.out.print(a + " ");
ktr++;
}*/
else if(ctr>=3)
{
System.out.println(a + " is a composite number!!!");
System.out.print("Factor of " + a + " are ");
while (b<=a)
{
if(a%b==0)
if(b==1)
System.out.print(b + " ");
else
System.out.print(b + " ");
b++;
}
/*for(b=1;b<=a;b++)
{
if(a%b==0)
if(b==1)
System.out.print(b + " ");
else
System.out.print(b + " ");
}*/
System.out.print("!!!");
System.out.println(" ");
for (a=1; ktr<11; ktr++)
{
if(a%2==0 || a%3==0 || a%5==0)
{
System.out.print(a + " ");
ktr++;
}
a--;
if (a>4) {break;}
}
}
/*while(ktr<11){
if(a%2==0 || a%3==0 || a%5==0){
System.out.print(a + " ");
ktr++;
}
a--;
if (a>4) {break;}
}*/
else
{
System.out.println(a + " is a prime number!!!");
System.out.print("Factors of " + a + " are 1 and itself only!!!");
System.out.println(" ");
[B]for (a=1; ktr<11; ktr++)
{
if(a%3!=0 && a%5!=0 && a%4!=0 && a%2!=0 && a%7!=0 || a==2 || a==3 || a==5)
{
System.out.print(a + " ");
}
a--;
if (a<1){break;}
}
}[/B]
/*while(ktr<11){
if(a%3!=0 && a%5!=0 && a%4!=0 && a%2!=0 && a%7!=0 || a==2 || a==3 || a==5){
System.out.print(a + " ");
ktr++;
}
a--;
if (a<1){break;}
}
}*/
System.out.print("!!!");
System.out.println(" ");
System.out.println("Have a nice day!!!");
}
}
On the bold part is my problem. When my loop is in while form, it runs perfectly, but when i try to change it to "for loop", it displays a wrong output.