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

In the for loop you set a=1, but you don't in the while loop.

Your FOR loops are completely wrong

``````for (a=1; ktr<11; ktr++)            // you start A at 1 but use KTR as the loop index
{
if(a%2==0 || a%3==0 || a%5==0)
{
System.out.print(a + " ");
ktr++;                      // you already do this in the FOR loop
}
a--;                            // you decrement A and use it to exit
if (a>4) {break;}               //    the loop but that's what the KTR<11
//    is supposed to do in the FOR loop
//    Also, you start A at 1 and decrement,
//    so how does it get to 4?
}``````

The other loop (the bolded one)

``````for (a=1; ktr<11; ktr++)    // you start A at 1 but use KTR as the loop index
{                           // You never use initialize nor use 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--;                // you decrement A and use it to exit
if (a<1){break;}    //    the loop but that's what the KTR<11
}                       //    is supposed to do in the FOR loop``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.