You are currently viewing How to write programs in C with Solution

How to write programs in C with Solution

How to write programs in C with Solution

Programs to print patterns

Write a program to print the following pattern.

*
**
***
****
*****
******
*******
********
*********
**********

Code:

#include<stdio.h>
int main()
{
    int i,j;
    for(i=1; i<=10; i++)
    {
        for(j=1; j<=i; j++)
        {
            printf("*");
        }
        printf("\n");
    }
    getch();
}

Write a program to print the following pattern.

**********
*********
********
*******
******
*****
****
***
**
*

Code:

#include<stdio.h>
int main()
{
    int i,j;
    for(i=10; i>=1; i--)
    {
        for(j=1; j<=i; j++)
        {
            printf("*");
        }
        printf("\n");
    }
    getch();
}

Write a program to print the table of a number using for loop.

Code:

#include<stdio.h>
int main()
{
    int i,num;
    printf("Enter an integer for a table: ");
    scanf("%d",&num);
    for(i=1; i<=10; i++)
    {
        printf("%d * %d = %d",num,i,num*i);
        printf("\n");
    }
    getch();
}

Output:

Enter an integer for a table: 5
5 * 1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50