05A-2015 – Computer Sir Ki Class

Login


Lost your password?

Don't have an account ?
Register (It's FREE) ×
  

Login
[lwa]



Exam Questions-ICSE2015-05A #JAVA#7362    siteicon   siteicon  

Problem Statement - 05A-2015

Write two separate programs to generate the following patterns using iteration(loop) statements:
(a)

1 *
2 * #
3 * # *
4 * # * #
5 * # * # *

(b)
[/code]
5 4 3 2 1
5 4 3 2
5 4 3
5 4
5
[/code]

Solution

TC++ #7362

(a)

import java.util.Scanner;

 public class Pattern1 {

 public static void main(String[] args) {
 Scanner scanner = new Scanner(System.in);
 System.out.print("Enter n: ");
 int n = scanner.nextInt();
 for (int i = 1; i <= n; i++) {
 for (int j = 1; j <= i; j++) {
 if (j % n == 1) {
 System.out.print("* ");
 } else {
 System.out.print("# ");
 }
 }
 System.out.println();
 }
 }
 }

(b)

import java.util.Scanner;
  public class Pattern2 {

  public static void main(String[] args) {
  Scanner scanner = new Scanner(System.in);
  System.out.print("Enter n: ");
  int n = scanner.nextInt();
  for (int i = n; i >= 1; i--) {
  int num = n;
  for (int j = 1; j <= i; j++) {
  System.out.print(num + " ");
  num--;
  }
  System.out.println();
  }
  }
  }


Share

CSKC| Created: 16-Apr-2019 | Updated: 16-Apr-2019|ICSE2015









Back