Basic Java Programming | CodeChum Java Solutions | AskTheCode
Problem:
Looping strings works like playing a broken record – it repeats something over and over again. But what's great about loops is that we can define as to how many times it will repeat something with proper code, and that's what we're about to do now using while() loops.
Instructions:
Scan a positive integer and store it in a variable. Using while() loop, repeatedly print out "CodeChum is awesome" for the same number of times as that of the inputted integer.
And each outputted string must be separated in new lines.
Input Sample:
4
Output Sample:
CodeChum is awesome
CodeChum is awesome
CodeChum is awesome
CodeChum is awesome
Code:
import java.util.Scanner;
public class loopingStrings{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String a = "CodeChum is awesome";
int n = sc.nextInt();
while(n != 0){
System.out.println(a);
n--;
}
}
}
Comments