Java program to print alphabets
class Alphabets
{
public static void main(String args[])
{
char ch;
for( ch = 'a' ; ch <= 'z' ; ch++ )
System.out.println(ch);
}
}
Java while loop
import java.util.Scanner;
class WhileLoop
{
public static void main(String[] args)
{
int n;
Scanner input = new Scanner(System.in);
System.out.println("Input an integer");
while ((n = input.nextInt()) != 0)
{
System.out.println("You entered " + n);
System.out.println("Input an integer");
}
System.out.println("Out of loop");
}
}
Java while loop break program
import java.util.Scanner;
class BreakWhileLoop
{
public static void main(String[] args)
{
int n;
Scanner input = new Scanner(System.in);
while (true)
{
System.out.println("Input an integer");
n = input.nextInt();
if (n == 0)
{
break;
}
System.out.println("You entered " + n);
}
}
}
Java while loop break continue program
import java.util.Scanner;
class BreakContinueWhileLoop
{
public static void main(String[] args)
{
int n;
Scanner input = new Scanner(System.in);
while (true)
{
System.out.println("Input an integer");
n = input.nextInt();
if (n != 0)
{
System.out.println("You entered " + n);
continue;
}
else
{
break;
}
}
}
}
Java for loop
class Stars
{
public static void main(String[] args)
{
int row, numberOfStars;
for (row = 1; row <= 10; row++)
{
for(numberOfStars = 1; numberOfStars <= row; numberOfStars++)
{
System.out.print("*");
}
System.out.println();
}
}
}
Java if else program
import java.util.Scanner;
class IfElse
{
public static void main(String[] args)
{
int marksObtained, passingMarks;
passingMarks = 40;
Scanner input = new Scanner(System.in);
System.out.println("Input marks scored by you");
marksObtained = input.nextInt();
if (marksObtained >= passingMarks)
{
System.out.println("You passed the exam.");
}
else
{
System.out.println("Unfortunately you failed to pass the exam.");
}
}
}
Nested If Else statements
import java.util.Scanner;
class NestedIfElse
{
public static void main(String[] args)
{
int marksObtained, passingMarks;
char grade;
passingMarks = 40;
Scanner input = new Scanner(System.in);
System.out.println("Input marks scored by you");
marksObtained = input.nextInt();
if (marksObtained >= passingMarks)
{
if (marksObtained > 90)
grade = 'A';
else if (marksObtained > 75)
grade = 'B';
else if (marksObtained > 60)
grade = 'C';
else
grade = 'D';
System.out.println("You passed the exam and your grade is " + grade);
}
else
{
grade = 'F';
System.out.println("You failed and your grade is " + grade);
}
}
}
Swap 2 Numbers without using 3rd Variable
import java.util.*;
class Swap
{
public static void main(String args[])
{
int n1, n2, temp;
Scanner scan= new Scanner(System.in);
System.out.println("Please Enter No 1: ");
n1=scan.nextInt();
System.out.println("Please Enter No 2: ");
n2=scan.nextInt();
n1=n1+n2;
n2=n1-n2;
n1=n1-n2
System.out.println("First No: " + n1);
System.out.println("Second No: " + n2);
}
}
Swap 2 Numbers using 3rd Variable
import java.util.*;
class Swap
{
public static void main(String args[])
{
int n1, n2, temp;
Scanner scan= new Scanner(System.in);
System.out.println("Please Enter No 1: ");
n1=scan.nextInt();
System.out.println("Please Enter No 2: ");
n2=scan.nextInt();
temp=n1;
n1=n2;
n2=temp;
System.out.println("First No: " + n1);
System.out.println("Second No: " + n2);
}
}
No comments:
Post a Comment