[Tutorial 6 ] 2. Write a program that prints the following text at the terminal.

i)

*

* *

* * *

* * * *

* * * * *

* * * * * *


#include<stdio.h>

int main(void)

{

int row,column;

for(row=1;row<=6;row++)

{

for(column=1;column<=row;column++)

{

printf("*");

}

printf("\n");

}

return 0;

}

This C program prints a pattern resembling a right triangle with asterisks, where the number of asterisks in each row increases from 1 in the first row to 6 in the last row. It achieves this using nested loops: the outer loop controls the number of rows, and the inner loop determines the number of asterisks to print in each row. After printing the required number of asterisks in each row, a newline character is printed to move to the next row. Thus, the result is a right triangle pattern made of asterisks.

ii)
* * * * * * *
* * * * * * *
* * * * * * *
* * * * * * *
* * * * * * *


#include<stdio.h>
int main(void)
{
int row,column;

for(row=1;row<=5;row++)
{
for(column=1;column<=7;column++)
{
printf("*");
}
printf("\n");
}
return 0;
}

This C program uses nested loops to print a rectangle made of asterisks with 5 rows and 7 columns. The outer loop iterates over each row, and the inner loop prints asterisks to form each row. After printing 7 asterisks in each row, a newline character is printed to move to the next row. The result is a rectangle composed of asterisks.

iii)

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


#include<stdio.h>
int main(void)
{
int row,column;
for(row=6;row>=1;row--)
{
for(column=1;column<=row;column++)
{
printf("*");
}
printf("\n");
}
return 0;
}

This C program prints a pattern resembling a right triangle with asterisks, where the number of asterisks in each row decreases from 6 in the first row to 1 in the last row. It achieves this by using nested loops: the outer loop controls the number of rows, and the inner loop determines the number of asterisks to print in each row. After printing the required number of asterisks in each row, a newline character is printed to move to the next row. Thus, the result is a right triangle pattern made of asterisks.

iv)
*
* *
* * *
* * * *
* * * * *
* * * * * *

#include<stdio.h>
int main()
{
int row,column;
for(row=6;row>=1;row--)
{
for(column=1;column<=6;column++)
{
if(row>column)
{
printf(" ");
}
else
{
printf("*");
}
}
printf("\n");
}
return 0;
}

This C program prints a pattern resembling a right triangle with asterisks, where the number of asterisks in each row increases from 1 in the first row to 6 in the last row, but the asterisks are aligned to the right. It achieves this by using nested loops: the outer loop controls the number of rows, and the inner loop determines the number of characters to print in each row. Within the inner loop, there's an if-else statement that checks if the current row number is greater than the current column number. If it is, it prints a space; otherwise, it prints an asterisk. This creates the alignment effect, shifting the asterisks to the right. After printing the required number of characters in each row, a newline character is printed to move to the next row. Thus, the result is a right triangle pattern made of asterisks aligned to the right.

v)
* * * * * *
* * * * *
* * * *
* * *
* *
*

#include<stdio.h>
int main()
{
int row, column;
for (row = 1; row <= 6; row++) 
    {
        for (column = 1; column <= 6; column++) 
        {
            if (column < row) 
            {
                printf(" ");
            } 
            else 
            {
                printf("*");
            }
        }
        printf("\n");
    }
    return 0;
}

This C program prints a pattern resembling a right triangle with asterisks, but with the asterisks aligned to the right. It achieves this using nested loops: the outer loop controls the number of rows, and the inner loop determines the number of characters to print in each row. Within the inner loop, there's an if-else statement that checks if the current column number is less than the current row number. If it is, it prints a space; otherwise, it prints an asterisk. This creates the alignment effect, shifting the asterisks to the right. After printing the required number of characters in each row, a newline character is printed to move to the next row. Thus, the result is a right triangle pattern made of asterisks aligned to the right.

vi)
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

#include<stdio.h>
int main() 
{
int row, column;
for (row = 1; row <= 6; row++)
  {
        for (column = 1; column <= 6;column++) 
{
if(row>=column)
{
printf("%i",column);
}
        }
        printf("\n");
    }
    return 0;
}

This C program prints a pattern where each row consists of numbers from 1 to the row number, and the number of columns is fixed at 6. It achieves this using nested loops: the outer loop controls the number of rows, and the inner loop determines the numbers to print in each row. Within the inner loop, there's an if statement that checks if the current row number is greater than or equal to the current column number. If it is, it prints the column number. After printing the required numbers in each row, a newline character is printed to move to the next row. Thus, the result is a pattern where each row displays numbers incrementally from 1 to the row number.

Comments

Popular posts from this blog

Introduction To C

First C Program

Compilation process in c