public class perfectnumber{// main method begins
public static void main (String []args){
int sum=0;//sum initialize as zero
int i=1;//”I” initialize as 1
int number;//number taken by the user
Scanner input = new Scanner( System.in );// create Scanner to obtain input from command window
System.out.print( "Enter first number: " ); // prompt
number= input.nextInt(); // read first number from user
while(i
sum=sum+i;}
i=i+1;//changing variable
}//end loop
if (sum==number){//condition works when loop is false
System.out.print("It is a perfect Number");}// display
else//condition works when "if" is false
{System.out.print("It is not a perfect Number");}// display
}// end method main
}// end class Addition
No comments:
Post a Comment