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

Patterns Using C

This document contains 10 C code examples that demonstrate different patterns that can be generated using for loops. The patterns include: printing stars, numbers, letters, triangles, diamonds, and other shapes. Each code sample accepts user input for the number of rows and uses nested for loops to print the desired pattern.

Uploaded by

Akshat Pattiwar
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views

Patterns Using C

This document contains 10 C code examples that demonstrate different patterns that can be generated using for loops. The patterns include: printing stars, numbers, letters, triangles, diamonds, and other shapes. Each code sample accepts user input for the number of rows and uses nested for loops to print the desired pattern.

Uploaded by

Akshat Pattiwar
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

Patterns Using C

//P1//
Code

#include <stdio.h>

int main()
{
int n,i,j;
printf("Enter the number of rows:");
scanf("%d",&n);
for (i=1;i<=n;i++)
{
for (j=1;j<=i;j++)
{
printf("*");
}
printf("\n");
}

return 0;
}

//P2//
Code

#include <stdio.h>

int main()
{
int n,i,j;
printf("Enter the number of rows:");
scanf("%d",&n);
for (i=1;i<=n;i++)
{
for (j=1;j<=i;j++)
{
printf("%d",j);
}
printf("\n");
}

return 0;
}

//P3//
Code
#include <stdio.h>

int main()
{
int n,i,j;
printf("Enter the number of rows:");
scanf("%d",&n);
for (i=1;i<=n;i++)
{
for (j=1;j<=i;j++)
{
printf("%c",(char)j+64);
}
printf("\n");
}

return 0;
}

//P4//
Code
#include <stdio.h>

int main()
{
int n,i,j;
printf("Enter the number of rows:");
scanf("%d",&n);
for (i=1;i<=n;i++)
{
for (j=n;j>=1;j--)
{
if(j<=i)
printf("%d",j);
else
printf(" ");
}
printf("\n");
}

return 0;
}

//P5//
Code
#include <stdio.h>

int main()
{
int i,j,n,m;
printf("Enter the number of rows:");
scanf("%d",&n);
m=n;
for(int i=1;i<=n;i++)
{
for(int j=1;j<i;j++)
{
printf(" ");
}
for(int k=1;k<=m;k++)
{
printf("*");
}
m--;

printf("\n");
}
return 0;
}

//P6//
Code

#include <stdio.h>

int main()
{
int i,j,n,m=1;
printf("Enter the number of rows:");
scanf("%d",&n);
for(int i=n;i>=1;i--)
{
for(int j=1;j<=i;j++)
{
printf("*");
}
printf("\n");
}
return 0;
}

//P7//
Code
#include <stdio.h>
int main()
{
int n, i, j;
printf("Enter number of rows: ");
scanf("%d", &n);
for(i=1; i<=n; i++)
{
for(j=1; j<=n; j++)
{
if(i%2 == 1)
{
printf("*");
}
else
{
printf("$");
}
}
printf("\n");
}
return 0;
}

//P8//
Code
#include <stdio.h>
int main()
{
int n;
printf("Enter n: ");
scanf("%d", &n);
printf("P8:\n");
for(int i=0; i<n; i++)
{
for(int j=0; j<2*n; j++)
{
if((i+1)%2 == 1)
{
if(j<=i || j>=2*n-i-1)
{
printf("$ ");
}
else
{
printf(" ");
}
}
else
{
if(j<=i || j>=2*n-i-1)
{
printf("& ");
}
else
{
printf(" ");
}
}
}
printf("\n");
}
}
//P9//
Code
#include <stdio.h>

int main()
{
int n,i,j,k;
printf("Enter the number of rows:");
scanf("%d", &n);
for(i=1;i<=n-2;i++){
k=1;
for(j=1;j<=n;j++){
if(j>=4-i && j<=2+i && k){
printf("*");
k=0;
}
else{
printf(" ");
k=1;
}

}
printf("\n");
}
for(i=2;i>=1;i--){
k=1;
for(j=1;j<=n;j++){
if(j>=4-i && j<=2+i && k){
printf("*");
k=0;
}
else{
printf(" ");
k=1;
}

}
printf("\n");
}
return 0;
}

//P10//
Code
#include <stdio.h>

int main()
{int i,j,k;
for( i=1;i<=3;i++){
k=1;
for(j=1;j<=5;j++){
if(j>=4-i && j<=2+i && k){
printf("*");
k=0;
}
else{
printf(" ");
k=1;
}

}
printf("\n");
}
for(i=2;i>=1;i--){
k=1;
for(j=1;j<=5;j++){
if(j>=4-i && j<=2+i && k){
printf("*");
k=0;
}
else{
printf(" ");
k=1;
}

}
printf("\n");
}

return 0;
}

You might also like