Knowee
Questions
Features
Study Tools

Write a C program to input any two characters and a number (number of lines) from thekeyboarddisplayatriangleasshowninbelowfigure.Thetwocharactersshouldfillthetrianglein every other place

Question

Write a C program to input any two characters and a number (number of lines) from thekeyboarddisplayatriangleasshowninbelowfigure.Thetwocharactersshouldfillthetrianglein every other place

🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, here is a simple C program that does what you asked for. This program will take two characters and a number as input and then print a triangle pattern with the two characters.

#include <stdio.h>

int main() {
    char ch1, ch2;
    int lines, i, j, k;

    printf("Enter two characters: ");
    scanf("%c %c", &ch1, &ch2);

    printf("Enter number of lines: ");
    scanf("%d", &lines);

    for(i=1; i<=lines; i++) {
        for(j=lines; j>i; j--) {
            printf(" ");
        }
        for(k=1; k<=(2*i-1); k++) {
            if(k%2 == 0)
                printf("%c", ch2);
            else
                printf("%c", ch1);
        }
        printf("\n");
    }

    return 0;
}

This program works by using nested for loops to print the triangle. The outer loop is used to print each line of the triangle, the first inner loop is used to print the spaces before the characters, and the second inner loop is used to print the characters. The if-else statement inside the second inner loop is used to alternate between the two characters.

This problem has been solved

Similar Questions

Write a C program to read a sentence through keyboard and to display the count ofwhite spaces in the given sentence.

Write a C program to read an English Alphabet through keyboard and display whetherthe given alphabet is in upper case or lower case.

A C program contains the following statements:#include <stdio.h>char a, b , c;( a ) Write appropriate getchar statements that will allow values for a, b and c to be entered into the computer.(b) Write appropriate putchar statements that will allow the current values of a, b and c to be written out ofthe computer (i.e., to be displayed).

easiest C Program to Write and Read a Character from a File

Write a program test-ascii.cpp that asks the user to input a line of text (which may possibly include spaces). The program should report all characters from the input line together with their ASCII codes.

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.