Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Lab Exercise 1 – Feb 2021

Name : Jeel Mehta


Reg. No. : 20BCE1117
Programme : BTech Semester : WIN 2021-22
Code : CSE1007
Course Title : Java Programming Class Nbr(s) : CH2021225000753

Faculty(s) : Dr PARKAVI K Slot L15+L16


Date : 21/01/2022

Introduction to Java and Simple Java Programming

Mar
Q.No. Question Description
ks
1 Write a simple Java program which will print Fibonacci series, e.g. 1 1 2 3 5 8 13 ... . up to a
2
given number.
Write a Java program to check if given Integer is palindrome or not. An integer is called
palindrome if it's equal to its reverse, e.g. 1001 is a palindrome, but 1234 is not because the
2 2
reverse of 1234 is 4321 which is not equal to 1234. You can use divide by 10 to reduce the
number and modulus 10 to get the last digit.

Finding a year is a leap or not is a bit tricky. We generally assume that if a year number is
evenly divisible by 4 it is a leap year. But it is not the case all time. 2100 and 2200 is divisible
by 4 yet not a leap year. Even if a year is evenly divided by 100 we cannot assume it is not
leap. For e.g., 1600 and 2000 are all leap year even though they are divisible by 100. So the
3 condition is as below 2
S.No Condition Leap or not Example
1. Divided by 4 Leap( if condition 2 is not true) 1988, 1996
2. Divided by 100 Not Leap( if condition 3 is not true) 2100, 2200
3. Divided by 400 Leap 1600, 2000
A number is called an Armstrong number if it is equal to the cube of its every digit. For
example, 153 is an Armstrong number because of 153= 1+ 125+27, which is equal to
4 1^3+5^3+3^3. You need to write a program to check if the given number is Armstrong 2
number or not.

Write a Java program to check if a given number is prime or not. Remember, a prime number
is a number which is not divisible by any other number, e.g. 3, 5, 7, 11, 13, 17, etc.
5 Enhance your algorithm without looping through all the numbers. 2
Hint: checking till the square root of a number, etc.

Total 10

Page 1 of 5
1.

Code
import java.util.*;
public class Main {
public static void main(String[] args) {
int n=0;
System.out.println("Enter the number of terms:");
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
int firstTerm = 0, secondTerm = 1;
System.out.println("Fibonacci Series till " + n + " terms:");
for (int i = 1; i <= n; ++i) {
System.out.print(firstTerm + ", ");
int nextTerm = firstTerm + secondTerm;
firstTerm = secondTerm;
secondTerm = nextTerm;
}
}
}

OUTPUT :

CODE
import java.util.*;
public class Main {
public static void main(String args[]){
int r,sum=0,temp;
int n=0;
System.out.println("Enter the number to be checked:");
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
Page 2 of 5
temp=n;
while(n>0){
r=n%10;
sum=(sum*10)+r;
n=n/10;
}
if(temp==sum)
System.out.println("Palindrome Number");
else
System.out.println("Not Palindrome");
}
}

OUTPUT:

3.

Code

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int year=0;
System.out.println("Enter the year to be checked:");
Scanner sc = new Scanner(System.in);
year = sc.nextInt();
boolean leap = false;
if (year % 4 == 0) {
if (year % 100 == 0) {
if (year % 400 == 0)
leap = true;
else
leap = false;
}
else
leap = true;
}
else
leap = false;
if (leap)
System.out.println(year + " is a leap year.");
Page 3 of 5
else
System.out.println(year + " is not a leap year.");
}
}

OUTPUT :

4.

Code
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int originalNumber, remainder, result = 0;
int number=0;
System.out.println("Enter the number to be checked:");
Scanner sc = new Scanner(System.in);
number = sc.nextInt();
originalNumber = number;
while (originalNumber != 0)
{
remainder = originalNumber % 10;
result += Math.pow(remainder, 3);
originalNumber /= 10;
}
if(result == number)
System.out.println(number + " is an Armstrong number.");
else
System.out.println(number + " is not an Armstrong number.");
}
}

OUTPUT :

Page 4 of 5
5.

CODE

import java.util.Scanner;
public class Main {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int number = Integer.MAX_VALUE;
System.out.println("Enter number to check if prime or not ");
while (number != 0) { number = sc.nextInt();
System.out.printf("Is %d prime? %s ", number, isPrime(number));
}
}
public static boolean isPrime(int number) {
int sqrt = (int) Math.sqrt(number) + 1;
for (int i = 2; i < sqrt; i++) {
if (number % i == 0) {
return false;
}
}
return true;
}
}

OUTPUT :

Page 5 of 5

You might also like