Showing posts with label di while loop. Show all posts
Showing posts with label di while loop. Show all posts

Wednesday, September 1, 2010

While Loop get even numbers

hello programmers.... i want to share to you how to create a program that get the even numbers using modulo operator and while loop.

here is the program:

import java.util.Scanner;

public class evennumbers
{
public static void main(String args[])
{
Scanner input=new Scanner(System.in);
int num;
int x=1;
System.out.print("How many numbers you want to print:");
num=input.nextInt();

while(x<=num)
{
if(x%2==0)
{
System.out.println(x);
}
x++;
}

}
}

Wednesday, August 4, 2010

Sample Program using while statement

this program will ask the user to input how many grades you want to compute
and get the average and check if Passed or Failed.

import java.util.Scanner;

public class Grades
{
public static void main(String args[])
{
Scanner input=new Scanner(System.in);
int num;
int grd;
int start=1;
int sum=0;
int ave=0;

System.out.print("How many Grades you want to compute:");
num=input.nextInt();

while(start<=num) { System.out.print("Grade" + start + ":"); grd=inout.nextInt(); sum=sum+grd; ave=sum/num; } System.out.print("The average is:" + ave); if(ave>=75)
{
System.out.println("Remarks: Passed");
}
else
{
System.out.print("Remarks: Failed");
}

}
}

Tuesday, August 3, 2010

while statement program

Problem: Create program that ask the user to input "How many hello world you want to print" and the output show number and hello world.

import java.util.Scanner;

public class helloworld
{
public static void main(String args[])
{
    Scanner input=new Scanner(System.in);
    int num;
    int start=1;
  System.out.print("How hello world you want to display");
  num=input.nextInt();

  while(start<= num)
  {
     System.out.println(start + ".Hello");
    start++;
  }

}
}

Monday, August 2, 2010

While Loop

While Loop is a control flow statement that allows code to be executed repeatedly based on a given boolean condition. The while loop can be thought of as a repeating if statement.

The while construct consists of a block of code and a condition. The condition is evaluated, and if the condition is true, the code within the block is executed. This repeats until the condition becomes false. Because while loops check the condition before the block is executed, the control structure is often also known as a pre-test loop. Compare with the do while loop, which tests the condition after the loop has executed.

syntax of while loop

initialization;
while(condition)
{
Statements..
inc/dec;
}

Looping

Looping is a programming language statement which allows code to be repeatedly executed. A for loop is classified as an iteration statement. Unlike many other kinds of loops, such as the while loop, the for loop is often distinguished by an explicit loop counter or loop variable.

there are three different type of looping, used in programming.
1. while Loop
2. do while Loop
3. for Loop