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.*/
Wednesday, April 21, 2010
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
Subscribe to:
Posts (Atom)
