-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChallenge 1
47 lines (47 loc) · 1.97 KB
/
Challenge 1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package project1;
import java.util.*;
public class LookNSay
{
public static void main(String[] args)
{
String input,output=" "; // taking integer input as String to avoid overflow or out of bounds exception
int LengthOfSeries;
int i=0,j=0,z=1,length;
Scanner sc=new Scanner(System.in);
System.out.println("Enter your starting number: ");
input=sc.nextLine(); //taking the input number from user.
System.out.println("Enter the length of number: ");
LengthOfSeries=sc.nextInt(); //taking length up to which the user wants the output to be displayed.
length=input.length(); //length of entered input number.
while(length<=LengthOfSeries)
{
char[] newinput=input.toCharArray(); //taking the input in the form of character array
length=input.length();
if(length==1)
{
output="1"+newinput[0];
System.out.println(output);
}
for(i=0;i<length-1;i++)
{
if(newinput[j]==newinput[j+1])
z++;
else
{
output=""+z+""+newinput[j]; //here z is the number of times a particular digit occurs and is followed by the digit.
z=1;
}
j++;
if(j==(length-1))
{
output=output+""+z+""+newinput[j]; // its thecondition when the iteration reaches to last digit.
System.out.println(output);
}
}
input=output; // reinitialization of input so that in next next iteration the output is fed as input
length=input.length(); // and the loop continues till length is less than user defined length i.e, LengthOfSeries.
j=0;
z=1;
}
}
}