In: Computer Science
I have uploaded the Images of the code, Typed code and Output of the Code. I have provided explanation using comments(read them for better understanding).
Images of the Code:
Note: If the below code is missing indentation please refer code Images
Typed Code:
//importing required packages
import java.util.*;
import java.lang.*;
public class Main
{
public static void main(String[] args)
{
//call a method
inputs();
}
//method called
static void inputs()
{
//scanner is used to get inputs from the user
Scanner scnr = new
Scanner(System.in);
System.out.print("Enter String:
");
//getting string input from the
user
String str = scnr.nextLine();
System.out.print("Enter Number:
");
//getting number input from the
user
int num = scnr.nextInt();
//call a method
string(str,num);
}
//method called
static void string(String s, int n)
{
//try block is used to check the errors in the block of code
try
{
//initializing empty string
String str = "";
//initializing character array with size of string
char[] c = new char[s.length()];
//for loop will iterate size of string times
for (int i = 0; i < s.length(); i++)
{
//for every iteration, storing a single character
//in character array from string
c[i] = s.charAt(i);
}
//storing n value in a
int a = n;
//for loop will iterate n times
for (int i = 0; i < n; i++)
{
//for loop will iterate a times
for(int j = 0; j<a; j++)
{
//for every iteration, storing a single character
//from character array to string
str += c[j];
}
//decreasing a
a--;
}
//printing string
System.out.print(str);
}
//catch block will handle the errors
catch(Exception e)
{
//printing that there is an error occured
System.out.print("An Error Occured");
}
}
}
//code ended here
Output:
If You Have Any Doubts. Please Ask Using Comments.
Have A Great Day!