Professional Documents
Culture Documents
Day 1 DSA Lab Practice Programs
Day 1 DSA Lab Practice Programs
Bengaluru, Karnataka
Computer Science & Engineering
School of Computer Science & Engineering
Subject: CSE2001 - Data Structures Algorithms (Day1 Lab Practice Programs) / Semester: III
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);
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;
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