Go to Top

Friday 27 May 2011

To print a pattern in Java

Question 13 : Write a program in Java to print the following pattern : 

1
22
333
4444
55555

Java Program : 


public class pattern_1
{
 public static void main()
 {
     int i,j;
     for(i=1;i<=5;i++)
     {
         for(j=i;j>=1;j--)
         {
             System.out.print(i);
            }
            System.out.print("\n");
        }
    }
}

Output : 


1
22
333
4444
55555

Note : Use the calculator and the notepad provided, at the top, to dry run the program.

To print the sum of the even numbers upto n in Java

Question 12 : Write a program in Java to compute the sum of the even numbers upto n terms using the for loop.


Java Program : 


class sum_even
{
 public static void main(int n)
 {
     int s=0,i,c=0;
     for(i=1;;i++)
    {
      if(i%2==0)
      {
        s=s+i;
        c=c+1;
     }
     if(c==n)
     break;
    }
     System.out.print("Sum : "+s);
    }
}

Output : 

When n=15;

Sum : 240

Note : Use the same procedure to print the sum of odd numbers upto n terms.
Note : Use the calculator and the notepad provided, at the top, to dry run the program.

To print the sum of the first ten odd numbers in Java

Question 11 : Write a program in Java to compute the sum of the first ten odd numbers using the for loop.


Java Program : 


class sum_even
{
 public static void main()
 {
     int s=0,i,c=0;
     for(i=1;;i++)
    {
      if(i%2!=0)
      {
        s=s+i;
        c=c+1;
     }
     if(c==10)
     break;
    }
     System.out.print("Sum : "+s);
    }
}

Output : 


Sum : 100

Note : Use the calculator and the notepad provided, at the top, to dry run the program.

To print the sum of the first ten even numbers in Java

Question 10 : Write a program in Java to compute the sum of the first ten even numbers using the for loop.


Java Program : 


class sum_even
{
 public static void main()
 {
     int s=0,i,c=0;
     for(i=1;;i++)
    {
      if(i%2==0)
      {
        s=s+i;
        c=c+1;
     }
     if(c==10)
     break;
    }
     System.out.print("Sum : "+s);
    }
}

Output : 


Sum : 110

Note : Use the calculator and the notepad provided, at the top, to dry run the program.

Monday 16 May 2011

To print the sum of the first ten numbers in Java

Question 9 : Write a program in Java to print the sum of the first ten numbers using the for loop.


Java Program : 


class sum
{
 public static void main()
 {
     int s=0,i;
     for(i=1;i<=10;i++)
     s=s+i;
     System.out.print("Sum : "+s);
    }
}

Output : 


Sum : 55

Saturday 14 May 2011

The For Loop in Java

                          The For loop is a kind of loop in Java which is used to repeat a set of instructions of code for a known number of times until the condition given to it becomes false. Its syntax is given below :

for(i=1;i<=10;i++)
{
-----code-----
-----------------
}

                          The first part within the first semicolon is the initialization of the variable (here "i") used as the counter in the loop. The second part between the two semicolons is the condition which when becomes false results in the termination of the loop. Here the condition given is "i<=10" which means that the value of the variable i must be less then or equal to 10, but not greater than 10. The third and the last part after the second semicolon is the increment or decrement operation (here i is incremented). After every completion of the execution of the whole set of the code, the value of the variable used in the loop is incremented or decremented as according to the instructions given, by a fixed amount as given by the user. For example, to increment the value of i by 5, we can write "i=i+5" or "i+5" instead of "i++", and it would definitely do our job.

Note : The three parameters of the for loop i.e. initialization, condition, and increment/decrement operation are totally optional. The for loop can work without any or all parts of it missing, though the the two semicolons are necessary to be given. below is an example of such a loop with all its parts missing, which would then result in an infinite loop i.e. the loop that will run forever.

for(;;)
{
-----code-----
-----------------
}

ShareThis