0% found this document useful (0 votes)
59 views12 pages

Day 5 CW

Uploaded by

suryasure35
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views12 pages

Day 5 CW

Uploaded by

suryasure35
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Sri Krishna College of Engineering and Technology

Name: SRI VARSHAN B Scan to verify results

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();
}

}
}

Status : Correct Marks : 10/10

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();
}
}
}

Status : Correct Marks : 10/10


3. 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 step number at the edges of the
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 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();}}}

Status : Correct Marks : 10/10

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();
}
}
}

Status : Correct Marks : 10/10

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--;
}
}
}
}

Status : Correct Marks : 10/10

6. Keerthi, a receptionist of Hotel Benzz Park rearranges the chairs in the


reception daily. Suresh, manager of the same hotel writes a program to
display the arrangement on the reception TV screen. Today, Keerthi
arranged the chairs as shown in the sample output(like Z shape). Help
Suresh to write a program to display the arrangement pattern.

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);
}
}

Status : Correct Marks : 10/10

7. Rani, a receptionist of Hotel SMS Grand rearranges the flower in the


reception daily. Sidharth, manager of the same Hotel writes a program to
display the flower arrangement on the reception TV screen. Today, Rani
arranged the flowers as shown in the sample output. Help Sidharth to write
a program to display the pattern

*
***
*****
*******
*********

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();
}
}
}

Status : Correct Marks : 10/10


8. Problem Statement :

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 &amp; 8+1=9. and 297
is a Kaprekar number since 2972 = 88209 &amp; 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");
}
}}

Status : Correct Marks : 10/10

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);
}
}
}

Status : Correct Marks : 10/10


10. Handshakes
It was Stefan's first day at school. His teacher Elena Gilbert asked the
students to meet every other student in the class and introduce
themselves. The teacher asked them to handshake each other when they
meet. If there are n number of students in the class then find the total
number of handshakes made by the students.

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);
}
}

Status : Correct Marks : 10/10

You might also like