Q-13- Assume a string variable ruler1 contains “1” initially i.e. String ruler1=”1” Write a java program to print the following output using string concatenation. (You can take extra string variables)
1
1 2 1
1 2 1 3 1 2 1
1 2 1 3 1 2 1 4 1 2 1 3 1 2 1
INPUT :
public class NCM {
public static void main(String[] args) {
// TODO Auto-generated method stub
String ruler1="1";
System.out.println(ruler1);
System.out.println(ruler1+" 2 "+ruler1);
ruler1=ruler1+" 2 "+ruler1;
System.out.println(ruler1+" 3 "+ruler1);
ruler1=ruler1+" 3 "+ruler1;
System.out.println(ruler1+" 4 "+ruler1);
}
}
OUTPUT :
1
1 2 1
1 2 1 3 1 2 1
1 2 1 3 1 2 1 4 1 2 1 3 1 2 1
1
1 2 1
1 2 1 3 1 2 1
1 2 1 3 1 2 1 4 1 2 1 3 1 2 1
INPUT :
public class NCM {
public static void main(String[] args) {
// TODO Auto-generated method stub
String ruler1="1";
System.out.println(ruler1);
System.out.println(ruler1+" 2 "+ruler1);
ruler1=ruler1+" 2 "+ruler1;
System.out.println(ruler1+" 3 "+ruler1);
ruler1=ruler1+" 3 "+ruler1;
System.out.println(ruler1+" 4 "+ruler1);
}
}
1
1 2 1
1 2 1 3 1 2 1
1 2 1 3 1 2 1 4 1 2 1 3 1 2 1
No comments:
Post a Comment