Wednesday, April 20, 2016

Write a code in which keep taking marks from user until user enter a negative number and calculate grade and at end give total number of A,B,C,D,E and F grades.

In this question we are required to  create a program which we will keep taking marks from user until user enter a negative number and then calculate grade after every input .At end give total number of A,B,C,D,E and F grades.
So here is the JAVA code for solving that problem.

Tuesday, April 5, 2016

Example of While loop & Nesting of loops

Nesting of loops:
Nesting of loops mean using loop inside a loop.It could be "For" loop inside a "For" loop or a "For" loop inside a "While" loop or the other way round. 

Our Objective ?
Our objective is to write a JAVA program which keeps on taking values from user unless user give a prime number as input value.
We can easily solve this problem using nested loops.
We will use a "While" loop because we don't know when user will give a prime number as input value.

Sunday, April 3, 2016

Print out table of any number given by the user

As we have already studied about "For Loop" , now we will see some example codes for that.
if you haven't read that before then check out : Introduction to loop and its types .
For example if you want to print out table of any number , given by the user then you can use "For" loop for that process and the code will become:


package table;
import java.util.Scanner;

public class Table
{
    public static void main(String[] args)
    {
        Scanner input=new Scanner(System.in);
        System.out.print("Enter an integer: ");
        int a=input.nextInt();
        for(int i=1;i<=10;i++)
        {
            int b=a*i;
            System.out.println(a+" * "+i+"= "+b);
        }
        
    }
}



Introduction to loop and its types

Actually loop is a process of repeating the same process again and again without writing it multiple times.
For example if you want to take 50 inputs from user then you can use a loop to take those inputs by using the input statement only once.
The process of repeating the same thing again and again ( Loop ) is known as Iteration.
Types of Loop:

  • "For" Loop
  • "While" Loop
  • "Do While" Loop
"For" Loop:
"For" Loop is used when we know how many time iteration will be performed.For example if we know that we will take 50 inputs from user then we will use "For" Loop for that work .
Syntax for a "For" loops is:
for(int i=starting value;condition;increment or decrement)
{
    This is the body of loop.
}

"While" and "Do While" Loop:
"While" and "Do While" Loop is used when don't know how many times the iteration will be performed.For example if we know that we want to do the given process but we have no idea that how many times we will perform this thing then we use "While" or "Do While" loop.
Syntax for "While" loop is :
while(true/condition)
{
    this is the body of "while" loop.
    use a break statement to end the loop . It is recommended to use a break statement in a      condition.
}

Syntax for "Do-While" loop is:
do
{
   This is body of "Do-While" loop.
   
}while(condition);