Time in Pune:

Thursday 25 June 2020

C Logical program :


Sample c program for  easy to learn c programming language.....
    #include<stdio.h>
    void main()
    {
     printf("Welcome to C Programming language");
    }
 ans:
     Welcome to C Programming language


Square  Star printing C Program .

  1. #include <stdio.h>  
  2.  int main()  
  3. {  
  4.     int n;  
  5.     printf("Enter the number of rows");  
  6.     scanf("%d",&n);  
  7.     for(int i=0;i<n;i++)  
  8.     {  
  9.         for(int j=0;j<n;j++)  
  10.         {  
  11.             printf("*");  
  12.         }
  13.         printf("\n");  
  14.     }  
  15.       
  16.     return 0;  
  17. }  
Output

Star Program in C

Star Patterns Program in C

In this topic, we will learn how to create the patterns by using C language. We will create the patterns by using either a '*' star character or some other character. We will create different patterns or a geometrical shape such as triangle, square, etc.
We are going to cover the following patterns. You can view the code of any pattern given below by clicking on the pattern.
Star Program in CStar Program in CStar Program in CStar Program in CStar Program in CStar Program in CStar Program in CStar Program in CStar Program in CStar Program in CStar Program in CStar Program in CStar Program in CStar Program in CStar Program in CStar Program in CStar Program in CStar Program in CStar Program in CStar Program in CStar Program in CStar Program in CStar Program in CStar Program in CStar Program in C

Square Star Pattern

The code to create the square star pattern is given below:
  1. #include <stdio.h>  
  2.  int main()  
  3. {  
  4.     int n;  
  5.     printf("Enter the number of rows");  
  6.     scanf("%d",&n);  
  7.     for(int i=0;i<n;i++)  
  8.     {  
  9.         for(int j=0;j<n;j++)  
  10.         {  
  11.             printf("*");  
  12.         }  
  13.         printf("\n");  
  14.     }  
  15.       
  16.     return 0;  
  17. }  
Output
Star Program in C

Hollow Square Star Pattern

Now, we create the hollow square star pattern. The source code for this pattern is given below:
  1. int main()  
  2. {  
  3.     int n;  
  4.     printf("Enter the number of rows");  
  5.     scanf("%d",&n);  
  6.     for(int i=1;i<=n;i++)  
  7.     {  
  8.         for(int j=1;j<=n;j++)  
  9.         {  
  10.             if(i==1 ||i==n||j==1||j==n)  
  11.             {  
  12.             printf("*");  
  13.             }  
  14.             else  
  15.             printf(" ");  
  16.         }  
  17.         printf("\n");  
  18.     }  
  19.       
  20.     return 0;  
  21. }  
Output
Star Program in C

Hollow Square Pattern with Diagonal

The code for the hollow square star pattern is given below:
  1. #include <stdio.h>  
  2. int main()  
  3. {  
  4.     int n;  
  5.     printf("Enter the number of rows");  
  6.     scanf("%d",&n);  
  7.     for(int i=1;i<=n;i++)  
  8.     {  
  9.         for(int j=1;j<=n;j++)  
  10.         {  
  11.             if(i==1 ||i==n||j==1||j==n-i+1||i==j||j==n)  
  12.             {  
  13.             printf("*");  
  14.             }  
  15.             else  
  16.             {  
  17.                   
  18.                       printf(" ");  
  19.                   }  
  20.                  
  21.             }        
  22.         printf("\n");  
  23.     }  
  24.       
  25.     return 0;  
  26. }  
Output
Star Program in C



Rhombus Star Pattern

The code for the rhombus star pattern is given below:
  1. #include <stdio.h>  
  2.   
  3. int main()  
  4. {  
  5.     int n;  
  6.     printf("Enter the number of rows");  
  7.     scanf("%d",&n);  
  8.     for(int i=n;i>=1;i--)  
  9.     {  
  10.         for(int j=1;j<=i-1;j++)  
  11.         {  
  12.             printf(" ");  
  13.         }  
  14.         for(int k=1;k<=n;k++)  
  15.         {  
  16.             printf("*");  
  17.         }  
  18.         printf("\n");  
  19.     }  
  20.     return 0;  
  21. }  
Output
Star Program in C



Hollow Rhombus Star Pattern


The code for the hollow rhombus star pattern is given below:
  1. #include <stdio.h>  
  2.   
  3. int main()  
  4. {  
  5.     int n;  
  6.     printf("Enter the number of rows");  
  7.     scanf("%d",&n);  
  8.     for(int i=n;i>=1;i--)  
  9.     {  
  10.         for(int j=1;j<=i-1;j++)  
  11.         {  
  12.             printf(" ");  
  13.         }  
  14.         for(int k=1;k<=n;k++)  
  15.         {  
  16.            if(i==1 || i==n || k==1 || k==n)  
  17.             printf("*");  
  18.             else  
  19.             printf(" ");   
  20.         }  
  21.         printf("\n");  
  22.     }  
  23.     return 0;  
  24. }  
Output
Star Program in C



Mirrored Rhombus Star Pattern


The code for the mirrored rhombus star pattern is given below:
  1. #include <stdio.h>  
  2. int main()  
  3. {  
  4.     int n;  
  5.     printf("Enter the number of rows");  
  6.     scanf("%d",&n);  
  7.     for(int i=1;i<=n;i++)  
  8.     {  
  9.         for(int j=1;j<i;j++)  
  10.         {  
  11.             printf(" ");  
  12.         }  
  13.         for(int k=1;k<=n;k++)  
  14.         {  
  15.            printf("*");  
  16.               
  17.         }  
  18.         printf("\n");  
  19.     }  
  20.     return 0;  
  21. }  
Output
Star Program in C

Hollow Mirrored Rhombus Star Pattern

The code for the hollow mirrored rhombus star pattern is given below:
  1. #include <stdio.h>  
  2. int main()  
  3. {  
  4.     int n;  
  5.     printf("Enter the number of rows");  
  6.     scanf("%d",&n);  
  7.     for(int i=1;i<=n;i++)  
  8.     {  
  9.         for(int j=1;j<i;j++)  
  10.         {  
  11.             printf(" ");  
  12.         }  
  13.         for(int k=1;k<=n;k++)  
  14.         {  
  15.          if(i==1 || i==n || k==1 || k==n)  
  16.            printf("*");  
  17.             else  
  18.             printf(" ");  
  19.         }  
  20.         printf("\n");  
  21.     }  
  22.     return 0;  
  23. }  
Output
Star Program in C

Right Triangle Star Pattern

The code for the right triangle star pattern is given below:
  1. #include <stdio.h>  
  2.   
  3. int main()  
  4. {  
  5.     int n;  
  6.     printf("Enter the number of rows");  
  7.     scanf("%d",&n);  
  8.     for(int i=1;i<=n;i++)  
  9.     {  
  10.         for(int j=1;j<=i;j++)  
  11.         {  
  12.             printf("* ");  
  13.         }  
  14.         printf("\n");  
  15.     }  
  16.     return 0;  
  17. }  
Output
Star Program in C

Hollow Right Triangle Star Pattern

The code for the hollow right triangle star pattern is given below:
  1. #include <stdio.h>  
  2.   
  3. int main()  
  4. {  
  5.     int n;  
  6.     printf("Enter the number of rows");  
  7.     scanf("%d",&n);  
  8.     for(int i=1;i<=n;i++)  
  9.     {  
  10.         for(int j=1;j<=i;j++)  
  11.         {  
  12.         if(j==1|| i==j || i==n )  
  13.         {  
  14.             printf("*");  
  15.         }  
  16.         else  
  17.         printf(" ");  
  18.         }  
  19.         printf("\n");  
  20.     }  
  21.     return 0;  
  22. }  
Output
Star Program in C

Mirrored Right Triangle Star Pattern

The code for the mirrored right triangle star pattern is given below:
  1. #include <stdio.h>  
  2.   
  3. int main()  
  4. {  
  5.     int n,m=1;  
  6.     printf("Enter the number of rows");  
  7.     scanf("%d",&n);  
  8.     for(int i=n;i>=1;i--)  
  9.     {  
  10.         for(int j=1;j<=i-1;j++)  
  11.         {  
  12.           printf(" ");  
  13.         }  
  14.         for(int k=1;k<=m;k++)  
  15.         {  
  16.             printf("*");  
  17.         }  
  18.         printf("\n");  
  19.         m++;  
  20.     }  
  21.     return 0;  
  22. }  
Output
Star Program in C

Hollow Mirrored Right Triangle Star Pattern

The code for the hollow mirrored right triangle star pattern is given below:
  1. #include <stdio.h>  
  2.   
  3. int main()  
  4. {  
  5.     int n,m=1;  
  6.     printf("Enter the number of rows");  
  7.     scanf("%d",&n);  
  8.     for(int i=n;i>=1;i--)  
  9.     {  
  10.         for(int j=1;j<=i-1;j++)  
  11.         {  
  12.           printf(" ");  
  13.         }  
  14.         for(int k=1;k<=m;k++)  
  15.         {  
  16.            if(k==1 || k==m || m==n)  
  17.             printf("*");  
  18.             else  
  19.             printf(" ");  
  20.         }  
  21.         printf("\n");  
  22.         m++;  
  23.     }  
  24.     return 0;  
  25. }  
Output
Star Program in C

Inverted Right Triangle Star Pattern

The code for the inverted right triangle star pattern is given below:
  1. #include <stdio.h>  
  2.   
  3. int main()  
  4. {  
  5.     int n,m=1;  
  6.     printf("Enter the number of rows");  
  7.     scanf("%d",&n);  
  8.     for(int i=n;i>=1;i--)  
  9.     {  
  10.       for(int j=1;j<=i;j++)  
  11.       {  
  12.           printf("*");  
  13.       }  
  14.       printf("\n");  
  15.     }  
  16.     return 0;  
  17. }  
Output
Star Program in C

Hollow Inverted Right Triangle Star Pattern

The code for the hollow inverted right triangle star pattern is given below:
  1. #include <stdio.h>  
  2.   
  3. int main()  
  4. {  
  5.     int n,m=1;  
  6.     printf("Enter the number of rows");  
  7.     scanf("%d",&n);  
  8.     for(int i=n;i>=1;i--)  
  9.     {  
  10.       for(int j=1;j<=i;j++)  
  11.       {  
  12.          if(j==1 || j==i || i==n)  
  13.           printf("*");  
  14.           else  
  15.           printf(" ");  
  16.       }  
  17.       printf("\n");  
  18.     }  
  19.     return 0;  
  20. }  
Output
Star Program in C

Inverted Mirrored Right Triangle Star Pattern

The code for the inverted mirrored right triangle star pattern is given below:
  1. #include <stdio.h>  
  2.   
  3. int main()  
  4. {  
  5.     int n,m;  
  6.     printf("Enter the number of rows");  
  7.     scanf("%d",&n);  
  8.     m=n;  
  9.    for(int i=1;i<=n;i++)  
  10.    {  
  11.        for(int j=1;j<i;j++)  
  12.        {  
  13.            printf(" ");  
  14.        }  
  15.        for(int k=1;k<=m;k++)  
  16.        {  
  17.            printf("*");  
  18.        }  
  19.        m--;  
  20.      
  21.       printf("\n");  
  22.     }  
  23.     return 0;  
  24. }  
Output
Star Program in C

Hollow Inverted Mirrored Right Triangle Star Pattern

The code for the hollow inverted mirrored right triangle star pattern is given below:
  1. #include <stdio.h>  
  2.   
  3. int main()  
  4. {  
  5.     int n,m;  
  6.     printf("Enter the number of rows");  
  7.     scanf("%d",&n);  
  8.     m=n;  
  9.    for(int i=1;i<=n;i++)  
  10.    {  
  11.        for(int j=1;j<i;j++)  
  12.        {  
  13.            printf(" ");  
  14.        }  
  15.        for(int k=1;k<=m;k++)  
  16.        {  
  17.           if(i==1 || k==1 || k==m)  
  18.            printf("*");  
  19.            else  
  20.            printf(" ");  
  21.        }  
  22.        m--;  
  23.      
  24.       printf("\n");  
  25.     }  
  26.     return 0;  
  27. }  
Output
Star Program in C

Pyramid Star Pattern

The code for the pyramid star pattern is given below:
  1. #include <stdio.h>  
  2. int main()  
  3. {  
  4.     int n,m;  
  5.     printf("Enter the number of rows");  
  6.     scanf("%d",&n);  
  7.     m=n;  
  8.    for(int i=1;i<=n;i++)  
  9.    {  
  10.        for(int j=1;j<=m-1;j++)  
  11.        {  
  12.            printf(" ");  
  13.        }  
  14.        for(int k=1;k<=2*i-1;k++)  
  15.        {  
  16.          printf("*");  
  17.        }  
  18.        m--;  
  19.      
  20.       printf("\n");  
  21.     }  
  22.     return 0;  
  23. }  
Output
Star Program in C

Hollow Pyramid Star Pattern

The code for the hollow pyramid star pattern is given below:
  1. #include <stdio.h>  
  2.   
  3. int main()  
  4. {  
  5.     int n,m;  
  6.     printf("Enter the number of rows");  
  7.     scanf("%d",&n);  
  8.     m=n;  
  9.    for(int i=1;i<=n;i++)  
  10.    {  
  11.        for(int j=1;j<=m-1;j++)  
  12.        {  
  13.            printf(" ");  
  14.        }  
  15.        for(int k=1;k<=2*i-1;k++)  
  16.        {  
  17.           if(k==1 || k==2*i-1 || i==n)  
  18.          printf("*");  
  19.          else  
  20.          printf(" ");  
  21.        }  
  22.        m--;  
  23.      
  24.       printf("\n");  
  25.     }  
  26.     return 0;  
  27. }  
Output
Star Program in C

Inverted Pyramid Star Pattern

The code for the inverted pyramid is given below:
  1. #include <stdio.h>  
  2.   
  3. int main()  
  4. {  
  5.     int n,m=1;  
  6.     printf("Enter the number of rows");  
  7.     scanf("%d",&n);  
  8.   
  9.    for(int i=n;i>=1;i--)  
  10.    {  
  11.        for(int j=1;j<m;j++)  
  12.        {  
  13.            printf(" ");  
  14.        }  
  15.        for(int k=1;k<=2*i-1;k++)  
  16.        {  
  17.            printf("*");  
  18.        }  
  19.        m++;  
  20.      
  21.       printf("\n");  
  22.     }  
  23.     return 0;  
  24. }  
Output
Star Program in C

Hollow Pyramid Star Pattern

The code for the hollow pyramid star pattern is given below:
  1. #include <stdio.h>  
  2.   
  3. int main()  
  4. {  
  5.     int n,m=1;  
  6.     printf("Enter the number of rows");  
  7.     scanf("%d",&n);  
  8.   
  9.    for(int i=n;i>=1;i--)  
  10.    {  
  11.        for(int j=1;j<m;j++)  
  12.        {  
  13.            printf(" ");  
  14.        }  
  15.        for(int k=1;k<=2*i-1;k++)  
  16.        {  
  17.           if(k==1 || k==2*i-1 || i==n)  
  18.            printf("*");  
  19.            else  
  20.            printf(" ");  
  21.        }  
  22.        m++;  
  23.      
  24.       printf("\n");  
  25.     }  
  26.     return 0;  
  27. }  
Output
Star Program in C

Half Diamond Star Pattern

The code for the half diamond star pattern is given below:
  1. #include <stdio.h>  
  2.   
  3. int main()  
  4. {  
  5.     int n,m=1;  
  6.     printf("Enter the number of columns");  
  7.     scanf("%d",&n);  
  8. for(int i=1;i<=n;i++)  
  9. {  
  10.   for(int j=1;j<=i;j++)  
  11.   {  
  12.     printf("*");  
  13.   }  
  14.   printf("\n");  
  15. }  
  16.  for(int i=n-1;i>=1;i--)  
  17.  {  
  18.    for(int j=1;j<=i;j++)  
  19.    {  
  20.      printf("*");  
  21.    }  
  22.    printf("\n");  
  23.  }    
  24.      
  25.     return 0;  
  26. }  
Output
Star Program in C

Diamond Star Pattern

The code for the diamond star pattern is given below:
  1. #include <stdio.h>  
  2. int main(void) {  
  3.   int n;  
  4.   printf("Enter the number of rows\n");  
  5.   scanf("%d",&n);  
  6.   int spaces=n-1;  
  7.   int stars=1;  
  8.   for(int i=1;i<=n;i++)  
  9.   {  
  10.     for(int j=1;j<=spaces;j++)  
  11.     {  
  12.       printf(" ");  
  13.     }  
  14.     for(int k=1;k<=stars;k++)  
  15.     {  
  16.       printf("*");  
  17.     }  
  18.     if(spaces>i)  
  19.     {  
  20.       spaces=spaces-1;  
  21.       stars=stars+2;  
  22.     }  
  23.     if(spaces<i)  
  24.     {  
  25.       spaces=spaces+1;  
  26.       stars=stars-2;  
  27.     }  
  28.     printf("\n");  
  29.   }  
  30.   return 0;}  
Output
Star Program in C

Right Arrow Star Pattern

The code of the right arrow star pattern is given below:
  1. #include <stdio.h>  
  2.   
  3. int main(void) {  
  4.     
  5.   int n;  
  6.   printf("Enter the number of columns");  
  7.   scanf("%d",&n);  
  8.   //printing the upper part of the pattern..  
  9.  for(int i=0;i<n;i++)  
  10.  {  
  11.    for(int j=0;j<i;j++)  
  12.    {  
  13.        printf(" ");  
  14.    }  
  15.    for(int k=1;k<=n-i;k++)  
  16.    {  
  17.      printf("*");  
  18.    }  
  19.    printf("\n");  
  20.  }  
  21. for(int i=1;i<n;i++)  
  22. {  
  23.   for(int j=1;j<n-i;j++)  
  24.   {  
  25.     printf(" ");  
  26.   }  
  27.   for(int k=1;k<=i+1;k++)  
  28.   {  
  29.     printf("*");  
  30.   }  
  31.   printf("\n");  
  32. }  
  33.   return 0;  
  34. }  
Output
Star Program in C

Left Arrow Star Pattern

The code for the Left arrow star pattern is given below:
  1. #include <stdio.h>  
  2.   
  3. int main(void) {  
  4.     
  5.   int n;  
  6.   printf("Enter the number of columns");  
  7.   scanf("%d",&n);  
  8.   //printing the upper part of the pattern..  
  9.  for(int i=1;i<=n;i++)  
  10.  {  
  11.    for(int j=1;j<=n-i;j++)  
  12.    {  
  13.        printf(" ");  
  14.    }  
  15.    for(int k=0;k<=n-i;k++)  
  16.    {  
  17.      printf("*");  
  18.    }  
  19.    printf("\n");  
  20.  }  
  21. for(int i=1;i<n;i++)  
  22. {  
  23.   for(int j=1;j<i+1;j++)  
  24.   {  
  25.     printf(" ");  
  26.   }  
  27.   for(int k=1;k<=i+1;k++)  
  28.   {  
  29.     printf("*");  
  30.   }  
  31.   printf("\n");  
  32. }  
  33.   return 0;  
  34. }  
Output
Star Program in C

Plus Star Pattern

The code for the plus star pattern is given below:
  1. #include <stdio.h>  
  2.   
  3. int main(void) {  
  4.   int n;  
  5.   printf("Enter the odd number only");  
  6.   scanf("%d", &n);  
  7.   for(int i=1;i<=n;i++)  
  8.   {  
  9.     if(i==((n/2)+1))  
  10.     {  
  11.       for(int j=1;j<=n;j++)  
  12.       {  
  13.         printf("+");  
  14.       }  
  15.    
  16.     }  
  17.     else  
  18.     {  
  19.     for(int j=1;j<=n/2;j++)  
  20.     {  
  21.       printf(" ");  
  22.     }  
  23.     printf("+");  
  24.     }  
  25.     printf("\n");  
  26.   }  
  27.   return 0;  
  28. }  
Output
Star Program in C

X Star Pattern

The code for the X star pattern is given below:
  1. #include <stdio.h>  
  2.   
  3. int main(void) {  
  4.   int n,m;  
  5.   printf("Enter the number");  
  6.   scanf("%d",&n);  
  7.   m=2*n-1;  
  8.   for(int i=1;i<=m;i++)  
  9.   {  
  10.     for(int j=1;j<=m;j++)  
  11.     {  
  12.        if(i==j || j==(m-i+1))  
  13.        {  
  14.          printf("*");  
  15.        }  
  16.        else  
  17.        {  
  18.          printf(" ");  
  19.        }  
  20.     }  
  21.     printf("\n");  
  22.   }  
  23.   return 0;  
  24. }  
Output
Star Program in C

InternetKing

Author & Editor

Has laoreet percipitur ad. Vide interesset in mei, no his legimus verterem. Et nostrum imperdiet appellantur usu, mnesarchum referrentur id vim.

0 comments:

Post a Comment

If you have any doubts,Please let me know...