Skip to main content

Top Beginner Patterns in Java: How to Print Star, Number and Character (Lecture 5)

 Java - Introduction to Programming

Lecture 5



Patterns - Part 1





import java.util.*;

 

public class Patterns {

   public static void main(String args[]) {

       int n = 5;

       int m = 4;

       for(int i=0; i<n; i++) {

           for(int j=0; j<m; j++) {

               System.out.print("*");

           }

           System.out.println();

       }

   }

}

 










import java.util.*;

 

public class Patterns {

   public static void main(String args[]) {

       int n = 5;

       int m = 4;

       for(int i=0; i<n; i++) {

           for(int j=0; j<m; j++) {

               if(i == 0 || i == n-1 || j == 0 || j == m-1) {

                   System.out.print("*");

               } else {

                   System.out.print(" ");

               }

           }

           System.out.println();

       }

   }

}

 















import java.util.*;

 

public class Patterns {

   public static void main(String args[]) {

       int n = 4;

      

       for(int i=1; i<=n; i++) {

           for(int j=1; j<=i; j++) {

                   System.out.print("*");

           }

           System.out.println();

       }

   }

}

 
















import java.util.*;

 

public class Patterns {

   public static void main(String args[]) {

       int n = 4;

      

       for(int i=n; i>=1; i--) {

           for(int j=1; j<=i; j++) {

                   System.out.print("*");

           }

           System.out.println();

       }

   }

}

 












import java.util.*;

 

public class Patterns {

   public static void main(String args[]) {

       int n = 4;

      

       for(int i=n; i>=1; i--) {

           for(int j=1; j<i; j++) {

               System.out.print(" ");

           }

 

           for(int j=0; j<=n-i; j++) {

               System.out.print("*");

           }

           System.out.println();

       }

   }

}

 












import java.util.*;

 

public class Patterns {

   public static void main(String args[]) {

       int n = 5;

      

       for(int i=1; i<=n; i++) {

           for(int j=1; j<=i; j++) {

               System.out.print(j);

           }

           System.out.println();

       }

   }

}

 











import java.util.*;

 

public class Patterns {

   public static void main(String args[]) {

       int n = 5;

      

       for(int i=n; i>=1; i--) {

           for(int j=1; j<=i; j++) {

               System.out.print(j);

           }

           System.out.println();

       }

   }

}

 









import java.util.*;

 

public class Patterns {

   public static void main(String args[]) {

       int n = 5;

       int number = 1;

 

       for(int i=1; i<=n; i++) {

           for(int j=1; j<=i; j++) {

               System.out.print(number+" ");

               number++;

           }

           System.out.println();

       }

   }

}

 












import java.util.*;

 

public class Patterns {

   public static void main(String args[]) {

       int n = 5;

 

       for(int i=1; i<=n; i++) {

           for(int j=1; j<=i; j++) {

               if((i+j) % 2 == 0) {

                   System.out.print(1+" ");

               } else {

                   System.out.print(0+" ");

               }

           }

           System.out.println();

       }

   }

}

 












Homework Problems (Solutions in next Lecture’s Video)

  1. Print a solid rhombus.






  1. Print a number pyramid.






  1. Print a palindromic number pyramid.









Homework Solution (Lecture 4)


  1. Print all even numbers till n.


  1. public class Solutions {

  2.    public static void main(String args[]) {

  3.        int n = 25;

  4.  

  5.        for(int i=1; i<=n; i++) {

  6.            if(i % 2 == 0) {

  7.                System.out.println(i);

  8.            }

  9.        }

  10.    }   

  11. }

  12.  


    3.  Make a menu driven program. The user can enter 2 numbers, either 1 or 0. 

If the user enters 1 then keep taking input from the user for a student’s marks(out of 100). 

If they enter 0 then stop.

If he/ she scores :

Marks >=90 -> print “This is Good”

89 >= Marks >= 60 -> print “This is also Good”

59 >= Marks >= 0 -> print “This is Good as well”

Because marks don’t matter but our effort does.

(Hint : use do-while loop but think & understand why)


import java.util.*;

 

public class Solutions {

   public static void main(String args[]) {

       Scanner sc = new Scanner(System.in);

       int input;

 

       do {

           int marks = sc.nextInt();

           if(marks >= 90 && marks <= 100) {

               System.out.println("This is Good");

           } else if(marks >= 60 && marks <= 89) {

               System.out.println("This is also Good");

           } else if(marks >= 0 && marks <= 59) {

               System.out.println("This is Good as well");

           } else {

               System.out.println("Invalid");

           }

 

           System.out.println("Want to continue ? (yes(1) or no(0))");

           input = sc.nextInt();

      

       } while(input == 1);

   }   

}

 


Qs. Print if a number n is prime or not (Input n from the user). 

[In this problem you will learn how to check if a number is prime or not]

import java.util.*;

 

public class Solutions {

   public static void main(String args[]) {

       Scanner sc = new Scanner(System.in);

       int n = sc.nextInt();

 

       boolean isPrime = true;

       for(int i=2; i<=n/2; i++) {

           if(n % i == 0) {

               isPrime = false;

               break;

           }

       }

 

       if(isPrime) {

           if(n == 1) {

               System.out.println("This is neither prime not composite");

           } else {

               System.out.println("This is a prime number");

           }

       } else {

           System.out.println("This is not a prime number");

       }

   }   

}

 

  


Comments

Popular posts from this blog

Conditional Statements | if, If-else, "if" and "if else", Switch Break | Complete Java Placement Course | Lecture 3

                                    Java - Introduction to Programming Lecture 3 Conditional Statements Conditional statements are a fundamental concept in programming. They are used to make decisions based on certain conditions, allowing the program to perform different actions depending on the input or data it receives. In this article, we'll take a closer look at conditional statements in programming and how they can be used effectively. What are conditional statements? Conditional statements, also known as "if-then" statements, are used in programming to execute certain code if a condition is met. In other words, the program will perform a particular action if a particular condition is true. For example, a program may check if a user has ent...

Enter 3 numbers from the user & make a function to print their average in java, c++, python, java.

Enter 3 numbers from the user & make a function to print their average. To find the average of three numbers, you need to add the three numbers together and then divide the sum by three. Here's how you can write a function in C++, C, Python, and Java to take three numbers from the user and calculate their average: JAVA import java . util .*; public class Solutions {     public static void main ( String args []) {        Scanner sc = new Scanner ( System . in );        int a = sc . nextInt ();        int b = sc . nextInt ();        int c = sc . nextInt ();          int average = ( a + b + c ) / 3 ;        System . out . println ( average );    }    }   C++: // c++ #include <iostream> using namespace std ;...

Recursion in One Shot | Theory + Question Practice + Code | Level 1 - Easy

JAVA Recursion Class 1 (Codes) Q1. Print numbers from 5 to 1. public static void printNumbers(int n) { if(n == 0) { return; } System.out.println(n); printNumbers(n-1); } Q2. Print numbers from 1 to 5. public static void printNumbers(int n) { if(n == 6) { return; } System.out.println(n); printNumbers(n+1); } Q3. Print the sum of first n natural numbers. class Recursion1 { public static void printSum(int n, int sum) { if(n == 0) { System.out.println(sum); return; } sum += n; printSum(n-1, sum); } public static void main(String args[]) { printSum(5, 0); } } Q4. Print factorial of a number n. class Recursion1 { public static void printFactorial(int n, int fact) { if(n == 0) { System.out.println(fact); return; } fact *= n; printFactorial(n-1, fact); } public static void main(String args[]) { printFactorial(5, 1); } } Q5. Print the fibonacci sequence till nth term. class Recursion1 { public static void printFactorial(int a, int b, int n) { if(n == 0) { return; } System.out.println(a); printF...