hello programmers... this program will show to you how to use if statement. It is for computing two input numbers by the user. this is for asking the user to choose the operator they want to execute. 1- addition, 2- subtraction, 3- Multiplication, 4- Quotient. this is a very simple program using nested if statement.
here is a sample program:
import java.io.*;
import java.util.Scanner;
public class chooseoperators
{
public static void main(String args[])
{
Scanner input==new Scanner(System.in);
int add=1;
int sub=2;
int mult=3;
int div=4;
int num1;
int num2;
int oper;
int sum=0,diff=0,prod=0,quo=0;
System.out.println("1- Addition");
System.out.println("2- Subtraction");
System.out.println("3- Multiplication");
System.println("4- Division");
System.out.print("Enter First Number:");
num1=input.nextInt();
System.out.print("Enter Second Number:");
num2=input.nextInt();
System.out.print("Chooose Operators:");
oper=input.nextInt();
if(oper==add)
{
sum=num1+num2;
System.out.println("The sum is:" + sum);
}
if(oper==sub)
{
diff=num1-num2;
System.out.println("The difference is:" +diff);
}
if(oper==mult)
{
prod=num1*num2;
System.out.println("The Product is:" + prod);
}
if(oper==div)
{
quo=num1/num2;
System.out.println("The quotient is:" + quo);
}
}
}
Tuesday, August 31, 2010
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");
}
}
}
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
File Streams
There are four types of streams: InputStream, OutputStream, Reader, and Writer. | |||||||||||||||||
1. Reader. A stream to read characters. 2. Writer. A stream to write characters. 3. InputStream. A stream to read binary data. 4. OutputStream. A stream to write binary data.
|
Labels:
File and Streams,
File Stream,
inputStream,
OutputStream,
printwiter,
Stream
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++;
}
}
}
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
Sample Program of while Loop
this program is using while statement to print 5 hello world in your screen .
here is a sample program;
import java.io.*;
public class samplewhile
{
public static void main(String args[])
{
//declaration
int start=1;
while(start<=5)
{
System.out.print("Hello World");
start++;
}
}
}
here is a sample program;
import java.io.*;
public class samplewhile
{
public static void main(String args[])
{
//declaration
int start=1;
while(start<=5)
{
System.out.print("Hello World");
start++;
}
}
}
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;
}
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;
}
Labels:
di while loop,
syntax of while loop
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
there are three different type of looping, used in programming.
1. while Loop
2. do while Loop
3. for Loop
Labels:
di while loop,
Do while Loop,
for loop,
looping
sample program of switch statement
This program will show to you how switch statement execute by the java compiler.
import java.util.Scanner;
public class SampleSwitch
{
public static void main(String args[])
{
Scanner input=new Scanner(System.in);
//declaration
int num;
System.out.print("Enter a number:");
num=input.nextInt();
Switch(num)
{
case 1:
case 3:
case 5:
case 7:
case 9:
System.out.print("You enter odd number:");
break;
case 2:
case 4:
case 6:
case 8:
case 10:
System.out.print("You enter even number:");
break;
default:
System.out.print("Your number you enter is not found in the case");
}
}
}
understand this program and practice to create your own program using switch statement,,
import java.util.Scanner;
public class SampleSwitch
{
public static void main(String args[])
{
Scanner input=new Scanner(System.in);
//declaration
int num;
System.out.print("Enter a number:");
num=input.nextInt();
Switch(num)
{
case 1:
case 3:
case 5:
case 7:
case 9:
System.out.print("You enter odd number:");
break;
case 2:
case 4:
case 6:
case 8:
case 10:
System.out.print("You enter even number:");
break;
default:
System.out.print("Your number you enter is not found in the case");
}
}
}
understand this program and practice to create your own program using switch statement,,
Switch Statement syntax
the syntax of Switch Statement:
switch(value)
{
case value1:
statement 1;
break;
case value 2:
statement 2;
break;
case value n:
statement ...
break;
default:
statement;
}
i hope you understand this syntax in switch statement to create running program.
}
switch(value)
{
case value1:
statement 1;
break;
case value 2:
statement 2;
break;
case value n:
statement ...
break;
default:
statement;
}
i hope you understand this syntax in switch statement to create running program.
}
Switch Case Statement
Switch Case Statement is a type of selection control statement that exists in most modern imperative programming languages (e.g., Pascal, C, C++, C#, and Java). Its purpose is to allow the value of a variable or expression to control the flow of program execution via a multiway branch (or "goto", one of several labels). The main reasons for using a switch include improving clarity, by reducing otherwise repetitive coding, and (if the heuristics permit), offering the potential of faster execution through compiler optimization. In some programming languages, a statement that is syntactically different but conceptually the same as the switch statement is known as a case statement, select statement or inspect instruction.
Subscribe to:
Posts (Atom)