Saturday, September 30, 2023

6.Inverted triangle pattern with left spacing

 

I have added numbers to make you understand how we will create this pattern using for loop
  • As we know we first see how many lines of code needs to be printed, in this case it is 9 so i will iterate from 1 to n
  • Since we will take user input it can be simply n which will be taken from user
  • Here there is a difference that only stars are not being printed
  • Along with the stars space is also being printed
  • Inside outer i for loop there will be two loops one for star and another for space
  • For space we will use variable s
  • As we can see for i =1 there is no space,for i=2 there is 1 space and so on
  • So we can say that s will iterate from 1 to i-1
  • As for the pattern as we have seen before
  • j will iterate from i to n
Code
#include<stdio.h>
#include<conio.h>
void main(){
int i,j,n,s;
clrscr();
printf("Enter number of lines ");
scanf("%d",&n);
for(i=1;i<=n;i++)
    {
          for(s=1;s<=i-1;s++)
               {
                printf(" ");
                 }
          for(j=i;j<=n;j++)
               {
                 printf("*");   
                   }
           printf("\n");
     }

getch();
}
Note: scanf is used for taking user input

5.Inverted Triangle

             

I have added numbers to make you understand how we will create this pattern using for loop
  • As we know we first see how many lines of code needs to be printed, in this case it is 9
  • Since we will take user input it can be simply n which will be taken from user
  • For the inner loop we will look how many times the stars are printing
  • If we observe carefully we can see that for each line 
  • Stars are printing from i to n
  • For i=1 , the stars are printing from 1to n i.e 9 from our example
  • For i=4 , the stars are printing from 4 to n i.e 6  from our example
  • So for the inner loop j will iterate from i to n
  • Remember to print "\n" to make it move to next line
Code
#include<stdio.h>
#include<conio.h>
void main(){
int i,j,n;
clrscr();
printf("Enter number of lines ");
scanf("%d",&n);
for(i=1;i<=n;i++)
    {
          for(j=i;j<=n;j++)
               {
                 printf("*");   
                   }
           printf("\n");
     }

getch();
}
Note: scanf is used for taking user input


 


4.Triangle pattern with left spacing

 

I have added numbers to make you understand how we will create this pattern using for loop
  • As we know we first see how many lines of code needs to be printed, in this case it is 9
  • Since we will take user input it can be simply n which will be taken from user
  • Here there is a difference that only stars are not being printed
  • Along with the stars space is also being printed
  • Inside outer i for loop there will be two loops one for star and another for space
  • For space we will use variable s
  • For i=1 ,the number of space is 8,for i=4 it is 5
  • Here we can see space is being printed n-i times
  • So s will iterate from from 1 to n-i
  • As we can see for each line the stars printed are same as i's value
  • For i=1, stars printed is 1,for i=n it is n
  • So j loop that will decide what needs to be printed will iterate from 1 to i
  • Remember to print "\n" to make it move to next line
Code
#include<stdio.h>
#include<conio.h>
void main(){
int i,j,n,s;
clrscr();
printf("Enter number of lines ");
scanf("%d",&n);
for(i=1;i<=n;i++)
    {    for(s=1;s<=n-i;s++)
                {
                   printf(" ");
                  }
          for(j=1;j<=i;j++)
               {
                 printf("*");   
                   }
           printf("\n");
     }

getch();
}
Note: scanf is used for taking user input,s is for space , j for pattern

3.Triangle pattern

 


I have added numbers to make you understand how we will create this pattern using for loop
  • As we know we first see how many lines of code needs to be printed, in this case it is 9
  • Since we will take user input it can be simply n which will be taken from user
  • As we can see for each line the stars printed are same as i's value
  • For i=1, stars printed is 1,for i=n it is n
  • So j loop that will decide what needs to be printed will iterate from 1 to i
  • Remember to print "\n" to make it move to next line

Code
#include<stdio.h>
#include<conio.h>
void main(){
int i,j,n;
clrscr();
printf("Enter number of lines ");
scanf("%d",&n);
for(i=1;i<=n;i++)
    {
          for(j=1;j<=i;j++)
               {
                 printf("*");   
                   }
           printf("\n");
     }

getch();
}
Note: scanf is used for taking user input

2.Rectangle pattern

 


I have added numbers to make you understand how we will create this pattern using for loop
  • As we know we first see how many lines of code needs to be printed, in this case it is 9 so i will iterate from 1 to n
  • Since we will take user input it can be simply n which will be taken from user
  • In this example each time stars are being printed 15 times , let that be m
  • It will also be taken from user
  • So j loop will iterate from 1 to m
Code
#include<stdio.h>
#include<conio.h>
void main(){
int i,j,n,m;
clrscr();
printf("Enter number of lines ");
scanf("%d",&n);
printf("Enter number of lines ");
scanf("%d",&m);

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

getch();
}
Note: scanf is used for taking user input

Wednesday, September 27, 2023

1.Square Pattern




I have added numbers to make you understand how we will create this pattern using for loop
  • As we know we first see how many lines of code needs to be printed, in this case it is 9
  • Since we will take user input it can be simply n which will be taken from user
  • So now we know i will iterate from 1 to n
  • In this simple pattern the number of times the symbol is printed is same as n i.e 9
  • So j will also iterate from 1 to n
  • Remember to use \n to move to next line

Code
#include<stdio.h>
#include<conio.h>
void main(){
int i,j,n;
clrscr();
printf("Enter number of lines ");
scanf("%d",&n);
for(i=1;i<=n;i++)
    {
          for(j=1;j<=n;j++)
               {
                 printf("*");   
                   }
           printf("\n");
     }

getch();
}
Note: scanf is used for taking user input