Question:

What is wrong with this program?

by  |  earlier

0 LIKES UnLike

public class ans

{

public static void main (String args[])

{

int sum=0,n=5;

int c=n;

for (int a=1;a>n;a++)

{

if (n%a==0)

sum=sum+a;

else continue;

}

if (c==sum)

System.out.println("yes");

else

System.out.println("no");

}

}

 Tags:

   Report

1 ANSWERS


  1. You don't say what computer language you're using. It seems to be Java.

    There may be other things wrong with the program, but mostly I notice that your first for loop sets "a>n" as its condition. Since a is initialized to 1 and n starts as 5, the loop will not even execute once.

    There may be computer languages where the second term in a for loop statement is an until condition, but all of the ones I know use a while condition. You'd want to change it to a<=n.

Question Stats

Latest activity: earlier.
This question has 1 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.
Unanswered Questions