java basics1
java basics1
First program:
}
}
We can also use escape sequences for output.
For example:
System.out.println( "Welcome \n to \n Java \n Programming!" );
import java.util.Scanner;
public class Addition
{
public static void main( String[] args ){
Scanner input = new Scanner( System.in );
int number1; // first number to add
int number2; // second number to add
int sum;
System.out.print( "Enter first integer: " );
number1 = input.nextInt();
System.out.print( "Enter second integer: " );
number2 = input.nextInt();
sum = number1 + number2;
System.out.printf( "Sum is %d\n", sum );
}
}
Program:
import java.util.Scanner;
public class Product
{
public static void main( String[] args )
{
/ create Scanner to obtain input from command window
Scanner input = new Scanner( System.in );
int x;
int y;
int z;
int result;
System.out.print( "Enter first integer: " ); // prompt for input
x = input.nextInt(); // read first integer
System.out.print( "Enter second integer: " ); // prompt for input
y = input.nextInt(); // read second integer
System.out.print( "Enter third integer: " ); // prompt for input
z = input.nextInt(); // read third integer
result = x * y * z;
System.out.printf( "Product is %d\n", result );
}
}
}
}
Exercise:
Q. 1: Write a Java program to get temperature from the user in Fahrenheit and print into Celsius using
formula C=5/9(F-32).
Q. 2: Write a Java program which takes radius of circle and prints its area and circumference of circle
using
formula. Area = πR2 Cir = 2πR
Q. 3: Write a Java program which takes a 4-digit number and print the sum, product of its digits.
Q. 4: Write a Java program that input two times in hh:mm:ss format, adds both times and displays total
time.
Q. 5: Write a Java program that input into pounds. Convert and print into kilograms.
Conditional Statements:
Program:
}
}
Nested If:
Program:
if(a>b)
if(a>c)
System.out.println(a + "is Greatest”);
else
System.out.println(c + "is Greatest”);
else
if(b>c)
System.out.println(b + "is Greatest”);
else
System.out.println(c + "is Greatest”);
}
}
Program:
import java.util.Scanner;
public class Compare{
public static void main(String[] args){
Scanner input = new Scanner( System.in );
char ch;
ch= input.next().charAt(0);
}
}
Conditional Statements:
Syntax:
logical_expression ? expression1 : expression2;
Switch statement:
import java.util.Scanner;
public class Compare{
public static void main(String[] args){
Scanner input = new Scanner( System.in );
char op;
int n1,n2;
System.out.println("Enter an Integer");
n1=input.nextInt();
System.out.println("Enter an operator");
op= input.next().charAt(0);
System.out.println("Enter an Integer");
n2=input.nextInt();
switch (op){
case ‘+’:
System.out.println(n1 + "+" + n2 + ”=” + n1+n2 );
break;
case ‘-’:
System.out.println(n1 + "-" + n2 + ”=” + n1-n2 );
break;
case ‘*’:
System.out.println(n1 + "*" + n2 + ”=” + n1*n2 );
break;
case ‘/’:
System.out.println(n1 + "/" + n2 + ”=” + n1/n2 );
break;
case ‘%’:
System.out.println(n1 + "%" + n2 + ”=” + n1%n2 );
break;
default:
System.out.println( "Invalid Operator" );
}
}
}
Exercise:
Q.1: Write a Java program that contains if statement that may be used to compute the area of a square
(area=side*side) or triangle (area=0.5*base*height) after prompting the user to type the first character
of the figure name(S or T).
Q.2: Write a Java program that convert MILITARY time to STANDARD time.
Q.3: Write a Java program which takes three numbers in a, b and c from user. If a is not zero, find out
whether it is common divisor of b and c.
Q. 4: Write a Java program which gets a character and print is it vowel or consonant using switch
statement.
Q. 5: Write a Java program which takes year and print is it leap year or not.
Loops:
While loop:
public class Hello{
public static void main(String[] args){
int n=1;
while(n<=5){
System.out.println("n=" + n);
n++;
}
}
}
Do-while loop:
import java.util.Scanner;
public class Compare{
public static void main(String[] args){
Scanner input = new Scanner( System.in );
int tab, c;
System.out.println("Enter Table Number");
tab=input.nextInt();
c = 1;
do{
System.out.println(tab + "*" + c + “=” + tab*c );
c++;
}while(c<=10);
}
}
For loop:
import java.util.Scanner;
public class Compare{
public static void main(String[] args){
Scanner input = new Scanner( System.in );
int n, c , f ;
f = 1;
System.out.println("Enter Number for factorial");
n=input.nextInt();
for( c=1 ; c<=n ; c++)
f = f * c;
System.out.println( "Factorial=" +f );
}
}
Exercise:
Q.1: Write a Java program that takes two numbers and displays the result of first number raise to the
power of second number.
Q.2: Write a Java program that gets a number and print whether it is palindrome or not.
Q.3: Write a Java program which get a number and print whether it is a prime or not.
Q. 4: Write a Java program which takes two numbers and displays the GCD of both numbers.
Q. 5: Write a Java program which displays the sum of following series.
1 + 2x + 3x2 + 4x3 + 5x4
*
Q. 6: Write a Java program which prints the following output.
* *
* *
*
*
*
*
*
Arrays: *
* *
public class Compare{
public static void main(String[] args){
Scanner input = new Scanner( System.in );
int n, i ;
int[] arr; // declare array as name arr
arr = new int[5]; //create array object
Two-dimensional array:
public class Compare{
public static void main(String[] args){
Scanner input = new Scanner( System.in );
int r, c ;
int[][] arr; // declare array as name arr
arr = new int[5][5]; //create array object
Exercise:
Q.1: Write a Java program that takes ten numbers in array. It displays the numbers that are greater than
the average value of the array.
Q.2: Write a Java program that gets ten random numbers in array and displays array after sorting.
Q.3: Write a Java program in which declare a 2-dimensional array of 5 row and 5 columns. Input values
and displays the minimum and maximum value from array.