0% found this document useful (0 votes)
20 views15 pages

Cohort 1 Class 6 (Patterns)

The document contains code snippets in Java, C++ and Python to print 5 patterns of multi line star patterns. It includes the code to print pattern 1 with increasing number of stars in each line, pattern 2 with increasing numbers, pattern 3 with increasing then decreasing number of stars, pattern 4 with stars separated by spaces, and pattern 5 with diagonal numbering.

Uploaded by

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

Cohort 1 Class 6 (Patterns)

The document contains code snippets in Java, C++ and Python to print 5 patterns of multi line star patterns. It includes the code to print pattern 1 with increasing number of stars in each line, pattern 2 with increasing numbers, pattern 3 with increasing then decreasing number of stars, pattern 4 with stars separated by spaces, and pattern 5 with diagonal numbering.

Uploaded by

Bhargav Raj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Pattern 1

Java Code:

import java.io.*;
import java.util.*;

public class Solution {

public static void main(String[] args) {


Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
int nst = 1;

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

for(int j=1;j<=nst;j++){
System.out.print("* ");
}

nst++;
System.out.println();
}

}
}
C++ Code:

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
int n;
cin >> n;
int nst = 1;

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


for (int j = 1; j <= nst; j++) {
cout << "* ";
}

nst++;
cout << endl;
}

return 0;
}

Python Code:
def main():
n = int(input())
nst = 1

for i in range(1, n + 1):


for j in range(1, nst + 1):
print("* ", end="")

nst += 1
print()

if __name__ == "__main__":
main()
Pattern 2

Java Code:
import java.io.*;
import java.util.*;

public class Solution {

public static void main(String[] args) {


Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
int nst = 1;

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

for(int j=1;j<=nst;j++){
System.out.print(count+" ");
count++;
}

nst++;
System.out.println();
}

}
}
C++ Code:
#include <iostream>
using namespace std;

int main() {
int n;
cin >> n;
int nst = 1;
int count = 1;

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


for (int j = 1; j <= nst; j++) {
cout << count << " ";
count++;
}

nst++;
cout << endl;
}

return 0;
}

Python Code:

def main():
n = int(input())
nst = 1
count = 1

for i in range(1, n + 1):


for j in range(1, nst + 1):
print(count, end=" ")
count += 1

nst += 1
print()

if __name__ == "__main__":
main()
Pattern 3

Java Code:

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

public static void main(String[] args) {


Scanner scn = new Scanner(System.in);
int n = scn.nextInt();

int nspaces = n / 2;
int nstars = 1;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= nspaces; j++) {
System.out.print(" ");
}

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


System.out.print("*");
}

if (i <= n / 2) {
nspaces--;
nstars += 2;
} else {
nspaces++;
nstars -= 2;
}

System.out.println();
}
}
}
C++ Code:

#include <iostream>
using namespace std;

int main() {
int n;
cin >> n;

int nspaces = n / 2;
int nstars = 1;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= nspaces; j++) {
cout << " ";
}

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


cout << "*";
}

if (i <= n / 2) {
nspaces--;
nstars += 2;
} else {
nspaces++;
nstars -= 2;
}

cout << endl;


}

return 0;
}
Python Code:

def main():
n = int(input())

nspaces = n // 2
nstars = 1
for i in range(1, n + 1):
for j in range(1, nspaces + 1):
print(" ", end="")

for j in range(1, nstars + 1):


print("*", end="")

if i <= n // 2:
nspaces -= 1
nstars += 2
else:
nspaces += 1
nstars -= 2

print()

if __name__ == "__main__":
main()
Pattern 4
Java Code:
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

public static void main(String[] args) {


Scanner scn = new Scanner(System.in);
int n = scn.nextInt();

int nstars = n / 2 + 1;
int nspaces = 1;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= nstars; j++) {
System.out.print("*");
}

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


System.out.print(" ");
}

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


System.out.print("*");
}

if (i <= n / 2) {
nspaces += 2;
nstars--;
} else {
nspaces -= 2;
nstars++;
}

System.out.println();
}
}
}
C++ Code:
#include <iostream>
using namespace std;

int main() {
int n;
cin >> n;

int nstars = n / 2 + 1;
int nspaces = 1;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= nstars; j++) {
cout << "*";
}

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


cout << " ";
}

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


cout << "*";
}

if (i <= n / 2) {
nspaces += 2;
nstars--;
} else {
nspaces -= 2;
nstars++;
}

cout << endl;


}

return 0;
}

Python Code:

def main():
n = int(input())
nstars = n // 2 + 1
nspaces = 1
for i in range(1, n + 1):
print("*" * nstars, end="")
print(" " * nspaces, end="")
print("*" * nstars)

if i <= n // 2:
nspaces += 2
nstars -= 1
else:
nspaces -= 2
nstars += 1

if __name__ == "__main__":
main()

Pattern 5_HW
Solution Video: https://youtu.be/u1cYgwIc62E
Java Code:

import java.io.*;
import java.util.*;

public class Solution {

public static void main(String[] args) {


Scanner scn = new Scanner(System.in);
int n = scn.nextInt();

int nst = 1;
int nsp = n / 2;

int val = 1;
for(int i=1;i<=n;i++){
for (int j = 0; j < nsp; j++) {
System.out.print(" ");
}
if (i <= n / 2) {
val = i;
} else {
val = n + 1 - i;
}
for (int j = 0; j < nst; j++) {
System.out.print(val + " ");

if (j < nst / 2) {
val++;
} else {
val--;

}
}

if (i <= n / 2) {
nsp--;
nst = nst + 2;
} else {
nsp++;
nst = nst - 2;
}
System.out.println();

}
}
}

C++ Code:

#include <iostream>
using namespace std;

int main() {
int n;
cin >> n;

int nst = 1;
int nsp = n / 2;

int val = 1;
for (int i = 1; i <= n; i++) {
for (int j = 0; j < nsp; j++) {
cout << " ";
}
if (i <= n / 2) {
val = i;
} else {
val = n + 1 - i;
}
for (int j = 0; j < nst; j++) {
cout << val << " ";

if (j < nst / 2) {
val++;
} else {
val--;
}
}
if (i <= n / 2) {
nsp--;
nst = nst + 2;
} else {
nsp++;
nst = nst - 2;
}
cout << endl;
}

return 0;
}

Python Code:
def main():
n = int(input())

nst = 1
nsp = n // 2

val = 1
for i in range(1, n + 1):
print(" " * nsp, end="")

if i <= n // 2:
val = i
else:
val = n + 1 - i

for j in range(nst):
print(val, end=" ")

if j < nst // 2:
val += 1
else:
val -= 1

if i <= n // 2:
nsp -= 1
nst += 2
else:
nsp += 1
nst -= 2

print()

if __name__ == "__main__":
main()

You might also like