You are on page 1of 17

Java Programming

Lab 3
Harsh Singh
CSE – 2
210351

Que. 1 Print natural numbers in reverse: Given a number N, print natural numbers in reverse from

N to 1.

#CODE

import java.util.Scanner;

public class Main {

private static Scanner sc;

public static void main(String[] args)

int number, i;

sc = new Scanner(System.in);

System.out.println("Enter the value to be printed in the reverse form : ");

number = sc.nextInt();

i = number;

while(i >= 1)

System.out.print(i +" ");


i--;

#OUTPUT

Que. 2 Arithmetic Progression (A.P.): Given three integers N1 and N2 and N3, find the sum of an
Arithmetic Progression ( A.P. ).Input:1,5,3where:First line represents the value of N1, which is the
first number of A.P. Second line represents the value of N2 which is total number sin the A.P Third
line represents the value of N3 which is a common difference of the A.P. Output:35Explanation: An
A.P. is a sequence of numbers, where the difference between the consecutive terms s constant.
Here N1 represents the start point, N2 is the number of elements in A.P. and N3 represents the
common difference between consecutive terms.

#CODE

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);

System.out.println("The sum of given AP Numbers");

System.out.print("Enter the first number of the AP = ");


int N1= sc.nextInt();

System.out.print("Enter the total number in AP, n= ");

int N2=sc.nextInt();

System.out.print("Enter the common difference ,d= ");

int N3= sc.nextInt();

int sum=0;

if(N2==0){

System.out.println("Please enter atleast one number for n ");

}else{

System.out.print("The sum of the AP is ");

for(int i=0;i<N2;i++){

sum+=N1;

N1+=N3;

System.out.println(sum);

#OUTPUT
Que. 3 Find Factors: Given numbers N, find all factors of  it. Input: 12 

where: First line represents value of N.


Output: 1 2 3 4 6 12 
Assumptions: 
N can be in the range 1 to 100000.

#CODE

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Input the number of which you want to find the prime factor");

int n = sc.nextInt();

if (n == 0) {

System.out.println("the fator of " + n + " 0");

} else {

System.out.println("the factor of " + n + " = ");

for (int i = 1; i <= n; i++) {

if (n % i == 0) {

System.out.print(i+" ");

}
#OUTPUT

Que. 4 Perfect Squares Between Two Integers: Given two numbers representing a range of
integers,  count the number of perfect squares that exist between them, the two integers inclusive.

Input:4 17 
Output 3 

#CODE

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);

System.out.println("Enter the starting number ");

int n1= sc.nextInt();

System.out.println("enter the last number ");

int n2=sc.nextInt();

int sum=0;

for(int i=n1;i<=n2;i++){
for(int j=1;j*j<=i;j++){

if(j*j==i){

sum++;

System.out.println("the total perfect square between "+n1+" and "+n2 +" =");

System.out.println(sum);

#OUTPUT
Que. 5 Automorphic Number: Given an integer, check whether it is automorphic or not. 
Automorphic number: An automorphic number is a number which is contained in the last digit(s)
of  its square. e.g. 25 is an automorphic number as its square is 625 and 25 presents as the last two 
digits. 5, 6 and 75 are also automorphic. 
Display true if the number is automorphic otherwise  
false. Input 25 
Output 
True  
Assume  
that, 
The integer ranges from [0 to  
2,147,483,647].  

#CODE

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);

System.out.println("Enter the number");

int n= sc.nextInt();

int sq=n*n;

while(n>0){

if(n%10!=sq%10){

System.out.println("false");

n/=10;

sq/=10;

return;

}else{

System.out.println("true");

return;

}
}

#OUTPUT

Que. 6 Convert an octal number into binary: Given an octal number N, convert it into a
binary  number. 
Input:
5 7where: First line represents an octal  number N. Output: 101111

#CODE

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

String binary=" ";

Scanner sc=new Scanner(System.in);

System.out.println("Enter the Octal number: ");

String octal= sc.nextLine();

for(int i=0;i<octal.length();i++){

char ch=octal.charAt((i));
switch (ch){

case'0':binary=binary+"000";

break;

case '1':binary=binary+"001";

break;

case'2':binary=binary+"010";

break;

case '3':binary=binary+"011";

break;

case'4':binary=binary+"100";

break;

case '5':binary=binary+"101";

break;

case'6':binary=binary+"110";

break;

case '7':binary=binary+"111";

break;

default:

System.out.println("\n Invalid Octal Digital!");

return;

System.out.println("\n Equivalent binar value is :"+ binary);

}
#OUTPUT

Que. 7 Display Fibonacci Series: The Fibonacci series is a series where the next term is the sum of 
pervious two terms. The first two terms of the Fibonacci sequence is 0 followed by 1. Inpu 
t :  
10  
Outp 
ut: 
The Fibonacci sequence: 0 + 1 + 1 + 2 + 3 + 5 + 8 + 13 + 21 + 34+ 

#CODE

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);

System.out.println("Enter the number till you want to print series");

int n= sc.nextInt();

int n1=0,n2=1,n3;

System.out.print(n1+" + "+ n2);

for(int i=2;i<n;i++){
n3=n1+n2;

System.out.print(" + "+n3);

n1=n2;

n2=n3;

System.out.print(" +");

#OUTPUT
Que. 8 Armstrong Number: Given an integer N, find whether it is Armstrong number or not. 
Display 1 if it is an Armstrong number else 0. 
Input: 153 where: 
First line represents input  
number N. Output: 1 
Explanation: For 153 number of digits are 3. Sum of digits raises to the power total number of
digits  = 1  + 5 + 3 = 153, which is equal to an original number and hence 153 is an Armstrong 
3 3 3

number.  

#CODE

import java.util.Scanner;

import java.lang.Math;

public class Main {

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);

System.out.println("1 -> Armstrong Number");

System.out.println("0 -> Non Armstrong Number");

System.out.println("Enter the number");

int n=sc.nextInt();

int result=0,on;

on=n;

while(on!=0){

int r=on%10;

result+=Math.pow(r,3);

on=on/10;

if(result==n){

System.out.println("1");

}else{
System.out.println(" ");

#OUTPUT

Que. 9 Number Pattern -Alternate 1 and 0 in columns: Given two integers N1 and N2,
display  the number pattern of 1's and 0's at alternate columns. Input 4 5 
where: First line represents the value of N1( number of rows ).  Second line represents the value of
N2( number of columns ). Output: 
0 1 0 1 0 
0 1 0 1 0 
0 1 0 1 0 
0 1 0 1 0 

#CODE

import java.util.Scanner;

public class Main {


public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter the number of the rows");

int n1= sc.nextInt();

System.out.println("Enter the number of the columns");

int n2= sc.nextInt();

for(int i=0;i<n1;i++){

for(int j=0;j<n2;j++){

if(j%2==0){

System.out.print(0+" ");

}else{

System.out.print(1+" ");

System.out.println();

#OUTPUT
Que. 10 Hollow Rectangle star pattern: Given an integer N1 and N2, print Rectangle Star  pattern
as described in output. 
Input: 


Where: First line represents the value of N1.
• Second line represents the value of N2. 

#CODE

import java.util.Scanner;

public class Main {

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);

System.out.println("Enter the value of N1");

int N1=sc.nextInt();

System.out.println("Enter the Value of N2");

int N2=sc.nextInt();

for(int i=1;i<=N1;i++){

for(int j=1;j<=N2;j++) {

if (N1 > 1000 * N2) {

System.out.println("error");

return;
} else {

if (i == 1 || i == N1 || j == 1 || j == N2) {

System.out.print("*");

} else {

System.out.print(" ");

System.out.println();

#OUTPUT

You might also like