Tutorial 6

πŸ‘‰
1. Write a program that prints the following text at the terminal.

#include <stdio.h>

int main (void)

{

    printf ("* In C, lowercase letters are significant. \n");

    printf ("* main is where program execution begins.\n");

    printf ("* Opening and closing braces enclose program statements in a routine.\n");

    printf ("* All program statements must be terminated by a semicolon.\n");

return 0;

}

#include <stdio.h> int main(void) {


The program starts with including the standard input-output library and defining the main function, which is where the program execution begins.int main(void) :-

This is the beginning of the main function, which is the entry point of every C program. int is the return type of the main function, and void inside the parentheses indicates that main takes no arguments.


printf("* In C, lowercase letters are significant. \n"); 
printf("* main is where program execution begins.\n");
printf("* Opening and closing braces enclose program statements in a routine.\n"); 
printf("* All program statements must be terminated by a semicolon.\n");

The printf() function is used to print out each statement. 
The \n is used to add a newline at the end of each statement for better formatting.return 0;

This statement indicates that the main function is finished executing, and it returns 0 to the operating system, indicating successful completion of the program.

return 0; }

Finally, the main function returns 0 to the operating system, indicating successful completion of the program.

πŸ‘‰
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.


πŸ‘‰
4. What output would you expect from the following program?

#include <stdio.h>

int main (void)

{

    printf ("Testing...");

    printf ("....1");a

    printf ("...2");

    printf ("..3");

    printf ("\n");

return 0;

}


OUTPUT =>

Testing.......1...2..3


This C program defines a main() function that sequentially prints out the strings "Testing...", "....1", "...2", and "..3" each on a separate line using the printf() function. It concludes with a newline character. The program then returns 0, indicating successful execution to the operating system.

Comments

Popular posts from this blog

Introduction To C

First C Program

Compilation process in c