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.

You are mixing "a" and "ktr" in a manner that doesn't make sense.
Did you mean for "a" to start at 1 and go to -1?

So let me understand this properly:
You have a program that does something - exactly what is a secret, which is why you have omitted the specification, removed all the comments, and changed the variable names to something meaningless. You replaced while loop that has some unknown function with a for loop, and now instead of perfect output you have incorrect output, neither of which you can share with us for reasons of commercial confidenliaty.
OK, seriously, do you think you have given us enough to understand your problem?


Anyway you moved the incrementing of the control variable ktr from inside the incomprehensible if test to outside it when you changed the loop type. That presumably leads to a different result?

... plus in the do version a is initialised to 1, whereas in the while version it's not initialised (keeps its previous value)

This article has been dead for over six months. Start a new discussion instead.