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);
}
}
Friday, April 23, 2010
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");
}
}
}
Subscribe to:
Posts (Atom)
