C, C++ 및 Python에서 X 별 패턴을 인쇄하는 프로그램
11661 단어 pythonprogrammingcpp
예:
예 1:
입력:
Given number of rows =10
산출:
* * * * * * * * * * * * * * * * * * * *예2:
입력:
Given number of rows =10 Given character to print='@'산출:
Enter some random number of rows = 10 Enter some random character = @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @
C, C++ 및 Python에서 X 별 패턴을 인쇄하는 프로그램
아래는 C, C++, Python에서 X Star Pattern을 출력하는 방법입니다.
방법 #1: For 루프 사용(별표 문자)
접근하다:
1) 파이썬 구현
다음은 구현입니다.
# Give the number of rows of the x pattern as static input and store it in a variable. xrows=10 #Loop from 0 to the number of rows using For loop. for m in range(0, xrows): # Loop from 0 to the number of rows using another For loop(Inner For loop). for n in range(0, xrows): '''Check if the parent loop iterator value is equal to the inner loop iterator value or if the inner loop iterator value is equal to the number of rows-parent loop iterator value-1 using If conditional Statement.''' if(m==n or n==xrows - 1 - m): #Print the star character with space if the condition is true. print('*',end=' ') else: #Else print space character. print(' ',end=' ') #Print the newline character after the end of the inner For loop. print()
산출:
* * * * * * * * * * * * * * * * * * * *
2) C++ 구현
다음은 구현입니다.
#include <iostream> using namespace std; int main() { // Give the number of rows of the x pattern as static // input and store it in a variable. int xrows = 10; // Loop from 0 to the number of rows using For loop. for (int m = 0; m < xrows; m++) { // Loop from 0 to the number of rows using another // For loop(Inner For loop). for (int n = 0; n < xrows; n++) { /*Check if the parent loop iterator value is equal to the inner loop iterator value or if the inner loop iterator value is equal to the number of rows-parent loop iterator value-1 using If conditional Statement.*/ if (n == m or n == (xrows - m - 1)) { // Print the star character with space if // the condition is true. cout << "* "; } else { // Else print space character. cout << " "; } } // Print the newline character after the end of // the inner For loop. cout << endl; } return 0; }산출:
* * * * * * * * * * * * * * * * * * * *3) C 구현
다음은 구현입니다.
#include <stdio.h> int main() { // Give the number of rows of the x pattern as static // input and store it in a variable. int xrows = 10; // Loop from 0 to the number of rows using For loop. for (int m = 0; m < xrows; m++) { // Loop from 0 to the number of rows using another // For loop(Inner For loop). for (int n = 0; n < xrows; n++) { /*Check if the parent loop iterator value is equal to the inner loop iterator value or if the inner loop iterator value is equal to the number of rows-parent loop iterator value-1 using If conditional Statement.*/ if (n == m || n == (xrows - m - 1)) { // Print the star character with space if // the condition is true. printf("* "); } else { // Else print space character. printf(" "); } } // Print the newline character after the end of // the inner For loop. printf("\n"); } return 0; }산출:
* * * * * * * * * * * * * * * * * * * *
방법 #2: For 루프 사용(사용자 문자)
접근하다:
1) 파이썬 구현
다음은 구현입니다.
# Give the number of rows as user input using int(input()) and store it in a variable. xrows = int(input( 'Enter some random number of rows = ')) # Give the Character as user input using input() and store it in another variable. givencharacter = input('Enter some random character = ') # Loop from 0 to the number of rows using For loop. for m in range(0, xrows): # Loop from 0 to the number of rows using another For loop(Inner For loop). for n in range(0, xrows): '''Check if the parent loop iterator value is equal to the inner loop iterator value or if the inner loop iterator value is equal to the number of rows-parent loop iterator value-1 using If conditional Statement.''' if(m == n or n == xrows - 1 - m): # Print the given character with space if the condition is true. print(givencharacter, end=' ') else: # Else print space character. print(' ', end=' ') # Print the newline character after the end of the inner For loop. print()
산출:
Enter some random number of rows = 10 Enter some random character = @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @
2) C++ 구현
다음은 구현입니다.
#include <iostream> using namespace std; int main() { // Give the number of rows // as user input using cin and store it in a // variable. int xrows; char givencharacter; cout << "Enter some random number of rows = " << endl; cin >> xrows; // Give the Character as user input using cin and store // it in another variable. cout << "Enter some random character = " << endl; cin >> givencharacter; cout << endl; // Loop from 0 to the number of rows using For loop. for (int m = 0; m < xrows; m++) { // Loop from 0 to the number of rows using another // For loop(Inner For loop). for (int n = 0; n < xrows; n++) { /*Check if the parent loop iterator value is equal to the inner loop iterator value or if the inner loop iterator value is equal to the number of rows-parent loop iterator value-1 using If conditional Statement.*/ if (n == m || n == (xrows - m - 1)) { // Print the given character with space if // the condition is true. cout << givencharacter << " "; } else { // Else print space character. cout << " "; } } // Print the newline character after the end of // the inner For loop. cout << endl; } return 0; }
산출:
Enter some random number of rows = 10 Enter some random character = @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @
3) C 구현
다음은 구현입니다.
#include <stdio.h> int main() { // Give the number of rows // as user input using scanf and store it in a // variable. int xrows; char givencharacter; // Give the Character as user input using scanf and // store it in another variable. scanf("%d", &xrows); scanf("%c", &givencharacter); printf("\n"); // Loop from 0 to the number of rows using For loop. for (int m = 0; m < xrows; m++) { // Loop from 0 to the number of rows using another // For loop(Inner For loop). for (int n = 0; n < xrows; n++) { /*Check if the parent loop iterator value is equal to the inner loop iterator value or if the inner loop iterator value is equal to the number of rows-parent loop iterator value-1 using If conditional Statement.*/ if (n == m || n == (xrows - m - 1)) { // Print the given character with space if // the condition is true. printf("%c", givencharacter); } else { // Else print space character. printf(" "); } } // Print the newline character after the end of // the inner For loop. printf("\n"); } return 0; }
산출:
10@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @
Reference
이 문제에 관하여(C, C++ 및 Python에서 X 별 패턴을 인쇄하는 프로그램), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/sachinponnapalli/program-to-print-x-star-pattern-in-c-c-and-python-2mnf텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)