Day 5 CW
Day 5 CW
Email: [email protected]
Roll no: 727723EUCS242
Phone: 8778017360
Branch: SKCET
Department: CSE-D
Batch: 2023_27
Degree: BE-CSE
2023/22_27_II/IV_Java Programming_IRC
IRC_Java_D5_Java Looping2_CE_COD1
Attempt : 1
Total Mark : 100
Marks Obtained : 100
Section 1 : CODING
1. Problem statement
Write a program to print x pattern of the given odd length string. It should
contain two methods namely main and pattern. The main method should
pass the inputs to the pattern.
Refer sample output for formatting specifications.
Answer
import java.util.Scanner;
class Main
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
String s=in.nextLine();
int a=s.length();
for(int i=1;i<=a;i++)
{
for(int j=1;j<=a;j++)
{
if(i==j)
{
System.out.print(s.charAt(i-1));
}
else if(i+j==a+1)
{
System.out.print(s.charAt(j-1));
}
else
{
System.out.print(" ");
}
}
System.out.println();
}
}
}
2. Problem statement
Write a program to print the following pattern.
Example:
input: 4
output:
1
01
101
0101
Answer
import java.util.Scanner;
class Main
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int n=in.nextInt();
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(j<=i)
{
if(i%2!=0)
{
if(j%2==0)
System.out.print(0+" ");
else
System.out.print(1+" ");
}
else
{
if(i%2==0&&j%2==0)
{
System.out.print(1+" ");
}
else
{
System.out.print(0+" ");
}
}
}
}
System.out.println();
}
}
}
Answer
import java.util.Scanner;
class Main
{
public static void main(String[]args)
{
Scanner in=new Scanner(System.in);
int n=in.nextInt();
int i,j;
int k=n*2-1;
for(i=1;i<=k;i++)
{
for(j=1;j<=k;j++)
{
if(j==i)
System.out.print(i);
else if(j==k)
System.out.print(i);
else
System.out.print(" ");
if(j<=k)
{
System.out.print(" ");
}
}
k--;
System.out.println();
}
k+=2;
for(i=n-1;i>=1;i--)
{
for(j=1;j<=k;j++)
{
if(j==i)
{
System.out.print(i);
}
else if(j==k)
{
System.out.print(i);
}
else
{
System.out.print(" ");
}
if(j<=k)
{
System.out.print(" ");
}
}
k++;
System.out.println();}}}
4. Problem statement
Hari is a civil engineer who is designing a fountain in square shape with
water sprinklers in the edges with n number of steps. He needs to draw a
sketch of the fountain in top view with the plot of ' * ' at the edges of the
square and the sides of the outer square.
Write a program to help him in printing the pattern with n number of steps.
Answer
import java.util.Scanner;
class Main
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int a=in.nextInt();
int n=(a*2)-1;
for(int i=1;i<=n;i++)
{
for(int j =1;j<=n;j++)
{
if(i==1||i==n||j==1||j==n)
{
System.out.print(" * ");
}
else if(i==j||i+j==n+1)
{
System.out.print(" * ");
}
else
{
System.out.print(" ");
}
}
System.out.println();
}
}
}
5. Problem statement
Deepa was ready to make a sketch for a rangoli, In her design, there is a
square inscribed in her square. The edges of the inner square meet the
center point of the sides of the outer circle.
She knows only about the number of points needed to draw one side of the
inner square.
Write a program to help her draw the sketch.
Answer
import java.util.Scanner;
class Main
{
public static void main(String[]args)
{
Scanner in=new Scanner(System.in);
int n=in.nextInt();
int b=n,c=n,k=n*2-1;
for(int i=1;i<=k;i++)
{
for(int j=1;j<=k;j++)
{
if(j>b&&j<c)
System.out.print(" ");
else
System.out.print("* ");
}
System.out.println();
if(i>=n)
{
c--;
b++;
}
else
{
c++;
b--;
}
}
}
}
Note : Each chair has a number and there is more than one chair with the
same number.
12345
4
3
2
12345
Answer
import java.util.Scanner;
class Main
{
public static void pattern(int n)
{
int t=1,b=1,d=n-1;
for(int i=0;i<n;i++)
{
System.out.print(t++ +" ");
}
System.out.println();
for(int i=1;i<n-1;i++)
{
for(int j=0;j<2*(n-i-1);j++)
{
System.out.print(" ");
}
System.out.print(d--);
System.out.println();
}
for(int i=0;i<n;i++)
{
System.out.print(b++ +" ");
}
}
public static void main(String[]args)
{
Scanner in=new Scanner(System.in);
int n=in.nextInt();
pattern(n);
}
}
*
***
*****
*******
*********
Answer
import java.util.Scanner;
class Main
{
public static void main(String[]args)
{
Scanner in=new Scanner(System.in);
int a=in.nextInt();
for(int i=0;i<a;i++)
{
for(int j=0;j<=i*2;j++)
{
System.out.print("*");
}
System.out.println();
}
}
}
Kaprekar Number
Consider an n-digit number k. Square it and add the right n digits to the left
n or n-1 digits. If the resultant sum is k, then k is called a Kaprekar number.
For example, 9 is a Kaprekar number since 92 = 81 & 8+1=9. and 297
is a Kaprekar number since 2972 = 88209 & 88+209 = 297
Answer
import java.util.Scanner;
class Main
{
public static boolean kaprekar(int n)
{
if(n==1)
return true;
int s=n*n;
int c=0;
while(s!=0)
{
c++;
s/=10;
}
s=n*n;
for(int i=1;i<c;i++)
{
int e=(int)Math.pow(10,i);
if(e==n)
{
continue;}
int sum=s/e+s%e;
if(sum==n)
return true;
}
return false;
}
public static void main(String[]args)
{
Scanner in=new Scanner(System.in);
int n=in.nextInt();
boolean y=kaprekar(n);
if(y)
{
System.out.print("Kaprekar Number");
}
else
{
System.out.print("Not a Kaprekar Number");
}
}}
9. Problem Statement:-
Write a program to generate the first 'n' terms of the following series 0.5,
1.5, 4.5, 13.5,...
Answer
// You are using Java
import java.util.Scanner;
class Main
{
public static void main(String[]args)
{
Scanner in=new Scanner(System.in);
int n=in.nextInt();
double a=0.5,k;
System.out.print(a);
for(int i=1;i<n;i++)
{
a=a*3;
System.out.print(" "+a);
}
}
}
Answer
import java.util.Scanner;
class Main
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int n=in.nextInt();
int sum=0;
for(int i=0;i<n;i++)
{
sum=n*(n-1);
}
System.out.println(sum/2);
}
}