Looping or Iteration Statements in Java With program to find the perfect number and reversing a number




Looping or Iteration Statements in Java

Entry Controlled Loop:- While, For Loop

In this type of loop condition is checked before execution of looping statements.

Minimum execution is zero time if the initial condition is false. e.g.

for(i=1;i<10;i++)
{
   print i;
}
Exit Controlled Loop:- Do while

In this type of loop condition is checked after the execution of looping statements.

Minimum execution is at least one time if the initial condition is false.

Q.1. WAP to find out a number is a perfect number or not using Java Swing controls and Net beans IDE.(Note:-All loops are used)

Ans.:- First you should know what is a perfect number:-
According to mathematical definition of perfect number an Integer
whose factors sum is that number(Excluding the number itself in the factor)e.g.

6=1+2+3=6

28=1+2+4+7+14=28

So 6 and 28 are perfect numbers.

Program:-

Design View


int n,i,sum=0;
       n=Integer.parseInt(jTextField1.getText());
       i=1;
       while(i<n)
       {
           if(n%i==0)
           {
               sum=sum+i;
           }
           i++;
       }
       if(sum==n)
       {
           JOptionPane.showMessageDialog(this,"The given no"+" "+n+" "+"is a perfect no");

       }
       else
       {
           JOptionPane.showMessageDialog(this,"The given no"+" "+n+" "+"is not a perfect no");
       }

int n,i,sum=0;
        n=Integer.parseInt(jTextField1.getText());
        i=1;
        do
        {
            if(n%i==0)
            {
                sum=sum+i;
            }
            i++;
         }while(i<n);
         if(sum==n)
       {
           JOptionPane.showMessageDialog(this,"The given no"+" "+n+" "+"is perfect no");

       }
       else
       {
           JOptionPane.showMessageDialog(this,"The given no"+" "+n+" "+"is not a perfect no");
       }

int n,i,sum=0;
        n=Integer.parseInt(jTextField1.getText());
        for(i=1;i<n;i++)
        {
            if(n%i==0)
            {
                sum=sum+i;
            }
           
         }
         if(sum==n)
       {
           JOptionPane.showMessageDialog(this,"The given no"+" "+n+" "+"is perfect no");

       }
       else
       {
           JOptionPane.showMessageDialog(this,"The given no"+" "+n+" "+"is not a perfect no");
       }

Run View

Q.1. WAP to find out the reverse of a number using Java Swing controls and Net beans IDE.(Note:-All loops are used).
Design View


int no,q,r,rev=0;
       no=Integer.parseInt(jTextField1.getText());
       while(no>0)
       {
           q=no/10;
           r=no%10;
           rev=rev*10+r;
           no=q;
       }
       jTextField2.setText(""+rev);




Run View


Dry Run
no=123;

(123>0) True

q=123/10=12;
r=123%10=3;
rev=0*10+3=3;
no=12;

(12>0) True

q=12/10=1;
r=12%10=2;
rev=3*10+2=32;
no=1;

(1>0) True

q=1/10=0;
r=1%10=1;
rev=32*10+1=321;
no=0;

(0>0) False

Loop terminate and execution will move to outside of the loop i.e.

jTextField2.setText(""+rev);

This line will print the value of reverse of the given number.

No comments:

Post a Comment