Pattern Printing Programs in C and Python — Pyramid & String

Classified in Computers

Written on in English with a size of 4.26 KB

Number Pyramid of Integers

Write a program to generate the following patterns of integers:

              1
            121
           12321
         1234321

Corrected C Program (Number Pyramid)

The following C program prints the above centered palindrome number pyramid. It prompts for the number of rows.

#include <stdio.h>

int main()
{
    int i, j, row;
    printf("Enter number of rows: ");
    if (scanf("%d", &row) != 1) {
        printf("Invalid input.\n");
        return 1;
    }

    for (i = 1; i <= row; i++)
    {
        for (j = 1; j <= row - i; j++)
        {
            printf(" ");
        }
        for (j = 1; j <= i; j++)
        {
            printf("%d", j);
        }
        for (j = i - 1; j >= 1; j--)
        {
            printf("%d", j);
        }
        printf("\n");
    }
    return 0;
}

Find and Fix Errors in C Code

Original faulty snippet (as provided):

#define (studio,h)

float main <>

 int m [][] = {{1,2,3,4}};

for (i = 4, I>=0; i--)

for (j=0, j <4; j++);

m [i][j] = a[i][j]+b[i][j];

return 0.

0;

 }

Several errors exist in the code. Corrected list of issues:

  1. Invalid macro syntax: '#define (studio,h)' is not valid. Use an identifier for a macro name, e.g. '#define STUDIO_H'.
  2. Incorrect main signature: 'float main <>' is invalid. Use 'int main(void)' or 'int main()'.
  3. Array declaration: 'int m [][]' requires sizes for at least one dimension, or specify dimensions correctly.
  4. Undeclared loop variables: 'i' and 'j' are not declared before use.
  5. Wrong for-loop syntax: 'for (i = 4, I>=0; i--)' and 'for (j=0, j <4; j++);' contain commas instead of semicolons; the inner loop ends with an extra semicolon which nullifies the loop body.
  6. Undeclared operands: 'a' and 'b' are used but not declared or initialized.
  7. Invalid return: 'return 0.' has an extra period; stray '0;' after return is incorrect.

Corrected C Example (Matrix Addition)

Below is a simple corrected example that demonstrates adding two 1x4 arrays into m. Adapt dimensions and initialization as needed.

#include <stdio.h>
#define STUDIO_H // placeholder macro if needed

int main(void)
{
    int m[1][4] = {{1, 2, 3, 4}}; // example initialization for m
    int a[1][4] = {{10, 20, 30, 40}}; // example a
    int b[1][4] = {{1, 1, 1, 1}};     // example b
    int i, j;

    for (i = 0; i < 1; i++) {
        for (j = 0; j < 4; j++) {
            m[i][j] = a[i][j] + b[i][j];
        }
    }

    /* Print result */
    for (i = 0; i < 1; i++) {
        for (j = 0; j < 4; j++) {
            printf("%d ", m[i][j]);
        }
        printf("\n");
    }

    return 0;
}

String Pattern Using "NEPAL"

Problem: Write a program to display the following pattern using the string "NEPAL":

N
NE
NEP
NEPA
NEPAL

Python Implementation (NEPAL)

def display_pattern(word):
    for i in range(len(word)):
        print(word[:i+1])

word = "NEPAL"
display_pattern(word)

This will output:

N
NE
NEP
NEPA
NEPAL

WAP to Display Substrings (L to NEPAL)

Original requested sequence (as provided):

L
AL
PAL
EPAL
NEPAL

Python Implementation (Custom Order)

A simple Python program that prints the given sequence (from shortest suffix to the full word):

def display_specific_substrings():
    countries = ["L", "AL", "PAL", "EPAL", "NEPAL"]
    for country in countries:
        print(country)

display_specific_substrings()

This code will output:

L
AL
PAL
EPAL
NEPAL

Notes:

  • All code blocks are corrected for syntax and basic errors, but you should adapt array sizes and initial values to your specific needs.
  • When compiling C code, ensure you have a C compiler (gcc/clang) and compile with warnings enabled (e.g., gcc -Wall -Wextra).
  • For Python examples, use Python 3.

Related entries: