Sunday, 31 January 2016

assignment for conversion of loop from while to for and do while

dear students please convert the following looping statement into for loop and do while loop

Assignment – IP (class XI)
Convert the following looping statements using for loop, while loop or do.. while loop accordingly.
1.       //to display multiples  of 7
int n=7, ctr=1;
while (ctr<=10)
{
                jTextArea1.setText(jTextArea1.getText()+” “+n*ctr);
                ctr++;
}

2.       //to display even and odd numbers in separate jTextArea controls
//from 1 to the limit given. Also count the number of even and odd numbers  
int max=0, ctr=1, odd_ctr=0, even_ctr=0;

max = Integer.parseInt(jTextField1.getText());

while (ctr<=max)
{
                If(ctr%2==0)
{
jTextArea1.setText(jTextArea1.getText()+” “+ctr);
                                even_ctr++;
                }
                else
{
jTextArea2.setText(jTextArea2.getText()+” “+ctr);
                                odd_ctr++;
                }
                ctr++;
}

3.       //to display skip counting  
int n=0, ctr=1, max=0;

max = Integer.parseInt(jTextField1.getText());

while (ctr<=max)
{
                jTextArea1.setText(jTextArea1.getText()+” “+  ++ctr);
                ctr+=3;

No comments:

Post a Comment