100% found this document useful (1 vote)
871 views14 pages

Java Programming Lab Exercises

This document contains 12 Java programs assigned as practice problems for a data structures and algorithms lab course. The programs cover basic concepts like Hello World, arithmetic operations, user input, arrays, conditionals, loops, and more. The programs are accompanied by sample outputs. Assignment questions at the end ask students to write additional programs involving 2D arrays, Armstrong numbers, reversing numbers, factorials, and Fibonacci series.

Uploaded by

Rohini Aravindan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
871 views14 pages

Java Programming Lab Exercises

This document contains 12 Java programs assigned as practice problems for a data structures and algorithms lab course. The programs cover basic concepts like Hello World, arithmetic operations, user input, arrays, conditionals, loops, and more. The programs are accompanied by sample outputs. Assignment questions at the end ask students to write additional programs involving 2D arrays, Armstrong numbers, reversing numbers, factorials, and Fibonacci series.

Uploaded by

Rohini Aravindan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

PRESIDENCY UNIVERSITY

Bengaluru, Karnataka
Computer Science & Engineering
School of Computer Science & Engineering

Subject: CSE2001 - Data Structures Algorithms (Day1 Lab Practice Programs) / Semester: III

1. Java Program to print Hello World


public class hello_world
{
public static void main(String args[])
{
System.out.println("Hello World");
}
}
Output:

\2. Java Program to add 2 numbers:


public class Add
{
public static void main(String args[])
{
int a=10, b=20;
int c;
c=a+b;
System.out.println("Sum of two numbers="+c);
}
}
Prepared by,
Ms. Sridevi S, AP/SoCSE, Ms. Meena Kumari, AP/SoCSE, Ms. Rohini A, AP/SoCSE 1
Output:

3. Java Program to read input from user:


import java.util.*;
public class Add_userinput
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter any integer value");
int a=sc.nextInt();
System.out.println("The entered input="+a);
}
}

Output:

Prepared by,
Ms. Sridevi S, AP/SoCSE, Ms. Meena Kumari, AP/SoCSE, Ms. Rohini A, AP/SoCSE 2
4. Java Program to add two numbers
import java.util.*;
public class Add_2nos
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);

System.out.println("Enter 1st integer number");


int a=sc.nextInt();

System.out.println("Enter 2nd integer number");


int b=sc.nextInt();

int c=a+b;
System.out.println("The sum of 2 numbers="+c);
}
}
Output:

Prepared by,
Ms. Sridevi S, AP/SoCSE, Ms. Meena Kumari, AP/SoCSE, Ms. Rohini A, AP/SoCSE 3
5. Java Program to print first 10 natural numbers:
public class natural
{
public static void main(String args[])
{
int i=1;
while(i<=10)
{
System.out.println(i);
i=++i;
}
}
}

Output:

Prepared by,
Ms. Sridevi S, AP/SoCSE, Ms. Meena Kumari, AP/SoCSE, Ms. Rohini A, AP/SoCSE 4
6. Write a java code to print sum of natural numbers
public class sum_of_natural_nos
{
public static void main(String args[])
{
int i=1, sum=0;
while(i<=10)
{
sum=sum+i;
i=i+1;
}
System.out.println("The sum of first 10 natural numbers="+sum);
}

}
Output:

Prepared by,
Ms. Sridevi S, AP/SoCSE, Ms. Meena Kumari, AP/SoCSE, Ms. Rohini A, AP/SoCSE 5
7. Write a java code to read and print array elements(1D array):
import java.util.*;
public class array_elements
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int a[]=new int[10];
int i=1;
System.out.println("Enter the size of array");
int n=sc.nextInt();
for(i=1;i<=n;i++)
{
a[i]=sc.nextInt();
}
System.out.println("The array elements are:");
for(i=1;i<=n;i++)
{
System.out.println(a[i]);
}
}
}
Output:

Prepared by,
Ms. Sridevi S, AP/SoCSE, Ms. Meena Kumari, AP/SoCSE, Ms. Rohini A, AP/SoCSE 6
Prepared by,
Ms. Sridevi S, AP/SoCSE, Ms. Meena Kumari, AP/SoCSE, Ms. Rohini A, AP/SoCSE 7
8. Write a java code to initialise array elements:
public class array_elements
{
public static void main(String args[])
{
int a[]={1,2,3,4};
int i;

System.out.println("The array elements are:");


for(i=0;i<4;i++)
{
System.out.println(a[i]);
}
}
}
Output:

Prepared by,
Ms. Sridevi S, AP/SoCSE, Ms. Meena Kumari, AP/SoCSE, Ms. Rohini A, AP/SoCSE 8
Prepared by,
Ms. Sridevi S, AP/SoCSE, Ms. Meena Kumari, AP/SoCSE, Ms. Rohini A, AP/SoCSE 9
9. Write a java code to find greatest of 3 numbers:
import java.util.*;
public class greatest_three
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int a,b,c;
System.out.println("Enter any three integer numbers");
a=sc.nextInt();
b=sc.nextInt();
c=sc.nextInt();
if(a>b && a>c)
{
System.out.println("A is greater");
}
else if(b>c)
{
System.out.println("B is greater");
}
else
{
System.out.println("C is greater");
}
}
}

Prepared by,
Ms. Sridevi S, AP/SoCSE, Ms. Meena Kumari, AP/SoCSE, Ms. Rohini A, AP/SoCSE 10
Output:

Prepared by,
Ms. Sridevi S, AP/SoCSE, Ms. Meena Kumari, AP/SoCSE, Ms. Rohini A, AP/SoCSE 11
10. Write a Java Program to check a given number is even or odd

Output:

Prepared by,
Ms. Sridevi S, AP/SoCSE, Ms. Meena Kumari, AP/SoCSE, Ms. Rohini A, AP/SoCSE 12
11. Write a Java Program to check a number is integer or not

Output:

Prepared by,
Ms. Sridevi S, AP/SoCSE, Ms. Meena Kumari, AP/SoCSE, Ms. Rohini A, AP/SoCSE 13
12. Write a Java Program to check a year is leap or not

Output

Assignment Questions:
1. Write a Java code to read and print 2D array elements.
2. Write a Java code to check a number is Armstrong number.
3. Write a Java code to print given number in reverse order.
4. Write a Java code to find factorial of a number.
5. Write a Java code to find first ‘n’ Fibonacci series.

Prepared by,
Ms. Sridevi S, AP/SoCSE, Ms. Meena Kumari, AP/SoCSE, Ms. Rohini A, AP/SoCSE 14

You might also like