sample program using for loop in java.
this program will ask the user to input how many numbers they want to print:
import java.util.Scanner;
public class samplefor
{
public static void main(String args[])
{
Scanner input=new Scanner(System.in);
int num;
int x;
System.out.print("How many numbers you want to print:");
num=input.nextInt();
for(x=0; x<=num; x++)
{
System.out.println(x");
}
}
}
Friday, April 30, 2010
Thursday, April 29, 2010
for Loop
for loop is a part of programming to execute program continuesly until it reaches the final looping of final number of loop.
example program.
int x;
for(x=0; x<=10; x++)
{
System.out.println(x);
}
sample output:
0
1
2
3
4
5
6
7
8
9
10
example program.
int x;
for(x=0; x<=10; x++)
{
System.out.println(x);
}
sample output:
0
1
2
3
4
5
6
7
8
9
10
Labels:
for loop,
looping,
program from 0 to 10
Tuesday, April 27, 2010
syntax in programming
syntax is a rules in programing.
for loop sysntax:
for(init; condition; inc/dec)
{
statements;
}
for loop sysntax:
for(init; condition; inc/dec)
{
statements;
}
Monday, April 26, 2010
Swap number using java program
1. /*
2. Swap Numbers Java Example
3. This Swap Numbers Java Example shows how to
4. swap value of two numbers using java.
5. */
6.
7. public class SwapElementsExample {
8.
9. public static void main(String[] args) {
10.
11. int num1 = 10;
12. int num2 = 20;
13.
14. System.out.println("Before Swapping");
15. System.out.println("Value of num1 is :" + num1);
16. System.out.println("Value of num2 is :" +num2);
17.
18. //swap the value
19. swap(num1, num2);
20. }
21.
22. private static void swap(int num1, int num2) {
23.
24. int temp = num1;
25. num1 = num2;
26. num2 = temp;
27.
28. System.out.println("After Swapping");
29. System.out.println("Value of num1 is :" + num1);
30. System.out.println("Value of num2 is :" +num2);
31.
32. }
33. }
34.
35. /*
36. Output of Swap Numbers example would be
37. Before Swapping
38. Value of num1 is :10
39. Value of num2 is :20
40. After Swapping
41. Value of num1 is :20
42. Value of num2 is :10
43. */
this program will swap number numbers.
2. Swap Numbers Java Example
3. This Swap Numbers Java Example shows how to
4. swap value of two numbers using java.
5. */
6.
7. public class SwapElementsExample {
8.
9. public static void main(String[] args) {
10.
11. int num1 = 10;
12. int num2 = 20;
13.
14. System.out.println("Before Swapping");
15. System.out.println("Value of num1 is :" + num1);
16. System.out.println("Value of num2 is :" +num2);
17.
18. //swap the value
19. swap(num1, num2);
20. }
21.
22. private static void swap(int num1, int num2) {
23.
24. int temp = num1;
25. num1 = num2;
26. num2 = temp;
27.
28. System.out.println("After Swapping");
29. System.out.println("Value of num1 is :" + num1);
30. System.out.println("Value of num2 is :" +num2);
31.
32. }
33. }
34.
35. /*
36. Output of Swap Numbers example would be
37. Before Swapping
38. Value of num1 is :10
39. Value of num2 is :20
40. After Swapping
41. Value of num1 is :20
42. Value of num2 is :10
43. */
this program will swap number numbers.
Sunday, April 25, 2010
Data Types
Data types
1. Boolean
2. byte
3. char
4. double
5.float
6.int
7.short
next Lesson i will explain to you all data types. .
1. Boolean
2. byte
3. char
4. double
5.float
6.int
7.short
next Lesson i will explain to you all data types. .
Saturday, April 24, 2010
Break statement in java
Break statement is one of the several control statements Java provide to control the flow of the program. As the name says, Break Statement is generally used to break the loop of switch statement.
Please note that Java does not provide Go To statement like other programming languages e.g. C, C++.
This form of break statement is used to jump out of the loop when specific condition occurs. This form of break statement is also used in switch statement.
For example,
for(int var =0; var < 5 ; var++){
System.out.println("Var is : " + var);
if(var == 3)
break;
}
Labeled Break Statement
The unlabeled version of the break statement is used when we want to jump out of a single loop or single case in switch statement. Labeled version of the break statement is used when we want to jump out of nested or multiple loops.
For example,
Outer:
for(int var1=0; var1 < 5 ; var1++)
{
for(int var2 = 1; var2 < 5; var2++)
{
System.out.println("var1:" + var1 + ", var2:" + var2);
if(var1 == 3)
break Outer;
}
}
next lesson about continue statement
Please note that Java does not provide Go To statement like other programming languages e.g. C, C++.
This form of break statement is used to jump out of the loop when specific condition occurs. This form of break statement is also used in switch statement.
For example,
for(int var =0; var < 5 ; var++){
System.out.println("Var is : " + var);
if(var == 3)
break;
}
Labeled Break Statement
The unlabeled version of the break statement is used when we want to jump out of a single loop or single case in switch statement. Labeled version of the break statement is used when we want to jump out of nested or multiple loops.
For example,
Outer:
for(int var1=0; var1 < 5 ; var1++)
{
for(int var2 = 1; var2 < 5; var2++)
{
System.out.println("var1:" + var1 + ", var2:" + var2);
if(var1 == 3)
break Outer;
}
}
next lesson about continue statement
Friday, April 23, 2010
Factoring in java program
this program will show how to get the factorial using java program.
public class NumberFactorial {
public static void main(String[] args) {
int number = 5;
/*
* Factorial of any number is !n.
* For example, factorial of 4 is 4*3*2*1.
*/
int factorial = number;
for(int i =(number - 1); i > 1; i--)
{
factorial = factorial * i;
}
System.out.println("Factorial of a number is " + factorial);
}
}
public class NumberFactorial {
public static void main(String[] args) {
int number = 5;
/*
* Factorial of any number is !n.
* For example, factorial of 4 is 4*3*2*1.
*/
int factorial = number;
for(int i =(number - 1); i > 1; i--)
{
factorial = factorial * i;
}
System.out.println("Factorial of a number is " + factorial);
}
}
Thursday, April 22, 2010
Find Largest and Smallest Number in an Array using java
/*
Find Largest and Smallest Number in an Array Example
This Java Example shows how to find largest and smallest number in an
array.
*/
public class FindLargestSmallestNumber {
public static void main(String[] args) {
//array of 10 numbers
int numbers[] = new int[]{32,43,53,54,32,65,63,98,43,23};
//assign first element of an array to largest and smallest
int smallest = numbers[0];
int largetst = numbers[0];
for(int i=1; i< numbers.length; i++)
{
if(numbers[i] > largetst)
largetst = numbers[i];
else if (numbers[i] < smallest)
smallest = numbers[i];
}
System.out.println("Largest Number is : " + largetst);
System.out.println("Smallest Number is : " + smallest);
}
}
/*
Output of this program would be
Largest Number is : 98
Smallest Number is : 23
Find Largest and Smallest Number in an Array Example
This Java Example shows how to find largest and smallest number in an
array.
*/
public class FindLargestSmallestNumber {
public static void main(String[] args) {
//array of 10 numbers
int numbers[] = new int[]{32,43,53,54,32,65,63,98,43,23};
//assign first element of an array to largest and smallest
int smallest = numbers[0];
int largetst = numbers[0];
for(int i=1; i< numbers.length; i++)
{
if(numbers[i] > largetst)
largetst = numbers[i];
else if (numbers[i] < smallest)
smallest = numbers[i];
}
System.out.println("Largest Number is : " + largetst);
System.out.println("Smallest Number is : " + smallest);
}
}
/*
Output of this program would be
Largest Number is : 98
Smallest Number is : 23
Even or Odd program using java
This program will determine if the number inputted by the user is even or odd.
we use here a modulo operator to get the even or odd numbers.
Modulo - this operator use in programming to get the remainder.
sample program:
import java.util.Scanner;
public class EvenOdd
{
public static void main(String args[])
{
Scanner input=new Scanner(System.in);
int num;
System.out.print("Enter a number:");
num=input.nextInt();
if(num%2==0)
{
System.out.print("Even number");
}
else if(num%2==1)
{
System.out.print("Odd Numbers");
}
}
}
we use here a modulo operator to get the even or odd numbers.
Modulo - this operator use in programming to get the remainder.
sample program:
import java.util.Scanner;
public class EvenOdd
{
public static void main(String args[])
{
Scanner input=new Scanner(System.in);
int num;
System.out.print("Enter a number:");
num=input.nextInt();
if(num%2==0)
{
System.out.print("Even number");
}
else if(num%2==1)
{
System.out.print("Odd Numbers");
}
}
}
Labels:
add two numbers in java,
Even,
Modulo,
Odd
Wednesday, April 21, 2010
Find Largest and Smallest Number in an Array Example
1./*2. Find Largest and Smallest Number in an Array Example3.
This Java Example shows how to find largest and smallest number in an 4. array.5.*/
public class FindLargestSmallestNumber
{
public static void main(String[] args)
{
//array of 10 numbers11.
int numbers[] = new int[]{32,43,53,54,32,65,63,98,43,23};
//assign first element of an array to largest and smallest14.
int smallest = numbers[0];
int largetst = numbers[0];
for(int i=1; i< numbers.length; i++)
{
if(numbers[i] > largetst)
largetst = numbers[i];
else if (numbers[i] < smallest)
smallest = numbers[i];
}
System.out.println("Largest Number is : " + largetst);
System.out.println("Smallest Number is : " + smallest);
}
}
/*32.Output of this program would be33.
Largest Number is : 9834.
Smallest Number is : 2335.*/
This Java Example shows how to find largest and smallest number in an 4. array.5.*/
public class FindLargestSmallestNumber
{
public static void main(String[] args)
{
//array of 10 numbers11.
int numbers[] = new int[]{32,43,53,54,32,65,63,98,43,23};
//assign first element of an array to largest and smallest14.
int smallest = numbers[0];
int largetst = numbers[0];
for(int i=1; i< numbers.length; i++)
{
if(numbers[i] > largetst)
largetst = numbers[i];
else if (numbers[i] < smallest)
smallest = numbers[i];
}
System.out.println("Largest Number is : " + largetst);
System.out.println("Smallest Number is : " + smallest);
}
}
/*32.Output of this program would be33.
Largest Number is : 9834.
Smallest Number is : 2335.*/
Tuesday, April 20, 2010
Simple String program
import java.io.*;
public class javastring sample
{
public static void main(String args[])
{
/* string in java represents the character sequence */
/* create string empty*/
String s1=new String(" ");
/* create new string object whose content whose content whould be "Hello world"*/
String S2=new String("Hello World");
/* getting the length of the string */
System.out.print(s2.length());
}
}
public class javastring sample
{
public static void main(String args[])
{
/* string in java represents the character sequence */
/* create string empty*/
String s1=new String(" ");
/* create new string object whose content whose content whould be "Hello world"*/
String S2=new String("Hello World");
/* getting the length of the string */
System.out.print(s2.length());
}
}
Monday, April 19, 2010
String Manipulation
String manipulation using for loop.
sample program:
this program will show result asterisk triangle.
*
**
***
****
import java.io.*;
public class trian
{
public static void main(String args[])
{
int x=1;
int y=1;
for(x=1; x<4; y="1;">{
System.out.print("*");
}
System.out.print("\n");
}
}
}
sample program:
this program will show result asterisk triangle.
*
**
***
****
import java.io.*;
public class trian
{
public static void main(String args[])
{
int x=1;
int y=1;
for(x=1; x<4; y="1;">{
System.out.print("*");
}
System.out.print("\n");
}
}
}
Sunday, April 18, 2010
Compute two numbers in java using user input
this program will ask the user to input two numbers and get the sum.
import java.io.*;
import java.util.Scanner;
public class getsum
{
public static void main(String args[])
{
Scanner input=new Scanner(System.in);
int num1,num2;
int sum=0;
System.out.print("Enter First Number:");
num1=input.nextInt();
System.out.print("Enter Second Number:");
num2=input.nextInt();
sum=num1+num2;
System.out.print("The sum is:" + sum);
}
}
import java.io.*;
import java.util.Scanner;
public class getsum
{
public static void main(String args[])
{
Scanner input=new Scanner(System.in);
int num1,num2;
int sum=0;
System.out.print("Enter First Number:");
num1=input.nextInt();
System.out.print("Enter Second Number:");
num2=input.nextInt();
sum=num1+num2;
System.out.print("The sum is:" + sum);
}
}
user input in java
The program will ask the user to input name, address and the program will show what they input.
we use here the namespace which is the util.scanner.
User input in java using string;
name of java file: javainput
Sample program:
import java.util.scanner;
public class javainput
{
public static void main(String args[])
{
Scanner input=new Scanner(System.in);
//declaration
String name,address;
System.out.println("Enter your name:");
name=input.next();
System.out.println("Enter your name:");
address=input.next();
//displaying the result
System.out.print("Your Name:" + name);
System.out.print("Your Name:" + add);
}
}
String in Java
he String
class represents character strings. All string literals in Java programs, such as "abc"
, are implemented as instances of this class.
Strings are constant; their values cannot be changed after they are created. String buffers support mutable strings. Because String objects are immutable they can be shared. For example:
String str = "abc";
sample program.
import java.io.*;
public class stringsample
{
public static void main(String args[])
{
System.out.print("Hello World");
System.out.print("Welcome to my String Java program");
}
}
Labels:
display hello world,
hello world,
java,
String in java
Tuesday, April 13, 2010
while loop sample program
import java.io*;
public class loopwhile
{
public static void main(String args[])
{
int x=1;
while(x<=10)
{
System.out.print(x);
x++;
}
}
}
public class loopwhile
{
public static void main(String args[])
{
int x=1;
while(x<=10)
{
System.out.print(x);
x++;
}
}
}
Monday, April 12, 2010
while Loop
Looping is a part of programming which is the program executed continously until it reaches the
Sunday, April 4, 2010
If Statement
if statement is a conditional statement in programming. In this statement we are using the conditonal operators. this is true or false only. if the conditional statement is true the program execute the true statements, if the conditional statement is false the program execute the false statements.
sample program:
this program will show to you if the grade of students is greater than 75 the program will show message "Your passed" else "Sorry your Failed".
import java.io.*;
public class sample
{
public static void main(String args[])
{
int average=75;
if(average>=75)
{
System.out.print("Your Passed");
}
else
{
System.out.print("Your Failed");
}
}
}
sample program:
this program will show to you if the grade of students is greater than 75 the program will show message "Your passed" else "Sorry your Failed".
import java.io.*;
public class sample
{
public static void main(String args[])
{
int average=75;
if(average>=75)
{
System.out.print("Your Passed");
}
else
{
System.out.print("Your Failed");
}
}
}
Conditional Operators
Conditional operators are used in programming to perform condition in a program to execute statement.
1. (>) greater than
2. (<) Less than
3. (>=) Greater than equal
4. (<=) Less that equal
5. (==) Equal
6. (!=) not equal
next lesson is if statement.
1. (>) greater than
2. (<) Less than
3. (>=) Greater than equal
4. (<=) Less that equal
5. (==) Equal
6. (!=) not equal
next lesson is if statement.
Keywords in Java programming
The following keywords in java programming are:
abstract assert boolean break byte case catch char class
const continue default do double else enum extends final
finally float for goto if implements import instanceof
int interface long native new package private protected public
return short static strictfp super switch synchronized this throw
throws transient try void volatile while
it is used in java programming to implement a code and generate a function to have a correct statement.
abstract assert boolean break byte case catch char class
const continue default do double else enum extends final
finally float for goto if implements import instanceof
int interface long native new package private protected public
return short static strictfp super switch synchronized this throw
throws transient try void volatile while
it is used in java programming to implement a code and generate a function to have a correct statement.
Subscribe to:
Posts (Atom)