21 Programs of 7-Patterns using Nested Loop, Logically explained in 40 Minutes. (200+ Programs)

 



//Various 7-Patterns Layout
#include<stdio.h>

//Global Variables
int i, j, k, n=5, a; 
char x;

void Pattern1_1(){
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf("* ");
}
printf("\n");
}
}

void Pattern1_2(){
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf("%2d",j);
}
printf("\n");
}
}

void Pattern1_3(){
for(i=1;i<=n;i++)
{
x='A';
for(j=1;j<=n;j++)
{
printf("%2c",x++);
}
printf("\n");
}
}

void Pattern2_1(){
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
printf("* ");
}
printf("\n");
}
}

void Pattern2_2(){
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
printf("%2d",n+1-j);
}
printf("\n");
}
}

void Pattern2_3(){
x='A';
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
printf("%2c",x++);
}
printf("\n");
}
}

void Pattern3_1(){
for(i=1;i<=n;i++)
{
for(j=1;j<=n+1-i;j++)
{
printf("* ");
}
printf("\n");
}
}

void Pattern3_2(){
a=1;
for(i=1;i<=n;i++)
{
for(j=1;j<=n+1-i;j++)
{
printf("%3d",a++);
}
printf("\n");
}
}

void Pattern3_3(){
x=64+n;
for(i=1;i<=n;i++)
{
for(j=1;j<=n+1-i;j++)
{
printf("%2c",x);
}
x--;
printf("\n");
}
}

void Pattern4_1(){
for(i=1;i<=n;i++)
{
for(j=1;j<=n-i;j++)
{
printf("  "); //2 times spacebar
}
for(k=1;k<=i;k++)
{
printf("* ");
}
printf("\n");
}
}

void Pattern4_2(){
a=1;
for(i=1;i<=n;i++)
{
for(j=1;j<=n-i;j++)
{
printf("   "); //3 times spacebar
}
for(k=1;k<=i;k++)
{
printf("%3d",a%2);
a++;
}
printf("\n");
}
}

void Pattern4_3(){
x='A';
for(i=1;i<=n;i++)
{
for(j=1;j<=n-i;j++)
{
printf("   "); //3 times spacebar
}
for(k=1;k<=i;k++)
{
if(i%2)
printf("%3c",x+32);
else
printf("%3c",x);
x++;
}
printf("\n");
}
}

void Pattern5_1(){
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
printf("   "); //3 times spacebar
}
for(k=1;k<=n+1-i;k++)
{
printf("*  ");
}
printf("\n");
}
}

void Pattern5_2(){
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
printf("   "); //3 times spacebar
}
for(k=1;k<=n+1-i;k++)
{
printf("%3d",n+1-k);
}
printf("\n");
}
}

void Pattern5_3(){
x='A';
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
printf("   "); //3 times spacebar
}
for(k=1;k<=n+1-i;k++)
{
printf("%3c",x);
}
x++;
printf("\n");
}
}

void Pattern6_1(){
for(i=1;i<=n;i++)
{
for(j=1;j<=n-i;j++)
{
printf(" "); //1 time spacebar
}
for(k=1;k<=i;k++)
{
printf("* ");
}
printf("\n");
}
}

void Pattern6_2(){
a=1;
for(i=1;i<=n;i++)
{
for(j=1;j<=n-i;j++)
{
printf("  "); //2 times spacebar
}
for(k=1;k<=i;k++)
{
printf("%4d",a++);
}
printf("\n");
}
}

void Pattern6_3(){
x='A';
for(i=1;i<=n;i++)
{
for(j=1;j<=n-i;j++)
{
printf(" "); //1 time spacebar
}
for(k=1;k<=i;k++)
{
printf("%2c",x++);
}
printf("\n");
}
}

void Pattern7_1(){
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
printf(" "); //1 time spacebar
}
for(k=1;k<=n+1-i;k++)
{
printf("* ");
}
printf("\n");
}
}

void Pattern7_2(){
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
printf("  "); //2 times spacebar
}
for(k=1;k<=n+1-i;k++)
{
printf("%4d",i);
}
printf("\n");
}
}

void Pattern7_3(){
x='A';
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
printf(" "); //1 time spacebar
}
for(k=1;k<=n+1-i;k++)
{
printf("%2c",x++);
}
printf("\n");
}
}

void menu()
{
printf("Menu of Patterns\n");
printf("=============================\n");
printf("0.  Exit\n");
printf("1.  Pattern-1 Program-1\n");
printf("2.  Pattern-1 Program-2\n");
printf("3.  Pattern-1 Program-3\n");
printf("4.  Pattern-2 Program-1\n");
printf("5.  Pattern-2 Program-2\n");
printf("6.  Pattern-2 Program-3\n");
printf("7.  Pattern-3 Program-1\n");
printf("8.  Pattern-3 Program-2\n");
printf("9.  Pattern-3 Program-3\n");
printf("10. Pattern-4 Program-1\n");
printf("11. Pattern-4 Program-2\n");
printf("12. Pattern-4 Program-3\n");
printf("13. Pattern-5 Program-1\n");
printf("14. Pattern-5 Program-2\n");
printf("15. Pattern-5 Program-3\n");
printf("16. Pattern-6 Program-1\n");
printf("17. Pattern-6 Program-2\n");
printf("18. Pattern-6 Program-3\n");
printf("19. Pattern-7 Program-1\n");
printf("20. Pattern-7 Program-2\n");
printf("21. Pattern-7 Program-3\n");
printf("Enter your choice: ");
}

void submenu(int ch)
{
switch(ch)
{
case 0: printf("Thanks for choosing us...\n"); break;
case 1: Pattern1_1(); break;
case 2: Pattern1_2(); break;
case 3: Pattern1_3(); break;
case 4: Pattern2_1(); break;
case 5: Pattern2_2(); break;
case 6: Pattern2_3(); break;
case 7: Pattern3_1(); break;
case 8: Pattern3_2(); break;
case 9: Pattern3_3(); break;
case 10: Pattern4_1(); break;
case 11: Pattern4_2(); break;
case 12: Pattern4_3(); break;
case 13: Pattern5_1(); break;
case 14: Pattern5_2(); break;
case 15: Pattern5_3(); break;
case 16: Pattern6_1(); break;
case 17: Pattern6_2(); break;
case 18: Pattern6_3(); break;
case 19: Pattern7_1(); break;
case 20: Pattern7_2(); break;
case 21: Pattern7_3(); break;
default: printf("Wrong Choice....\n");
}
}

int main()
{
int ch;
do{
system("cls");
menu();
scanf("%d",&ch);
submenu(ch);
system("pause");
}while(ch);
}



Click to Watch Video Explanation

Download the PDF Patterns Practice File

No comments

Post your comments

Powered by Blogger.