Thursday, November 2, 2023

15.ASCII alphabet value 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
  • As we can see for each line character c is printed
  • The character c is incremented after the inner for loop
  • So each line there is a different character printed
  • For i=1, character printed is 1 a,
  • 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);
    char currentChar = 'a';

    for (i = 1; i <= n; i++) {
        for (j = 1; j <= i; j++) {
            printf("%c", currentChar);
        }
        currentChar++;  // Move to the next character
        printf("\n");
    }

    getch();
}

Note: scanf is used for taking user input

Sunday, October 1, 2023

14.Hollow Number Triangle

 

Steps

  • Let's count the number of lines i.e 9 so we take it as n
  • So i will iterate from 1 to n
  • The pattern printed never exceeds i
  • For i=1 it is 1, i=9 pattern is there till 9
  • So j loop will iterate from 1 to i
  • Now coming to the pattern
  • We will see where numbers are being printed 
  • First vertical line i.e for j=1 each time
  • Next horizontal line i.e i=n (i=9 in our example)
  • Finally there is a diagonal values 1==1,2==2 so it is i=j
  • We will use these conditions in a if condition using logical or
  • A difference we see in this pattern is that stars are not being printed
  • If we observe the numbers carefully we can understand those are just j values
  • So true part will print j values
  • False part will print space

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++)
               {
                              if(i==n || j==1 || i==j)
                                     {
                                       printf("%d",j);
                                       }
                                else
                                       {
                                         printf(" ");
                                         }   
                   }
           printf("\n");
     }

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

13.Hollow Triangle

 

Steps

  • Let's count the number of lines i.e 9 so we take it as n
  • So i will iterate from 1 to n
  • The pattern printed never exceeds i
  • For i=1 it is 1, i=9 pattern is there till 9
  • So j loop will iterate from 1 to i
  • Now coming to the pattern
  • We will see where stars are being printed 
  • First vertical line i.e for j=1 each time
  • Next horizontal line i.e i=n (i=9 in our example)
  • Finally there is a diagonal values 1==1,2==2 so it is i=j
  • We will use these conditions in a if condition using logical or
  • True part will print *
  • False part will print space

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++)
               {
                              if(i==n || j==1 || i==j)
                                     {
                                       printf("*");
                                       }
                                else
                                       {
                                         printf(" ");
                                         }   
                   }
           printf("\n");
     }

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

12.Hollow rectangle

 

Steps

  • First we will count the number of lines i.e 9
  • So i will iterate from 1 to n // n is the value given by user for number of lines
  • Now for inner loop j we need to see how many times the pattern is printed i.e 15
  • Let it be m
  • So j will iterate from 1 to m 
  • Now we need to see where stars are printed and where space is printed
  • First we can see for the first line (horizontal) and last line (horizontal)
  • These two conditions will be i==1 for first line and i==n for last line
  • Next we see that each time first star is printed and last star is printed (vertical)
  • These two conditions will be j==1 for first vertical line and j==m for last line
  • These four conditions can be used together using logical or 
  • It will be used in a if  condition inside j loop
  • True part will print * 
  • False part will print space
  • Remember to add "\n"

Code

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

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