Professional Documents
Culture Documents
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;
int number, i;
sc = new Scanner(System.in);
number = sc.nextInt();
i = number;
while(i >= 1)
#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;
int N2=sc.nextInt();
int sum=0;
if(N2==0){
}else{
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
#CODE
import java.util.Scanner;
System.out.println("Input the number of which you want to find the prime factor");
int n = sc.nextInt();
if (n == 0) {
} else {
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;
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;
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;
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:
return;
}
#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;
int n= sc.nextInt();
int n1=0,n2=1,n3;
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;
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;
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:
6
4
Where: First line represents the value of N1.
• Second line represents the value of N2.
#CODE
import java.util.Scanner;
int N1=sc.nextInt();
int N2=sc.nextInt();
for(int i=1;i<=N1;i++){
for(int j=1;j<=N2;j++) {
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