0% found this document useful (0 votes)
39 views5 pages

Java Lab 2

The document contains 14 programming exercises on loops and patterns in Java. It introduces while, do-while, and for loops, and provides examples to print numbers, check for palindromes, calculate sums of digits, and print various patterns using nested loops. It also covers taking input from the keyboard and includes exercises to find the largest of three numbers, calculate a square root, and get the sum of digits of a given number by accepting input. The final exercises are to print a multiplication table for a given number and implement a basic calculator using switch statements.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
39 views5 pages

Java Lab 2

The document contains 14 programming exercises on loops and patterns in Java. It introduces while, do-while, and for loops, and provides examples to print numbers, check for palindromes, calculate sums of digits, and print various patterns using nested loops. It also covers taking input from the keyboard and includes exercises to find the largest of three numbers, calculate a square root, and get the sum of digits of a given number by accepting input. The final exercises are to print a multiplication table for a given number and implement a basic calculator using switch statements.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 5

Omar Mukhtar University Faculty of Engineering

Dept. of Computer Engineering


Albeida , Libya
Instructor: Mrs. Krishna Kumari Ganga

Advanced Programming 1: Lab sheet 2

Loops

1. program to print numbers from 1 to 9


class NumDisplay {
public static void main(String arg[]) {
System.out.println( “1”);
System.out.println( “2”);
System.out.println( “3”);
System.out.println( “4”);
System.out.println( “5”);
System.out.println( “6”);
System.out.println( “7”);
System.out.println( “8”);
System.out.println( “9”);
}
}
2. program to print numbers from 1 to 9 using while loop
class WhileLoop {
public static void main(String arg[]) {
int i=1;
while(i<10) {
System.out.println( i);
i++;
}
}
}
3. program to print numbers from 1 to 9 using do while loop
class DoLoop {
public static void main(String arg[]) {
int i=15;
do {
System.out.println( i);
i++;
} while(i<10);
}
}
Note: The difference between while loop and do while loop is the while loop executes the statements only when
the condition is satisfied. But do while executes the statements for the first time before checking the condition
4. Program to check whether the number is palindrome or not using while loop
Palindrome: A number which will be the same if we read backward of forward
Ex: 121, 34543, 26862
class palind {
public static void main(String a[ ]) {
int n,temp, m, rev = 0;
n = 2335332;
temp = n;
while( temp > 0 ) {
m = temp % 10;
rev = ( rev * 10 ) + m;
temp = temp / 10;
}
if( rev == n )
System.out.println("The number "+ n + " is a palindrome" );

Advanced Programming Using Java - Prepared by: Krishna Kumari Ganga


else
System.out.println("The number "+ n + " is not a palindrome" );
}
}
5. Program to Find the Sum of the Digits of a Number using while loop
class DigitAdd {
public static void main(String args[]) {
int temp,sum=0;
int num=12345;
System.out.println("The number is:" +num);
while ( num > 0 ) {
temp = num % 10;
num = num / 10;
sum = sum + temp;
}
System.out.println("The sum of the digits of the above number is:" +sum);
}
}
6. program to print numbers from 1 to 9 using for loop
class ForLoop {
public static void main(String arg[]) {
//int i=1;
//while(i<10)
for(int i=1;i<10;i++) {
System.out.println( i);
//i++;
}
}
}
7. program to print the below pattern
* * * *
class ForLoop1 {
public static void main(String arg[]) {
for(int i=1;i<=4;i++) {
System.out.print( “* ”);
}
System.out.println( );
}
}
8. program to print the below pattern
* * * *
* * * *
* * * *
* * * *
class ForLoop2 {
public static void main(String arg[]) {
for(int i=1;i<=4;i++) {
System.out.print( “*”);
}
System.out.println( );
for(int i=1;i<=5;i++) {
System.out.print( “*”);
}
System.out.println( );
for(int i=1;i<=5;i++) {
System.out.print( “*”);
}

Advanced Programming Using Java - Prepared by: Krishna Kumari Ganga


System.out.println( );
for(int i=1;i<=4;i++) {
System.out.print( “* ”);
}
System.out.println( );

}
}

9. program to print the below pattern


* * * *
* * * *
* * * *
* * * *
class ForLoop3
{
public static void main(String arg[])
{
For(int i=1;i<=4;i++)
{ //nested for loop
for(int j=1;j<=4;j++)
{
System.out.print( “*”);
}
System.out.println( );
}
}
}

10. program to print the below pattern


*
* *
* * *
* * * *
class ForLoop4 {
public static void main(String arg[]) {
for(int i=1;i<=4;i++) {
for(int j=1;j<=i; j++) {
System.out.print( “*”);
}
System.out.println( );
}
}
}

11. program to print the below pattern


1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
class ForLoop5 {
public static void main(String arg[]) {
for(int i=1;i<=4;i++) {
for(int j=1;j<=4;j++) {
System.out.print( j+ “ “);
}
System.out.println( );

Advanced Programming Using Java - Prepared by: Krishna Kumari Ganga


}
}
}

12. program to print the below pattern


1 2 3 4
2 3 4 1
3 4 1 2
4 1 2 3
class ForLoop6 {
public static void main(String arg[]) {
for(int i=1;i<=4;i++) {
for(int j=1;j<=4;j++) {
int k= i+j-1;
if (k>4)
System.out.print( k-4+” “);
else
System.out.print( k+” “);
}
System.out.println( );
}
}
}
13. Write a program to display the following pattern by using for loops?
* * * *
* * *
* *
*
14. Write a program to display the following pattern by using for loops
* * * * *
* *
* *
* *
* * * * *

Advanced Programming Using Java - Prepared by: Krishna Kumari Ganga


Taking Input from the Keyboard

1. Program to find the biggest of the three numbers by taking the input from keyboard
public class Biggest {
public static void main(String a[])throws IOException {
DataInputStream dts=new DataInputStream(System.in);
System.out.println("Enter the first number");
int x=Integer.parseInt(dts. readLine());
System.out.println("Enter the second number");
int y=Integer.parseInt(dts. readLine());
System.out.println("Enter the third number");
int z=Integer.parseInt(dts. readLine());
if (x>y && x>z)
System.out.println("The biggest number is "+x);
else if (y>z && y>x)
System.out.println("The biggest number is "+y);
else
System.out.println("The biggest number is "+z);

}
2. Program to find the square root of a number by taking the input from keyboard
class Sqrt {
public static void main(String a[])throws IOException {
DataInputStream dts=new DataInputStream(System.in);
System.out.println("enter number to find squaroot");
int m=Integer.parseInt(dts. readLine());
System.out.println("square root of "+ m + " = "+Math.sqrt(m));
}
}
3. Program to find the sum of digits of a given number by taking the input from keyboard
class DigitAdd1{
public static void main(String args[])throws IOException{
int temp,sum=0;
DataInputStream dts=new DataInputStream(System.in);
System.out.println("enter number to get the sum of digits");
int num=Integer.parseInt(dts. readLine());
System.out.println("The number is:" +num);
while ( num > 0 ){
temp = num % 10;
num = num / 10;
sum = sum + temp;
}
System.out.println("The sum of the digits of the above number is:" +sum);
}
}

4. Write a program to print the multiplication table of a given number?

5. Write a program to implement the Basic Functionality of a Calculator using Switch?

Best of luck

Advanced Programming Using Java - Prepared by: Krishna Kumari Ganga

You might also like