0% found this document useful (0 votes)
3 views13 pages

Java Lecture 5

Uploaded by

manya2004tiwari
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views13 pages

Java Lecture 5

Uploaded by

manya2004tiwari
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java - Introduction to Programming

Lecture 5

Patterns - Part 1

1.​

import [Link].*;

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++) {
[Link]("*");
}
[Link]();
}
}
}

Apna College
2.​

import [Link].*;

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) {
[Link]("*");
} else {
[Link](" ");
}
}
[Link]();
}
}
}

Apna College
3.​

import [Link].*;

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++) {
[Link]("*");
}
[Link]();
}
}
}

Apna College
4.​

import [Link].*;

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++) {
[Link]("*");
}
[Link]();
}
}
}

Apna College
5.​

import [Link].*;

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++) {
[Link](" ");
}

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


[Link]("*");
}
[Link]();
}
}
}

Apna College
6.​

import [Link].*;

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++) {
[Link](j);
}
[Link]();
}
}
}

Apna College
7.​

import [Link].*;

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++) {
[Link](j);
}
[Link]();
}
}
}

Apna College
8.​

import [Link].*;

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++) {
[Link](number+" ");
number++;
}
[Link]();
}
}
}

Apna College
9.​

import [Link].*;

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) {
[Link](1+" ");
} else {
[Link](0+" ");
}
}
[Link]();
}
}
}

Apna College
Homework Problems (Solutions in next Lecture’s Video)
1.​ Print a solid rhombus.

2.​ Print a number pyramid.

3.​ Print a palindromic number pyramid.

Apna College
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.​ [Link](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.

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

import [Link].*;

public class Solutions {


public static void main(String args[]) {
Scanner sc = new Scanner([Link]);
int input;

do {
int marks = [Link]();
if(marks >= 90 && marks <= 100) {
[Link]("This is Good");
} else if(marks >= 60 && marks <= 89) {
[Link]("This is also Good");
} else if(marks >= 0 && marks <= 59) {
[Link]("This is Good as well");
} else {
[Link]("Invalid");
}

[Link]("Want to continue ? (yes(1) or no(0))");


input = [Link]();

} 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 [Link].*;

public class Solutions {


public static void main(String args[]) {
Scanner sc = new Scanner([Link]);
int n = [Link]();

boolean isPrime = true;


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

Apna College
if(n % i == 0) {
isPrime = false;
break;
}
}

if(isPrime) {
if(n == 1) {
[Link]("This is neither prime not composite");
} else {
[Link]("This is a prime number");
}
} else {
[Link]("This is not a prime number");
}
}
}

Apna College

You might also like