[go: up one dir, main page]

0% found this document useful (0 votes)
26 views7 pages

EXP3 Writeup-2

Cp 3

Uploaded by

chiragmore0077
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views7 pages

EXP3 Writeup-2

Cp 3

Uploaded by

chiragmore0077
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

EXPERIMENT NUMBER: 3

Date of Performance:

Date of Submission:

3a. Write a program to print following pattern:


1
2 2
3 3 3
3b. Write a program to print full pyramid of * by taking user input.
3c. Input a string from the user and print in the following pattern
D M C E
D M C
D M
D

THEORY:
for loop:
The for loop is an entry-controlled loop that executes the statements till the given condition.
All the elements (initialization, test condition, and increment) are placed together to form
a for loop inside the parenthesis with the for keyword.

Syntax of for loop:


for (expression 1; expression 2; expression 3)
{

// code block to be executed


}
Flowchart of for loop:

Nested-for loop:
It is also possible to place a loop inside another loop. This is called a nested loop. The "inner
loop" will be executed one time for each iteration of the "outer loop". This structure is
particularly useful when working with multidimensional arrays, intricate patterns, or
scenarios that require repeated calculations. Code readability and maintainability are
improved by nested loops in C.

Syntax:

for (initialization; condition; increment/decrement)


{
statement(s);
for (initialization; condition; increment/decrement)
{
statement(s);
... ... ...
}
... ... ...
}
Flowchart:

SOURCE CODE:
3a. Write a program to print following pattern:
1
2 2
3 3 3
Algorithm
1. Start the program.
2. Initialize two integer variables i and j.
3. Set a loop to iterate i from 1 to 3 (inclusive):
o a. For each value of i:
▪ i. Set another loop to iterate j from 1 to i (inclusive):
1. Print the value of i.
2. Print a space character.
o b. After exiting the inner loop, print a newline character to move to the
next line.
End the program
Code:
#include <stdio.h>

int main()
{
int i, j;
for(i=1;i<=3;i++)
{
for(j=1;j<=i; j++)
{
printf("%d",i);
printf(" "); // for single space “/t” for 3 spaces
}
printf("\n");
}

return 0;
}
Output:

3b. Write a program to print full pyramid of * by taking user input.


Algorithm
1. Start the program.
2. Declare integer variables: i, space, rows, and k (initialized to 0).
3. Prompt the user to enter the number of rows for the pyramid.
4. Read the input value and store it in rows.
5. Set a loop to iterate i from 1 to rows (inclusive):
o a. Initialize k to 0 at the start of each row iteration.
o b. Set a loop to iterate space from 1 to rows - i (inclusive):
▪ i. Print two spaces to create indentation for the pyramid shape.
o c. Set a loop to iterate while k is not equal to 2 * i - 1:
▪ i. Print an asterisk followed by a space.
▪ ii. Increment k by 1.
o d. Print a newline character to move to the next row.
6. End the program.

Code:
#include <stdio.h>
int main() {
int i, space, rows, k = 0;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; ++i, k = 0) {
for (space = 1; space <= rows - i; ++space) {
printf(" ");
}
while (k != 2 * i - 1) {
printf("* ");
++k;
}
printf("\n");
}
return 0;
}
Output:

3c. Input a string from the user and print in the following pattern
D M C E
D M C
D M
D
Algorithm:
1. Start the program.
2. Declare a character array (string) to hold the user input.
3. Prompt the user to enter a string.
4. Read the input string and store it in the character array.
5. Determine the length of the string and store it in a variable n.
6. Set a loop to iterate i from 0 to n - 1 (inclusive):
o a. For each value of i, set a nested loop to iterate j from 0 to n - i - 1
(inclusive):
▪ i. Print the character at position j of the string followed by a space.
o b. Print a newline character to move to the next line after each iteration of i.
7. End the program.
Code:
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
int i, j;
printf("Enter a string: ");
scanf("%s",str);
int len = strlen(str);
for(i = len; i > 0; i--)
{ for(j = 0; j < i; j++)
{
printf("%c ", str[j]);
}
printf("\n");
}
return 0;
}

Output:

CONCLUSION:

Sign & Remark:


R1 R2 R3 R4 R5 Total Signature
(3 Marks) (3 Marks) (3 Marks) (3 Mark) (3 Mark) (15 Marks)

You might also like