0% found this document useful (0 votes)
65 views

Cse115 Lab Manual 10 Nested - Loop - Part2

Uploaded by

tasin.nextlab
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)
65 views

Cse115 Lab Manual 10 Nested - Loop - Part2

Uploaded by

tasin.nextlab
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/ 2

CSE 115 Lab on nested loop (part 2) – Ara2

C programs to print the following patterns:


1. 2.
* 1
* * 1 2
* * * 1 2 3
* * * * 1 2 3 4
* * * * * 1 2 3 4 5
#include <stdio.h> #include <stdio.h>
void main() void main()
{ {
int i, j, rows; int i, j, rows;
printf("Enter no. of rows: "); printf("Enter no. of rows: ");
scanf("%d",&rows); scanf("%d",&rows);

int space=rows-1; int space=rows-1;


for(i=1; i<=rows; i++){ for(i=1; i<=rows; ++i){
for(j=1;j<=space; j++) for(j=1;j<=space; j++)
printf(" "); printf(" ");

for(j=1; j<=i; j++) for(j=1; j<=i; j++)


printf("* "); printf("%d ",j);

printf("\n"); printf("\n");
space--; space--;
} }
} }
3. Write a C program to compute the sum of the following series using nested loop

( )(
1 1 1 1 1 1 1 1
+ + + + + +…+ + +… +
1 1 2 1 2 3 1 2 ) (
1
n )
#include<stdio.h>
void main()
{
int i, j, n;
float sum=0, term;
printf("Enter n:");
scanf("%d", &n);

for ( i = 1 ; i <= n ; i++ ) {


//compute i-th term = 1/1 + 1/2 + … + 1/i
term = 0;
for ( j = 1 ; j <= i ; j++ )
term+=1.0/j;
//add i-th term with sum
sum += term;
}//i
printf("%f\n",sum);
}//main
Exercise Problems:
1. Write separate C programs to print the following patterns (read number of rows from user):
* * * * * A A
* * * * A B ABC
* * * A B C ABCDE
* * A B C D ABCDEFG
* A B C D E ABCDEFGHI

Assignment Problems:

1. Write separate C programs to print the following patterns for n lines (n is input) using nested loop:

***** A 1 1 0
* * B B 23 234 01
* * C C C 456 56789 010
** D D D D 7890 0123456 0101
* E E E E E 12345 789012345 01010
*
***
*****
*******
*********
*******
*****
***
*

You might also like