Showing posts with label C Programs. Show all posts
Showing posts with label C Programs. Show all posts
C PROGRAM : PATTERN

C PROGRAM : PATTERN

C PROGRAMS : PATTERN
16. Program to print the given pattern
// Program to print the given Pattern

/*

1  
2 6 
3 7 10
4 8 11 13
5 9 12 14 15 ... till n rows

*/

/* 

For example :

For n=3

1
2 4
3 5 6

For n=6

1  
2 7 
3 8 12
4 9 13 16 
5 10 14 17 19
6 11 15 18 20 21

*/

#include <stdio.h>
#include<conio.h> // Linux user - Remove this

 int main()
  { 
  // Declaring variable i, j to iterate loop
 int i, j;    
 
 // Declaring variable n = maximum rows 
 int n;
 
 //Declaring variable k and num to help in generating "values"
 int k, num=1; 
 
 // Inputting Number of rows
 printf("Enter number of rows : ");
  scanf("%d",&n);
 
  // Running loop for rows
  for (i=1; i<=n ; i++) 
  {
     num=i;
     k=n-1;
  
   // printing values
  for (j=1; j<=i; j++) 
      {
       // %-4d = reserves 4 spaces for printing "value" to obtain clarity in pattern
 printf("%-4d",num);
        num=num+k;
        k=k-1;    
      } 
      
   printf("\n");    
 }
        
     getch(); // Linux user - Remove this

     return 0; 
  } 


17. Program to print the given pattern
// Program to print pattern : Hollow Diamond

/* 

******** ********
*******   *******
******     ****** 
*****       *****
****         ****
***           ***
**             **
*               *

*               *
**             **
***           ***
****         ****
*****       ***** 
******     ******
*******   *******
******** ********

LOGIC :

BREAKING IT UP : It has 2 parts

First part :

******** ********
*******   *******
******     ****** 
*****       *****
****         ****
***           ***
**             **
*               *

Which is further divided into : 

First Sub Part :

********
*******
******
*****
****
***
**
* 

Second Sub part :

******** 
 ******* 
  ****** 
   *****  
    ****             
     ***               
      **
       *
       
And ofcourse the spaces before it.

Second Part :

*               *
**             **
***           ***
****         ****
*****       ***** 
******     ******
*******   *******
******** ********

Which is further divided into :

First Sub Part :

*       
**      
***     
****    
*****   
******  
******* 
********

Second Sub Part :

       *       
      **      
     ***     
    ****    
   *****   
  ******  
 ******* 
******** 

And the spaces before it.

*/

#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
 // Declaring variable i, j, s to iterate loop 
 int i, j, s;
 
 // Declaring ms = maximum space
 int ms = 1;
 
 // Running loop for rows for first part
 for(i=1;i<=8;i++)
 {
  // Printing first Sub Part :
  for(j=8;j>=i;j--)
  {
   printf("*");
  }
  
  // Printing spaces
  for(s=1;s<=ms;s++)
  {
   printf(" ");
  }
  // Incresing spaces
  ms=ms+2;
  
  // Printing Second Sub Part :
  for(j=8;j>=i;j--)
  {
   printf("*");
  }
  
  printf("\n");
  
 }
 
 printf("\n");
 // Initialising ms = 15 i.e the maximum space
 ms = 15;
 
// Running loop for Second Part
 for(i=1;i<=8;i++)
 {
  // Printing first Sub Part :
  for(j=1;j<=i;j++)
  {
   printf("*");
  }
  
  // Printing spaces
  for(s=1;s<=ms;s++)
  {
   printf(" ");
  }
  // Reducing spaces
  ms=ms-2;
  
  // Printing Second Sub Part :
  for(j=1;j<=i;j++)
  {
   printf("*");
  }
  
  printf("\n");
  
 }

    getch(); // Linux user - Remove this

    return 0;
}

18. Program to print the given pattern
/*

      *
     ***
    *****
   *******
  *********
   *******  
    *****
     ***
      * 

BREAKING IT UP : The above pattern has two parts

First Part :

      *
     ***
    *****
   *******
  *********

Second Part :

   *******  
    *****
     ***
      * 

 */

#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
 // Declaring variable i, j, s to iterate loop
    int i,j,s;
    
    // Declaring variable ms = maximum space
    int ms = 5;
    
    // Declaring variable to print "*" ( asterik )
    int as = 1;
    
    // Running loop for First part
    for(i=1;i<=5;i++)
    {
     // Printing Space
     for(s=1;s<=ms;s++)
     {
      printf(" ");
     }
     
     // Reducing spaces
     ms--;
     
     // Printing First Part
     for(j=1;j<=as;j++)
     {
      printf("*");
     }
     
     // Increasing Asterik ( "*" )
     as=as+2;
     
     printf("\n");
    }
    
 // Re-initialising ms = maximum space and aterik
 ms=2;
 as=7;
 
    // Running loop for Second Part
     for(i=1;i<=5;i++)
    {
     // Printing Space
     for(s=1;s<=ms;s++)
     {
      printf(" ");
     }
     
     // Increasing spaces
     ms++;
     
     // Printing First Part
     for(j=1;j<=as;j++)
     {
      printf("*");
     }
     
     // Decreasing Asterik ( "*" )
     as=as-2;
     
     printf("\n");
    }
    
getch(); // Linux user - Remove this

return 0;
}

19. Program to print the given pattern
/*

8 4 2 1
4 2 1
2 1
1

 */

#include<stdio.h>
#include<conio.h>
int main()
{
 int n=8,m;
 int i,j;
 m=n;
 
 for(i=4;i>=1;i--)
 {
  for(j=1;j<=i;j++)
  {
   printf("%d ",m);
   m=m/2; 
  }
  m=n/2;
  n=n/2;
  printf("\n");
 }
 
 getch();
 return 0;
}

20. Program to print the given pattern
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
        int arr[30],temp[30],i,j,k,l,n;
     
       printf("Enter the number of lines to be printed: ");
       scanf("%d",&n);

       temp[0]=1;
       arr[0]=1;

       for(j=0;j<n;j++)
       printf(" ");

       printf(" 1\n");

       for(i=1;i<n;i++)
       {
               for(j=0;j<n-i;j++)
       printf(" ");

       for(k=1;k<i;k++)
       {
                  arr[k]=temp[k-1]+temp[k];      
        }

                arr[i]=1;

                for(l=0;l<=i;l++)
       {
                 printf("%2d",arr[l]);
                 temp[l]=arr[l];
        }


                printf("\n");
        }

getch(); // Linux user - Remove this

return 0;

}

More Informative posts :




C PROGRAM : PATTERN

C PROGRAM : PATTERN

C PROGRAMS : PATTERN
11. Program to print the given pattern
// Program to print the given patter :

/* 
        5
      4 5
    3 4 5
  2 3 4 5
1 2 3 4 5


*/

/* 

REMEMBER : We always design logic according to the pattern.

LOGIC:

Simplifying the pattern :

--------5
------4 5
----3 4 5 
--2 3 4 5
1 2 3 4 5

Here ( - ) indicates space before "value" to be printed.
i.e -- indicates two space, ---- indicates four space and so on.

The above patter is similar to patern:
5
4 5
3 4 5 
2 3 4 5
1 2 3 4 5

The only difference is we print spaces before the "values". 
So design logic according to this and print spaces before that.
Its as simple as you like.

Consider the diagonal to be rows and a single line to be column.

Now earlier we worked only on rows and columns without spaces before the printed "value" 
but now we have spcaes as well.
So we include another loop for spaces.

Number of loops required = 3 
( since we have rows and columns and also spaces. 1 loop for row and 1 loop for column 
and 1 loop for spaces.)

It would be a nested loop since Number of columns depends on Number of rows.

Outer loop will be for rows and inner loop will be for columns. 
One more inner loop will be for printing space.

Above pattern is different from the pattern we have been printing.
Notice the diagonal values are all same ( i.e 5 - neither increasing nor decreasing ) and
Also the initial column value is changing. So what do we do????

Simple, Since diagonal values are all same ( i.e 5 - neither increasing nor decreasing ) 
so we consider ( 5 4 3 2 1 ) as diagonal values.
So, we run "i" loop i.e the outer loop from 5 to 1 
as the diagonal values are in decreasing order according to our assumption.

Also if you notice the single line value( i.e column value ) is in increasing order 
the only catch is this time the initial "value" is changing and the last "value" is fixed. 
Earlier it was just the reverse of it.
So, now we run "j" loop i.e the inner loop from "i" to 5 as last value is always 5.

Now For spaces : If you notice the spaces are in decreasing manner 
( i.e 8 spaces, 6 spaces, 4 spaces, 2 spaces and then none )
We can print the spaces by utilising "i" value 
but to simplify the pattern and make it easier we use another value m=8 and 
reduce it by 2 after the completion of loop for spaces.
i.e We run the inner loop "s" from 1 to m.

We print "value" in the inner loop 
since number of columns = number of "value" to be printed.
NOTE : WE CAN PRINT ANYTHING WHICH WE WANT. ( for ex : printf("%d",j) or printf("%d",i) or anything according to pattern.

Lastly after one rotation (completion) of inner loop we need to go to the next line so we use printf("\n") statement after closing the 
inner loop and before closing the outer loop.

Comments are given to make you understand the program..

*/

#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
// Declaring variable i, j, s to iterate outer and inner loop respectively
// we are using "i" for rows, "j" for columns ans "s" for spaces.
int i, j, s;
 
// Initialising m = 8 as maximum number of space = 8
int m = 8;
 
/* Since we have five rows so we iterate the "i" loop from 5 to 1 ( i.e 5 times )
We run the "i" loop in decreasing order ( i.e from 5 to 1 ) 
since rows is in increasing order(5 4 3 2 1 diagnol value - according to our assumption of diagonal values ) */
for(i=5;i>=1;i--)
{
// Printing spaces
for(s=1;s<=m;s++)
{
printf(" ");
}
// Reducing value of "m" by 2 to reduce spaces
m=m-2;
  
/* Notice in above pattern Number of columns = Position of rows 
( i.e 1st row has 1 column, 2nd row has 2 column, 3rd row has 3 columns and so on )
we run the "j" loop in increasing order ( i.e from i to 5 ) since column is in increasing order. */
  
for(j=i;j<=5;j++)
{
// Printing "value" in the inner loop since number of columns = number of "value" to be printed.
/* Here we print the value of "j" since value to be printed is the position of the column according to initialisation in loop. */

printf("%d ",j);
}
  
// After completion of printing "value" in row we need to go to the next row.
printf("\n");
}
 
getch(); // Linux user - Remove this

return 0;
}

12. Program to print the given pattern
// NOTE : For Better understanding read both LOGIC and COMMENTS

// Program to print the given patter :

/* 

1
* *
1 2 3
* * * *
1 2 3 4 5 .... till n rows

Here n = any integer value entered by user.
  n represents rows.

Example :

For n=2

1
* *

For n=6

1
* *
1 2 3
* * * *
1 2 3 4 5 
* * * * * *

*/

/* 

REMEMBER : We always design logic according to the pattern.

LOGIC:

In above Pattern Number of rows = n

Consider the diagonal to be rows and a single line to be column.

Number of loops required = 2 
( since we have rows and columns. 1 loop for row and 1 loop for column. )

It would be a nested loop since Number of columns depends on Number of rows.

Outer loop will be for rows and inner loop will be for columns.
outer loop "i" runs from 1 to n since we have n rows.
We run the "i" loop in increasing order ( i.e from 1 to n )
since rows is in increasing order(1 3  5 ... n diagnol value)

Notice in above pattern Number of columns = Position of rows 
( i.e 1st row has 1 column, 2nd row has 2 column, 3rd row has 3 columns and so on )
so we run the "j" loop i.e the inner loop from 1 to "i".
We run the "j" loop in increasing order ( i.e from 1 to i ) 
since column is in increasing order ( 1 2 3 ... i single line value )

Now every time row%2 == 0 we print "*" instead of the regular values.

We print "value" in the inner loop 
since number of columns = number of "values" to be printed.
NOTE : WE CAN PRINT ANYTHING WHICH WE WANT. ( for ex : printf("%d",j) or printf("%d",i) or anything according to pattern.

Lastly after one rotation (completion) of inner loop we need to go to the next line so we use printf("\n") statement after closing the 
inner loop and before closing the outer loop.

Comments are given to make you understand the program..

*/

#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
// Declaring variable i,j to iterate outer and inner loop respectively
// we are using "i" for rows and "j" for columns
int i,j;
 
//Declaring Variale n=rows
int n;
 
// Entering number of rows from user
printf("Enter number of rows : ");
scanf("%d",&n);
 
// Since we have five rows so we iterate the "i" loop from 1 to 5 ( i.e 5 times )
for(i=1;i<=n;i++)
{
/* Notice in above pattern Number of columns = Position of rows 
( i.e 1st row has 1 column, 2nd row has 2 column, 3rd row has 3 columns and so on )
so we run the "j" loop from 1 to "i" */
   
for(j=1;j<=i;j++)
{
/* Printing "value" in the inner loop 
since number of columns = number of "value" to be printed. */
  
if(i%2==0)
printf("* ");
else
printf("%d ",j);
}
  
// After completion of printing "value" in row we need to go to the next row. 
printf("\n");
}
 
getch(); // Linux user - Remove this

return 0;
}

13. Program to print the given pattern
// Program to print the given patter :

/* 

1                 1
1 2             1 2
1 2 3         1 2 3
1 2 3 4     1 2 3 4
1 2 3 4 5 1 2 3 4 5


*/

/* 

REMEMBER : We always design logic according to the pattern.

LOGIC:

Simplifying the pattern :

We break the pattern in parts : In this case in three parts.

First part :
1
1 2
1 2 3
1 2 3 4 
1 2 3 4 5

Second part : It is actually the spaces that we print before the third part.
----------------
  ------------
    --------
      ----  
      
Here ( - ) indicates space before "value" to be printed.
i.e -- indicates two space, ---- indicates four space and so on.

Third part :
        1
      1 2
    1 2 3
  1 2 3 4
1 2 3 4 5

The above patter ( i.e the third part ) is similar to patern:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

The only difference is we print spaces before the "values". 
So design logic according to this and print spaces before that.
I am referring to the second part spaces. Its as simple as you like.

In earlier example I have explained enough so from now on I will not go in details.

Number of loops required = 4, one outer loop and the three inner loop.

Comments are given to make you understand the program..

*/

#include<stdio.h>
#include<conio.h> // Linux user - Remove this


int main()
{
 /* Declaring variable i, j, k, s 
 to iterate outer and the three inner loop respectively */
 int i, j, k, s;
 
 // Initialising ms = 16 as maximum number of space = 16
 int ms = 16;
 
 // Running loop for rows
 for(i=1;i<=5;i++)
 {
  
  // Printing the First part : 
  for(j=1;j<=i;j++)
  {
   printf("%d ",j);
  }
  
  // Printing spaces
  for(s=1;s<=ms;s++)
  {
   printf(" ");
  }
  // Reducing value of "m" by 4 to reduce spaces
  ms=ms-4;
  
  // Printing the third part :
  for(k=1;k<=i;k++)
  {
   printf("%d ",k);
  }
  
  /* After completion of printing "value" in row 
  we need to go to the next row.*/
  printf("\n");
 }
 
 getch(); // Linux user - Remove this

 return 0;
}

14. Program to print the given pattern
// Program to print the given patter :

/* 

1                 1
1 2             2 1
1 2 3         3 2 1
1 2 3 4     4 3 2 1
1 2 3 4 5 5 4 3 2 1


*/

/* 

REMEMBER : We always design logic according to the pattern.

LOGIC:

Simplifying the pattern :

We break the pattern in parts : In this case in three parts.

First part :
1
1 2
1 2 3
1 2 3 4 
1 2 3 4 5

Second part : It is actually the spaces that we print before the third part.
----------------
  ------------
    --------
      ----  
      
Here ( - ) indicates space before "value" to be printed.
i.e -- indicates two space, ---- indicates four space and so on.

Third part :
        1
      2 1
    3 2 1
  4 3 2 1
5 4 3 2 1

The above patter ( i.e the third part ) is similar to patern:
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1

The only difference is we print spaces before the "values". 
So design logic according to this and print spaces before that.
I am referring to the second part spaces. Its as simple as you like.

In earlier example I have explained enough so from now on I will not go in details.

Number of loops required = 4, one outer loop and the three inner loop.

Comments are given to make you understand the program..

*/

#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
 /* Declaring variable i, j, k, s 
 to iterate outer and thre three inner loop respectively */
 int i, j, k, s;
 
 // Initialising ms = 16 as maximum number of space = 16
 int ms = 16;
 
 // Running loop for rows
 for(i=1;i<=5;i++)
 {
  
  // Printing the First part : 
  for(j=1;j<=i;j++)
  {
    printf("%d ",j);
  }
  
  // Printing spaces
  for(s=1;s<=ms;s++)
  {
   printf(" ");
  }
  // Reducing value of "m" by 4 to reduce spaces
  ms=ms-4;
  
  // Printing the third part :
  for(k=i;k>=1;k--)
  {
   printf("%d ",k);
  }
  
  /* After completion of printing "value" in row 
  we need to go to the next row. */
  printf("\n");
 }
 
 getch(); // Linux user - Remove this

 return 0;
}

15. Program to print the given pattern
// Program to print the given patter :

/* 

1                 5
1 2             5 4
1 2 3         5 4 3
1 2 3 4     5 4 3 2
1 2 3 4 5 5 4 3 2 1


*/

/* 

REMEMBER : We always design logic according to the pattern.

LOGIC:

Simplifying the pattern :

We break the pattern in parts : In this case in three parts.

First part :
1
1 2
1 2 3
1 2 3 4 
1 2 3 4 5

Second part : It is actually the spaces that we print before the third part.
----------------
  ------------
    --------
      ----  
      
Here ( - ) indicates space before "value" to be printed.
i.e -- indicates two space, ---- indicates four space and so on.

Third part :
        5
      5 4
    5 4 3
  5 4 3 2
5 4 3 2 1

The above patter ( i.e the third part ) is similar to patern:
5
5 4
5 4 3
5 4 3 2
5 4 3 2 1

The only difference is we print spaces before the "values". 
So design logic according to this and print spaces before that.
I am referring to the second part spaces. Its as simple as you like.

In earlier example I have explained enough so from now on I will not go in details.

Number of loops required = 4, one outer loop and the three inner loop.

Comments are given to make you understand the program..

*/

#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
 /* Declaring variable i, j, k, s 
 to iterate outer and thre three inner loop respectively */
 int i, j, k, s;
 
 // Initialising ms = 16 as maximum number of space = 16
 int ms = 16;
 
 // Initialising variable to help print third part tp = 5
 int tp = 5;
 
 // Running loop for rows
 for(i=1;i<=5;i++)
 {
  
  // Printing the First part : 
  for(j=1;j<=i;j++)
  {
    printf("%d ",j);
  }
  
  // Printing spaces
  for(s=1;s<=ms;s++)
  {
   printf(" ");
  }
  // Reducing value of "m" by 4 to reduce spaces
  ms=ms-4;
  
  // Printing the third part :
  for(k=5;k>=tp;k--)
  {
   printf("%d ",k);
  }
  //Reducing value of tp = variable used to print third part
  tp--;
  
  /* After completion of printing "value" in row 
  we need to go to the next row. */
  printf("\n");
 }
 
 getch(); // Linux user - Remove this

 return 0;
}

More Informative posts :




C PROGRAMS : PATTERN

C PROGRAMS : PATTERN

C PROGRAMS : PATTERN
6. Program to print the given pattern
// Program to print the given patter :

/* 

5
5 4
5 4 3
5 4 3 2
5 4 3 2 1

*/

/* 

REMEMBER : We always design logic according to the pattern.

LOGIC:

Consider the diagonal to be rows and a single line to be column.

Number of loops required = 2 
( since we have rows and columns. 1 loop for row and 1 loop for column. )

It would be a nested loop since Number of columns depends on Number of rows.

Outer loop will be for rows and inner loop will be for columns.

outer loop "i" runs from 5 to 1 since we have 5 rows.
We run the "i" loop in decreasing order ( i.e from 5 to 1 ) 
since rows is in increasing order(5 4 3 ... 1 diagnol value)

Notice in above pattern Number of columns = Position of rows 
( i.e 1st row has 1 column, 2nd row has 2 column, 3rd row has 3 columns and so on )
so we run the "j" loop i.e the inner loop from 5 to i.
We run the "j" loop in decreasing order ( i.e from 5 to i ) 
since column is in decreasing order ( 5 4 ... i single line value )

We print "value" in the inner loop 
since number of columns = number of "value" to be printed.
NOTE : WE CAN PRINT ANYTHING WHICH WE WANT. ( for ex : printf("%d",j) or printf("%d",i) or anything according to pattern.

Lastly after one rotation (completion) of inner loop we need to go to the next line so we use printf("\n") statement after closing the 
inner loop and before closing the outer loop.

Comments are given to make you understand the program..

*/

#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
// Declaring variable i,j to iterate outer and inner loop respectively
// we are using "i" for rows and "j" for columns
int i,j;

/* Since we have five rows but now we should also keep an eye on the pattern to be printed.

 Since the pattern to be printed is in descending order so we iterate the "i" loop from 5 to 1 ( i.e 5 times ) */

for(i=5;i>=1;i--)
{
/* Notice in above pattern Number of columns = Position of rows ( i.e 1st row has 1 column, 2nd row has 2 column, 3rd row has 3 columns and so on )
so we run the "j" loop i.e the inner loop from 5 to i. */

for(j=5;j>=i;j--)
{
/* Here we print the value of "j" since value to be printed is the position of the column according to initialisation in loop. */

printf("%d ",j);
}

// After completion of printing "value" in row we need to go to the next row.
printf("\n");
}

getch(); // Linux user - Remove this
return 0;
}

7. Program to print the given pattern
// Program to print the given patter :

/* 

1 2 3 4 5
1 2 3 4
1 2 3
1 2
1

*/

/* 

REMEMBER : We always design logic according to the pattern.

LOGIC:

Consider the diagonal to be rows and a single line to be column.

Number of loops required = 2 
( since we have rows and columns. 1 loop for row and 1 loop for column. )

It would be a nested loop since Number of columns depends on Number of rows.

Outer loop will be for rows and inner loop will be for columns.

outer loop "i" runs from 5 to 1 since we have 5 rows.
We run the "i" loop in decreasing order ( i.e from 5 to 1 ) 
since rows is in decreasing order(5 4 3 ... 1 diagnol value)

Notice in above pattern Number of columns = Position of rows 
( i.e 1st row has 1 column, 2nd row has 2 column, 3rd row has 3 columns and so on )
We run the "j" loop i.e the inner loop from 1 to i.
We run the "j" loop in increasing order ( i.e from 1 to i ) 
since rows is in increasing order(1 2 3 ... i single line value)

We print "value" in the inner loop 
since number of columns = number of "value" to be printed.
NOTE : WE CAN PRINT ANYTHING WHICH WE WANT. ( for ex : printf("%d",j) or printf("%d",i) or anything according to pattern.

Lastly after one rotation (completion) of inner loop we need to go to the next line so we use printf("\n") statement after closing the 
inner loop and before closing the outer loop.

Comments are given to make you understand the program..

*/

#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
// Declaring variable i,j to iterate outer and inner loop respectively
// we are using "i" for rows and "j" for columns
int i,j;

// Since we have five rows so we iterate the "i" loop from 5 to 1 ( i.e 5 times )
// We run the "i" loop in decreasing order ( i.e from 5 to 1 ) since rows is in decreasing order(5 4 3 ... 1 diagnol value)
for(i=5;i>=1;i--)
{
// Notice in above pattern Number of columns = Position of rows ( i.e 1st row has 1 column, 2nd row has 2 column, 3rd row has 3 columns and so on )
// We run the "j" loop in increasing order ( i.e from 1 to i ) since rows is in increasing order(1 2 3 ... i single line value)
for(j=1;j<=i;j++)
{
/* Printing "value" in the inner loop since number of columns = number of "value" to be printed.
Here we print the value of "j" since value to be printed is the position of the column according to initialisation in loop. */

printf("%d ",j);
}
  
// After completion of printing "value" in row we need to go to the next row.
printf("\n");
}
 
getch(); // Linux user - Remove this
return 0;
}

8. Program to print the given pattern
// Program to print the given patter :

/* 

5 4 3 2 1
5 4 3 2
5 4 3
5 4
5

*/

/* 

REMEMBER : We always design logic according to the pattern.

LOGIC:

Consider the diagonal to be rows and a single line to be column.

Number of loops required = 2 
( since we have rows and columns. 1 loop for row and 1 loop for column.)

It would be a nested loop since Number of columns depends on Number of rows.

Outer loop will be for rows and inner loop will be for columns.

outer loop "i" runs from 1 to 5 since we have 5 rows.
We run the "i" loop in increasing order 
( i.e from 1 to 5 ) since rows is in increasing order(1 2 3 4 5 diagnol value)

Notice in above pattern Number of columns = Position of rows 
( i.e 1st row has 1 column, 2nd row has 2 column, 3rd row has 3 columns and so on )
We run the "j" loop i.e the inner loop from 1 to i.
We run the "j" loop in decreasing order ( i.e from 5 to i ) 
since rows is in decreasing order(5 4 ... i single line value)

We print "value" in the inner loop 
since number of columns = number of "value" to be printed.
NOTE : WE CAN PRINT ANYTHING WHICH WE WANT. ( for ex : printf("%d",j) or printf("%d",i) or anything according to pattern.

Lastly after one rotation (completion) of inner loop we need to go to the next line so we use printf("\n") statement after closing the 
inner loop and before closing the outer loop.

Comments are given to make you understand the program..

*/

#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
// Declaring variable i,j to iterate outer and inner loop respectively
// we are using "i" for rows and "j" for columns
int i,j;
 
/* Since we have five rows so we iterate the "i" loop from 5 to 1 ( i.e 5 times )
We run the "i" loop in decreasing order ( i.e from 5 to 1 ) 
since rows is in decreasing order(5 4 3 ... 1 diagnol value) */
 
for(i=1;i<=5;i++)
{
/* Notice in above pattern Number of columns = Position of rows ( i.e 1st row has 1 column, 2nd row has 2 column, 3rd row has 3 columns and so on )
We run the "j" loop in increasing order ( i.e from 1 to i ) since rows is in increasing order(1 2 3 ... i single line value) */
for(j=5;j>=i;j--)
{
/* Printing "value" in the inner loop since number of columns = number of "value" to be printed.
Here we print the value of "j" since value to be printed is the position of the column according to initialisation in loop. */
   
printf("%d ",j);
}
 
// After completion of printing "value" in row we need to go to the next row.
printf("\n");
}

getch(); // Linux user - Remove this

return 0;
}

9. Program to print the given pattern
// Program to print the given patter :

/* 
        *
      * *
    * * *
  * * * *
* * * * *


*/

/* 

REMEMBER : We always design logic according to the pattern.

LOGIC:

Simplifying the pattern :

--------*
------* *
----* * *
--* * * *
* * * * *

Here ( - ) indicates space before star ( asterik ) to be printed.
i.e -- indicates two space, ---- indicates four space and so on.

The above pattern is similar to pattern :
*
* *
* * *
* * * *
* * * * *

The only difference is we print spaces before the "values". 
So design logic according to this and print spaces before that.
Its as simple as you like.

Consider the diagonal to be rows and a single line to be column.

Now earlier we worked only on rows and columns without spaces before the printed "value" 
but now we have spcaes as well. So we include another loop for spaces.

Number of loops required = 3 
( since we have rows and columns and also spaces. 1 loop for row and 1 loop for column 
and 1 loop for spaces.)

It would be a nested loop since Number of columns depends on Number of rows.

Outer loop will be for rows and inner loop will be for columns. 
One more inner loop will be for printing space.

outer loop "i" runs from 1 to 5 since we have 5 rows.

Notice in above pattern Number of columns = Position of rows 
( i.e 1st row has 1 column, 2nd row has 2 column, 3rd row has 3 columns and so on )
We run the "j" loop i.e the inner loop from 1 to i.

Now For spaces : If you notice the spaces are in decreasing manner 
( i.e 8 spaces, 6 spaces, 4 spaces, 2 spaces and then none )
We can print the spaces by utilising "i" value 
but to simplify the pattern and make it easier we use another value m=8 and 
reduce it by 2 after the completion of loop for spaces.
i.e We run the inner loop "s" from 1 to m.

We print "value" in the inner loop 
since number of columns = number of "value" to be printed.
NOTE : WE CAN PRINT ANYTHING WHICH WE WANT. ( for ex : printf("%d",j) or printf("%d",i) or anything according to pattern.

Lastly after one rotation (completion) of inner loop we need to go to the next line so we use printf("\n") statement after closing the 
inner loop and before closing the outer loop.

Comments are given to make you understand the program..

*/

#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
// Declaring variable i, j, s to iterate outer and inner loop respectively
// we are using "i" for rows, "j" for columns ans "s" for spaces.
int i, j, s;
 
// Initialising m = 8 as total number of space = 8
int m = 8;
 
// Since we have five rows so we iterate the "i" loop from 5 to 1 ( i.e 5 times )
for(i=1;i<=5;i++)
{
// Printing spaces
for(s=1;s<=m;s++)
{
printf(" ");
}
// Reducing value of "m" by 2 to reduce spaces
m=m-2;
  
/* Notice in above pattern Number of columns = Position of rows ( i.e 1st row has 1 column, 2nd row has 2 column, 3rd row has 3 columns and so on ) */
for(j=1;j<=i;j++)
{
// Printing "value" in the inner loop since number of columns = number of "value" to be printed.
printf("* ");
}
  
// After completion of printing "value" in row we need to go to the next row.
printf("\n");
}
 
getch(); // Linux user - Remove this

return 0;
}

10. Program to print the given pattern
// Program to print the given patter :

/* 
        1
      1 2
    1 2 3
  1 2 3 4
1 2 3 4 5


*/

/* 

REMEMBER : We always design logic according to the pattern.

LOGIC:

Simplifying the pattern :

--------1
------1 2
----1 2 3
--1 2 3 4
1 2 3 4 5

Here ( - ) indicates space before "value" to be printed.
i.e -- indicates two space, ---- indicates four space and so on.

The above patter is similar to patern:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

The only difference is we print spaces before the "values". 
So design logic according to this and print spaces before that.
Its as simple as you like.

Consider the diagonal to be rows and a single line to be column.

Now earlier we worked only on rows and columns without spaces before the printed "value" 
but now we have spcaes as well. So we include another loop for spaces.

Number of loops required = 3 
( since we have rows and columns and also spaces. 1 loop for row and 1 loop for column 
and 1 loop for spaces.)

It would be a nested loop since Number of columns depends on Number of rows.

Outer loop will be for rows and inner loop will be for columns. 
One more inner loop will be for printing space.

outer loop "i" runs from 1 to 5 since we have 5 rows.
We run the "i" loop in increasing order ( i.e from 1 to 5 ) 
since rows is in increasing order(1 2 3 4 5 diagnol value)

Notice in above pattern Number of columns = Position of rows 
( i.e 1st row has 1 column, 2nd row has 2 column, 3rd row has 3 columns and so on )
We run the "j" loop i.e the inner loop from 1 to i.
We run the "j" loop in increasing order ( i.e from 1 to i ) 
since column is in increasing order(1 2 3 4 5 single line value)

Now For spaces : If you notice the spaces are in decreasing manner 
( i.e 8 spaces, 6 spaces, 4 spaces, 2 spaces and then none )
We can print the spaces by utilising "i" value but to simplify the pattern 
and make it easier we use another value m=8 and 
reduce it by 2 after the completion of loop for spaces.
i.e We run the inner loop "s" from 1 to m.

We print "value" in the inner loop 
since number of columns = number of "value" to be printed.
NOTE : WE CAN PRINT ANYTHING WHICH WE WANT. ( for ex : printf("%d",j) or printf("%d",i) or anything according to pattern.

Lastly after one rotation (completion) of inner loop we need to go to the next line so we use printf("\n") statement after closing the 
inner loop and before closing the outer loop.

Comments are given to make you understand the program..

*/

#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
// Declaring variable i, j, s to iterate outer and inner loop respectively
// we are using "i" for rows, "j" for columns ans "s" for spaces.
int i, j, s;
 
// Initialising m = 8 as maximum number of space = 8
int m = 8;
 
// Since we have five rows so we iterate the "i" loop from 5 to 1
/* We run the "i" loop in increasing order ( i.e from 1 to 5 ) 
since rows is in increasing order(1 2 3 4 5 diagnol value) */

for(i=1;i<=5;i++)
{
// Printing spaces
for(s=1;s<=m;s++)
{
printf(" ");
}
// Reducing value of "m" by 2 to reduce spaces
m=m-2;
  
/* Notice in above pattern Number of columns = Position of rows 
( i.e 1st row has 1 column, 2nd row has 2 column, 3rd row has 3 columns and so on )
We run the "j" loop in increasing order ( i.e from 1 to i ) 
since column is in increasing order(1 2 ... i single line value) */

for(j=1;j<=i;j++)
{
// Printing "value" in the inner loop since number of columns = number of "value" to be printed.
/* Here we print the value of "j" since value to be printed is the position of the column according to initialisation in loop. */

printf("%d ",j);
}
  
// After completion of printing "value" in row we need to go to the next row.
printf("\n");
}
 
getch(); // Linux user - Remove this

return 0;
}

More Informative posts :

C PROGRAMS : PATTERN

C PROGRAMS : PATTERN

C PROGRAMS : PATTERN
1. Program to print the given pattern
/* 

* 
* * 
* * * 
* * * * 
* * * * * 

*/

/* 

REMEMBER : We always design logic according to the pattern.

LOGIC:

Number of loops required = 2 
(since we have rows and columns. 1 loop for row and 1 loop for column.)

It would be a nested loop since Number of columns depends on Number of rows.

Outer loop will be for rows and inner loop will be for columns.
outer loop "i" runs from 1 to 5 since we have 5 rows.

Notice in above pattern Number of columns = Position of rows 
( i.e 1st row has 1 column, 2nd row has 2 column, 3rd row has 3 columns and so on )
so we run the "j" loop i.e the inner loop from 1 to "i".

We print "*" in the inner loop since number of columns = number of star to be printed.
NOTE : WE CAN PRINT ANYTHING WHICH WE WANT. ( for ex : printf("%d",j) or printf("%d",i) or anything according to pattern.

Lastly after one rotation (completion) of inner loop we need to go to the next line so we use printf("\n") statement after closing the 
inner loop and before closing the outer loop.

Comments are given to make you understand the program..

*/

#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
 // Declaring variable i,j to iterate outer and inner loop respectively
 // we are using "i" for rows and "j" for columns
 int i,j;
 
 // Since we have five rows so we iterate the "i" loop from 1 to 5 ( i.e 5 times )
 for(i=1;i<=5;i++)
 {
    /* Notice in above pattern Number of columns = Position of rows 
    ( i.e 1st row has 1 column, 2nd row has 2 column, 3rd row has 3 columns and so on )
    so we run the "j" loop i.e the inner loop from 1 to "i". */
    for(j=1;j<=i;j++)
    {
   // Printing "*" in the inner loop since number of columns=number of star to be printed
   printf("* ");
    }
  
  // After completion of printing "*" in row we need to go to the next row.
  printf("\n");
 }
 
 getch(); // Linux user - Remove this

 return 0;
}

2. Program to print the given pattern
// Program to print the given patter :

/* 

* 
* * 
* * * 
* * * * 
* * * * * 
* * * * * * ..... till n rows

Here n = any integer value entered by user.
  n represents rows.
  
Example : 

For n=3

*
* * 
* * *

For n=5

* 
* * 
* * *
* * * *
* * * * * 

*/

/*

REMEMBER : We always design logic according to the pattern. 

LOGIC:

In above Pattern Number of rows = n

Number of loops required = 2 
( since we have rows and columns. 1 loop for row and 1 loop for column. )

It would be a nested loop since Number of columns depends on Number of rows.

Outer loop will be for rows and inner loop will be for columns.
outer loop "i" runs from 1 to n since we have n rows.

Notice in above pattern Number of columns = Position of rows 
( i.e 1st row has 1 column, 2nd row has 2 column, 3rd row has 3 columns and so on )
so we run the "j" loop i.e the inner loop from 1 to "i".

We print "*" in the inner loop since number of columns = number of star to be printed.
NOTE : WE CAN PRINT ANYTHING WHICH WE WANT. ( for ex : printf("%d",j) or printf("%d",i) or anything according to pattern.

Lastly after one rotation (completion) of inner loop we need to go to the next line so we use printf("\n") statement after closing the 
inner loop and before closing the outer loop.

Comments are given to make you understand the program..

*/

#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
 // Declaring variable i,j to iterate outer and inner loop respectively
 // we are using "i" for rows and "j" for columns
 int i,j;
 
 //Declaring Variale n=rows
 int n;
 
 // Entering number of rows from user
 printf("Enter number of rows : ");
 scanf("%d",&n);
 
 // Since we have "n" number of  rows so we iterate the "i" loop from 1 to n
 for(i=1;i<=n;i++)
 {
  /* Notice in above pattern Number of columns = Position of rows 
  ( i.e 1st row has 1 column, 2nd row has 2 column and so on )
   so we run the "j" loop i.e the inner loop from 1 to "i". */
  for(j=1;j<=i;j++)
  {
   /* Printing "*" in the inner loop 
   since number of columns = number of star to be printed. */
   printf("* ");
  }
  
  // After completion of one row we need to go to the next line.
  printf("\n");
 }
 
 getch(); // Linux user - Remove this

 return 0;
}

3. Program to print the given pattern
// NOTE : For Better understanding read both LOGIC and COMMENTS

// Program to print the given patter :

/* 

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5 .... till n rows

Here n = any integer value entered by user.
  n represents rows.

Example :

For n=2

1
1 2

For n=6

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5 
1 2 3 4 5 6

*/

/* 

REMEMBER : We always design logic according to the pattern.

LOGIC:

In above Pattern Number of rows = n

Consider the diagonal to be rows and a single line to be column.

Number of loops required = 2 
( since we have rows and columns. 1 loop for row and 1 loop for column. )

It would be a nested loop since Number of columns depends on Number of rows.

Outer loop will be for rows and inner loop will be for columns.
outer loop "i" runs from 1 to n since we have n rows.
We run the "i" loop in increasing order ( i.e from 1 to n ) 
since rows is in increasing order(1 2 3 ... n diagnol value)


Notice in above pattern Number of columns = Position of rows 
( i.e 1st row has 1 column, 2nd row has 2 column, 3rd row has 3 columns and so on )
so we run the "j" loop i.e the inner loop from 1 to "i".
We run the "j" loop in increasing order ( i.e from 1 to i ) 
since column is in increasing order ( 1 2 3 ... i single line value )

We print "value" in the inner loop since number of columns = number of "values" to be printed.
NOTE : WE CAN PRINT ANYTHING WHICH WE WANT. ( for ex : printf("%d",j) or printf("%d",i) or anything according to pattern.

Lastly after one rotation (completion) of inner loop we need to go to the next line so we use printf("\n") statement after closing the 
inner loop and before closing the outer loop.

Comments are given to make you understand the program..

*/

#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
 // Declaring variable i,j to iterate outer and inner loop respectively
 // we are using "i" for rows and "j" for columns
 int i,j;
 
 //Declaring Variale n=rows
 int n;
 
 // Entering number of rows from user
 printf("Enter number of rows : ");
 scanf("%d",&n);
 
 // Since we have five rows so we iterate the "i" loop from 1 to 5
 for(i=1;i<=n;i++)
 {
  /* Notice in above pattern Number of columns = Position of rows 
  ( i.e 1st row has 1 column, 2nd row has 2 column and so on )
   so we run the "j" loop from 1 to "i" */
  for(j=1;j<=i;j++)
  {
   /* Printing "value" in the inner loop 
   since number of columns=number of "value" to be printed.
   Here we print the value of "j" 
   since value to be printed is the position of the column. */
   printf("%d ",j);
  }
  
  /* After completion of printing "value" in row
   we need to go to the next row. */
  printf("\n");
 }
 
 getch(); // Linux user - Remove this

 return 0;
}

4. Program to print the given pattern
// Program to print the given patter :

/* 

1
2 2
3 3 3
4 4 4 4
5 5 5 5 5 .... till n rows

Here n = any integer value entered by user.
  n represents rows.

Example :

For n=3

1
2 2
3 3 3

For n=6

1
2 2 
3 3 3 
4 4 4 4
5 5 5 5 5
6 6 6 6 6 6

*/

/* 

REMEMBER : We always design logic according to the pattern.

LOGIC:

In above Pattern Number of rows = n

Number of loops required = 2 
( since we have rows and columns. 1 loop for row and 1 loop for column. )

It would be a nested loop since Number of columns depends on Number of rows.

Outer loop will be for rows and inner loop will be for columns.
outer loop "i" runs from 1 to n since we have n rows.

Notice in above pattern Number of columns = Position of rows 
( i.e 1st row has 1 column, 2nd row has 2 column, 3rd row has 3 columns and so on )
so we run the "j" loop i.e the inner loop from 1 to "i".

We print "value" in the inner loop 
since number of columns = number of "values" to be printed.
NOTE : WE CAN PRINT ANYTHING WHICH WE WANT. ( for ex : printf("%d",j) or printf("%d",i) or anything according to pattern.

Lastly after one rotation (completion) of inner loop we need to go to the next line so we use printf("\n") statement after closing the 
inner loop and before closing the outer loop.

Comments are given to make you understand the program..

*/

#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
 // Declaring variable i,j to iterate outer and inner loop respectively
 // we are using "i" for rows and "j" for columns
 int i,j;
 
 //Declaring Variale n=rows
 int n;
 
 // Entering number of rows from user
 printf("Enter number of rows : ");
 scanf("%d",&n);
 
 // Since we have five rows so we iterate the "i" loop from 1 to 5
 for(i=1;i<=n;i++)
 {
  /* Notice in above pattern Number of columns = Position of rows 
  ( i.e 1st row has 1 column, 2nd row has 2 column and so on )
   so we run the "j" loop from 1 to "i" */
   
  for(j=1;j<=i;j++)
  {
   /* Printing "value" in the inner loop 
   since number of columns = number of "value" to be printed.
   Here we print the value of "i" 
   since value to be printed is the position of the row. */
   
   printf("%d ",i);
  }
  
  /* After completion of printing "value" in row 
  we need to go to the next row. */
  printf("\n");
 }
 
 getch(); // Linux user - Remove this

 return 0;
}

5. Program to print the given pattern
// Program to print the given patter :

/* 

1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
5 5 5 5 5

*/

/* 

REMEMBER : We always design logic according to the pattern.

LOGIC:

Number of loops required = 2 
( since we have rows and columns. 1 loop for row and 1 loop for column. )

It would be a nested loop since Number of columns depends on Number of rows.

Outer loop will be for rows and inner loop will be for columns.
outer loop "i" runs from 1 to 5 since we have 5 rows.

Inner loop also runs from 1 to 5 since we have 5 columns in each row.
so we run the "j" loop i.e the inner loop from 1 to 5.

We print "value" in the inner loop 
since number of columns = number of "value" to be printed.
NOTE : WE CAN PRINT ANYTHING WHICH WE WANT. ( for ex : printf("%d",j) or printf("%d",i) or anything according to pattern.

Lastly after one rotation (completion) of inner loop we need to go to the next line so we use printf("\n") statement after closing the 
inner loop and before closing the outer loop.

Comments are given to make you understand the program..

*/

#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
 // Declaring variable i,j to iterate outer and inner loop respectively
 // we are using "i" for rows and "j" for columns
 int i,j;
 
 // Since we have five rows so we iterate the "i" loop from 1 to 5
 for(i=1;i<=5;i++)
 {
  // We have five columns as well so we iterate the "j" loop from 1 to 5.
  for(j=1;j<=5;j++)
  {
   /* Printing "value" in the inner loop 
   since number of columns = number of "value" to be printed.
   Here we print the value of "i" 
   since value to be printed is the position of the row. */
   
   printf("%d ",i);
  }
  
  /* After completion of printing "*" in row 
  we need to go to the next row. */
  
  printf("\n");
 }
 
 getch(); // Linux user - Remove this

 return 0;
}

More Informative posts :

Other References : 
C PROGRAMS : DYNAMIC MEMORY ALLOCATION

C PROGRAMS : DYNAMIC MEMORY ALLOCATION

C PROGRAMS : DYNAMIC MEMORY ALLOCATION
5. Program to find the sum of "n" elements
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

#include<stdlib.h>

int main()
{
    int *ptr,n,i, sum=0;
    
    printf("Enter number of elements : ");
    scanf("%d",&n);
    
    // Allocating memory dynamically
    ptr=(int *)malloc(n*sizeof(int));
    
    // Inputing elements
    printf("Enter Elements : ");
    for(i=0;i<n;i++)
    {
       scanf("%d",(ptr+i));
    }
    
    // Calculating sum
   for(i=0;i<n;i++)
    {
     sum = sum + *(ptr+i);
    }  
    
    // Displaying elements
    printf("Entered elements : ");
    for(i=0;i<n;i++)
    {
    printf("%d ",*(ptr+i));
    }
 
    // Displaying sum
    printf("\nSum of elements : %d",sum);

    // Deallocating memory
    free(ptr);
 
    getch(); // Linux user - Remove this

    return 0;
}

6. Program to sort "n" elements in ascending order
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

#include<stdlib.h>

int main()
{
    int *ptr,n,i,j, temp;
    
    printf("Enter number of elements : ");
    scanf("%d",&n);
    
    // Allocating memory dynamically
    ptr=(int *)malloc(n*sizeof(int));
    
    // Inputing elements
    printf("Enter Elements : ");
    for(i=0;i<n;i++)
    {
       scanf("%d",(ptr+i));
    }
    
    // Displaying elements
    printf("Entered elements : ");
    for(i=0;i<n;i++)
    {
       printf("%d ",*(ptr+i));
    }
 
    // Sorting elements
   for(i=0;i<n;i++)
        {
     for(j=i+1;j<n;j++)
     {
      if( *(ptr+i) > *(ptr+j) )
      {
       temp=*(ptr+i);
       *(ptr+i)=*(ptr+j);
       *(ptr+j)=temp;
      }
     }
  }  
    
 
 // Displaying sorted elements
 printf("\nElements sorted in ascending order : ");
 for(i=0;i<n;i++)
        {
        printf("%d ",*(ptr+i));
 }
 
 // Deallocating memory
 free(ptr);
 
    getch(); // Linux user - Remove this

    return 0;
}

7. Program to search an element
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

#include<stdlib.h>

int main()
{
    int *ptr,n,i, sum=0;
    
    //Declaring variable es=element to be searched, flag=indicates if element found or not
    int es, flag=0;
    
    printf("Enter number of elements : ");
    scanf("%d",&n);
    
    // Allocating memory dynamically
    ptr=(int *)malloc(n*sizeof(int));
    
    // Inputing elements
    printf("Enter Elements : ");
    for(i=0;i<n;i++)
    {
    scanf("%d",(ptr+i));
 }
    
    // Displaying elements
    printf("Entered elements : ");
    for(i=0;i<n;i++)
    {
    printf("%d ",*(ptr+i));
 }
 
    // Inputing element to be searched
    printf("\nEnter element to be searched : ");
    scanf("%d",&es);
    
    // Searching element
   for(i=0;i<n;i++)
    {
     if( *(ptr+i)==es)
     {
      flag=1;
      break;
     }
 }  
 
 // Displaying element searched or not
 if(flag==1)
 printf("Element found ");
 
 else
 printf("Element not found ");
 
 // Deallocating memory
 free(ptr);
 
    getch(); // Linux user - Remove this

    return 0;
}

8. Program to enter three strings and sort them in ascending order alphabetically
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

#include<stdlib.h>
#include<string.h>
int main()
{
 int i,j;
 
 // Declaring variable temp=tempeorary helps in swapping strings
 char temp[50];
 
 // Here 3 = number of string which we can enter
 char *p1[3];

 // Inputing string
 printf("Enter three strings : ");
 for(i=0;i<3;i++)
 {
  p1[i]=(char *)malloc(12*sizeof(char));
  gets(p1[i]);
 }
 
 // Performing sorting
 for(i=0;i<3;i++)
 {
  for(j=i+1;j<3;j++)
  {
   // swapping strings
   if(strcmp(p1[i],p1[j])>0)
   {
    strcpy(temp,p1[i]);
    strcpy(p1[i],p1[j]);
    strcpy(p1[j],temp);
   }
  }
 }
 
 // Displaying sorted string
 printf("Sorted string : \n");
 for(i=0;i<3;i++)
 puts(p1[i]);

 getch(); // Linux user - Remove this

 return 0;
}

C PROGRAMS : DYNAMIC MEMORY ALLOCATION

C PROGRAMS : DYNAMIC MEMORY ALLOCATION

C PROGRAMS : DYNAMIC MEMORY ALLOCATION
1. Program to dynamically allocate memory using calloc for "n" integer elements, input elements, display it and then free the memory
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

#include<stdlib.h>

int main()
{
    int *ptr,n,i;
    
    printf("Enter number of elements : ");
    scanf("%d",&n);
    
    // Allocating memory dynamically
    ptr=(int *)calloc(n,sizeof(int));
    
    // Inputing elements
    printf("Enter Elements : ");
    for(i=0;i<n;i++)
    {
    scanf("%d",(ptr+i));
 }
    
    // Displaying elements
    printf("Entered elements : ");
    for(i=0;i<n;i++)
    {
    printf("%d ",*(ptr+i));
 }
 
 // Deallocating memory
 free(ptr);
 
    getch(); // Linux user - Remove this

    return 0;
}

2. Program to dynamically allocate memory using malloc for "n" integer elements, input elements, display it and then free the memory
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

#include<stdlib.h>

int main()
{
    int *ptr,n,i;
    
    printf("Enter number of elements : ");
    scanf("%d",&n);
    
    // Allocating memory dynamically
    ptr=(int *)malloc(n*sizeof(int));
    
    // Inputing elements
    printf("Enter Elements : ");
    for(i=0;i<n;i++)
    {
    scanf("%d",(ptr+i));
 }
    
    // Displaying elements
    printf("Entered elements : ");
    for(i=0;i<n;i++)
    {
    printf("%d ",*(ptr+i));
 }
 
 // Deallocating memory
 free(ptr);
 
    getch(); // Linux user - Remove this

    return 0;
}

3. Program to dynamically allocate memory using malloc for "n" integer elements, input elements and then display it. Then reallocate memory for the same pointer variable, input elements, display it and then free the memory
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

#include<stdlib.h>

int main()
{
    int *ptr,n,i;
    printf("Enter number of elements : ");
    scanf("%d",&n);
    
    // Allocating memory dynamically
    ptr=(int *)malloc(n*sizeof(int));
    
    // Inputing elements
    printf("Enter Elements : ");
    for(i=0;i<n;i++)
    {
    scanf("%d",(ptr+i));
 }
    
    // Displaying elements
    printf("Entered elements : ");
    for(i=0;i<n;i++)
    {
    printf("%d ",*(ptr+i));
 }
 
 printf("\nEnter number of elements : ");
    scanf("%d",&n);
    
    // Reallocating memory 
 ptr=realloc(ptr,n);
 
 // Inputing elements
    printf("Enter Elements : ");
    for(i=0;i<n;i++)
    {
    scanf("%d",(ptr+i));
 }
    
    // Displaying elements
    printf("Entered elements : ");
    for(i=0;i<n;i++)
    {
    printf("%d ",*(ptr+i));
 }
 
 // Deallocating memory
 free(ptr);
    
    getch(); // Linux user - Remove this

    return 0;
}

4. Program to find the length of the string
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

#include<stdlib.h>
void main()
{
 char *ptr;
 
 // Declaring variable i=to iterate loop, l=lenght of string
 int i, l=0;
 
    // Allocating memory
 ptr=(char *)malloc(12*sizeof(char));
 
 // Inputing string
 printf("Enter string : ");
 gets(ptr);
 
 // Displaying string
 printf("Entered string : ");
 puts(ptr);
 
 // Calculating length
 while(ptr[l]!='\0')
 {
 l++;
 }
 
 // Printing length of string
 printf("Lenght of string : %d",l);
 
 // Deallocating memory
 free(ptr);
 
 getch(); // Linux user - Remove this

 return 0;
}

C PROGRAMS : POINTERS

C PROGRAMS : POINTERS

C PROGRAMS : POINTERS
6. Program to find maximum element of an array of elements using pointer
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
 // Declaring array variable "a" of size 10, i=to iterate loop, max=maximum element
 int a[10], i, max;
 
 // Declaring pointer variable pointing to the the base address i.e ( a[0] )of array 'a'  
 int *ptr=a; // It is equivalent to : int *ptr; ptr=&a;
 
 // Inputing array elements
 printf("Enter 10 elements in an array : ");
 for(i=0;i<10;i++)
 {
  scanf("%d",&a[i]);
 }
 
 // Assigning first element to max, ptr points to second element after post increment
 max=*ptr++;
 
 // Determining maximum value
 for(i=0;i<10;i++)
 {
  if(*ptr > max)
  {
   max=*ptr;
   ptr++;
  }
 }
 
 // Printing maximum value
 printf("\nMaximum value : %d",max);
 
 getch(); // Linux user - Remove this

 return 0;
}

7. Program to print sum of all the elements in 2D array using pointer
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
 // Declaring 2D array and initializing it
 int a[3][4]={{3,4,5,6},{5,6,7,8},{8,9,10,11}};
 
 // Declaring varaible sum
 int sum=0;
 
 // Declaring pointer variable
 int *ptr, *ptre;
 
 ptr=&a[0][0]; // points to address of first element
 ptre=ptr+12; 
 
 // Calculating sum
 while(ptr<ptre)
 {
  sum+=*ptr++;
 }
 
 // Displaying calculated sum
 printf("Sum of all elements : %d",sum);
 
 getch(); // Linux user - Remove this

 return 0;
}

8. Program to sort an array of elements using pointers
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{

 // Declaring array and initializing it
 int a[]={5,6,3,4,5};

 // Declaring pointer variable *p, *q and temp=tempeorary that helps in swapping numbers
 int *p,*q,temp;

 // Declaring variable i, j to iterate loop
 int i,j;

 //Displaying array elements
 printf("Array elements : ");
 for(i=0;i<5;i++)
 {
  printf("%d ",a[i]);
 }
 printf("\n");
 
 // Pointing to the address of first element
 p=&a[0];
 
 // Performing sorting
 for(i=0;i<5;i++)
 {
  for(j=i+1;j<5;j++)
  {
   // Swapping elements
   if(*(p+i) > *(p+j))
   {
    temp=*(p+i);
    *(p+i)=*(p+j);
    *(p+j)=temp;
   }
  }
 }

 // Displaying sorted elements
 printf("Sorted elements : ");
 for(i=0;i<5;i++)
 {
  printf("%d ",a[i]);
 }
 
 getch(); // Linux user - Remove this

 return 0;
}

9. Program to sort string using pointers
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
 // Declaring variable str
 char str[50], *p;
 
 // Declaring variable i, j to iterate loop
 int i, j;
 
 // Declaring variable l=length, temp=tempeorary that helps in swapping
 int l=0, temp;
 
 // Inputing string
 printf("Enter any string : ");
 gets(str);
 
 p=&str[0];
 
 // finding length
 while(str[l]!='\0')
 l++;
 
 // Performing sorting
 for(i=0;i<l;i++)
 {
  for(j=i+1;j<l;j++)
  {
   // Swapping characters
   if(*(p+i)>*(p+j))
   {
    temp=*(p+i);
    *(p+i)=*(p+j);
    *(p+j)=temp;
   }
  }
 }
 
 // Displaying sorted string
 printf("Sorted string : \n");
 p=&str[0];
 
 for(i=0;i<l;i++)
 {
  printf("%c\n",*(p+i));
 }
 
 getch(); // Linux user - Remove this

 return 0;
}

10. Program to search given element in an array using pointers
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{

 // Declaring array and initializing it, *p=points to the address of first element
 int a[]={5,6,3,4,5}, *p;
 p=&a[0];
 
 // Declaring variable i = to iterate loop
 int i;
 
 /* Declaring variable es = element to be searched, flag = to indicate 
 if element is present or not */
 int es, flag=0;
 
 // Displaying array element
 printf("Array elements : ");
 for(i=0;i<5;i++)
 {
  printf("%d ",*(p+i));
 }
 
 // Inputing element to be searched
 printf("\nEnter element to be searched : ");
 scanf("%d",&es);
 
 // Searching element
 for(i=0;i<5;i++)
 {
  if(*(p+i)==es)
  {
   flag=1;
   break;
  }
 }
 
 // Displaying whether element found or not
 if(flag==1)
 printf("Element found ");
 
 else
 printf("Element not found ");
 
 getch(); // Linux user - Remove this

 return 0;
}

11. Program to demonstrate the use of pointer to pointer
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
 int x,*p,**q;
             
    printf("\nEnter a number : ");
    scanf("%d",&x);
    
    p=&x;
    q=&p;
    
    printf("\nValue of x=%d",x);
    printf("\nValue of x through pointer=%d",*p);
    printf("\nVal. of x through pointer to  pointer=%d",**q);
    printf("\nValue of p=%u",p);
    printf("\nValue of q=%u",q);
    printf("\nAddres of p=%u",q);
     
 getch(); // Linux user - Remove this

 return 0;
}
C PROGRAMS : POINTERS

C PROGRAMS : POINTERS

C PROGRAMS : POINTERS
1. Program to accept two numbers and print its address along with the numbers
# include <stdio.h>
# include <conio.h> // Linux user - Remove this

int main( )
{
 // Declaring variable a, b = two numbers
 int a, b;
 
 // Inputing two numbers
 printf("Enter first number : ");
 scanf("%d",&a);
 
 printf("Enter first number : ");
 scanf("%d",&b);
 
 // Printing address along with number
 printf("First number : %d, Address : %d\n",a, &a);
 printf("First number : %d, Address : %d\n",*(&a), &a);
 
 printf("Second number : %d, Address : %d\n",b, &b);
 printf("Second number : %d, Address : %d",*(&b), &b);
 
 getch(); // Linux user - Remove this

 return 0;
}

2. Program to accept two numbers and print the sum of given two numbers by using pointers
# include <stdio.h>
# include <conio.h> // Linux user - Remove this

int main( )
{
 // Declaring variable a, b = two numbers
 int a, b;
 
 // Declaring variale s=sum
 int s=0;
 
 // Inputing two numbers
 printf("Enter first number : ");
 scanf("%d",&a);
 
 // Inputing second number
 printf("Enter first number : ");
 scanf("%d",&b);
 
 // Determining sum
 s=*(&a)+*(&b);
 
 // Printing sum
 printf("Sum = %d",s);
 
 getch( ); // Linux user - Remove this

 return 0;
}

3. Program to interchange two values using pointers
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
 // Declaring variable a, b for two values, swap to interchange value
 int a, b, swap;
 
 // Declaring pointer that holds address of two values
 int *c, *d;
 
 // Storing address
 c=&a;
 d=&b;
 
 // Inputing values
 printf("Enter first value a : ");
 scanf("%d",&a);
 
 printf("Enter second value b : ");
 scanf("%d",&b);
 
 // Printing original value
 printf("Original value : a = %d, b = %d\n",a, b);
 
 // Interchanging value
 swap=*c;
 *c=*d;
 *d=swap;
 
 // Printing interchanged value
 printf("Interchanged value : a = %d, b = %d ",a, b);
  
 getch(); // Linux user - Remove this

 return 0;
}

4. Implement a function interchange() to interchange two values using pointers
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
 // Declaring variable a, b for two values
 int a,b;

 // Inputing values
 printf("Enter first value a : ");
 scanf("%d",&a);
 
 printf("Enter second value b : ");
 scanf("%d",&b);
 
 // Printing original value
 printf("Original value : a = %d, b = %d\n",a, b);
 
 // Calling function interchange
 interchange(&a, &b);
 
 getch(); // Linux user - Remove this

 return 0;
}

void interchange(int *x, int *y)

{
 int swap;
 
 // Interchanging value
 swap=*x;
 *x=*y;
 *y=swap;
 
 // Printing interchanged value
 printf("Interchanged value : a = %d, b = %d ",*x, *y);
}


5. Program to sum all the elements of an array using pointer
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
 // Declaring array variable "a" of size 10 and initializing it
 int a[10]={21, -2, 4, 6, 9, 12, 43, 22, 4, 8}, sum=0;
 
 // Declaring pointer type variable pointing to the the base address i.e ( a[0] ) of array 'a'  
 int *ptr=a; // It is equivalent to : int *ptr; ptr=&a;
 
 // Declaring variable ptre = stores address of 11th element
 int *ptre;
 ptre = ptr+10;
 
 // Calculating sum
 while(ptr<ptre)
 {
  sum+=*ptr++; // sum=sum + *ptr++
 }
 
 // Displaying calculated sum
 printf("Sum of all elements : %d ",sum);
 
 getch(); // Linux user - Remove this

 return 0;
}


C PROGRAMS : STRINGS

C PROGRAMS : STRINGS

C PROGRAMS : STRINGS
21. Program to accept a string and print each word of the string separately also print total number of words
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

#include<string.h>

int main()
{
 // Declaring variable str=string, word=to store the word
 char str[50], word[50];
 
 /* Declaring variable cw=count words, i=to iterate loop, 
 j=works as index position of word */
 int cw=0, i, j=0;
 
 // Inputing string
 printf("Enter any string : ");
 gets(str);
 
 /* We are concatinating a space here assuming user does not 
 give space at the last */
 strcat(str," ");

 // Printing string
 printf("Entered string : %s",str);
 
 //Determing total number of words and displaying it
 for(i=0;str[i]!='\0';i++)
 {
  if(str[i]!=' ')
  {
   word[j++]=str[i];
  }
  else
  {
   word[j]='\0';
   printf("\nWord : %s",word);
   cw++;
   j=0;
  }
 }
 
 // Printing total words
 printf("\nTotal number of words : %d",cw);
 
 getch(); // Linux user - Remove this

 return 0;
}

22. Program to accept a string and display vowels frequency( total number of vowels)
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
 // Declaring variable str=string 
 char str[50];
 
 /* Declaring variable cv=count vowel, i=to iterate loop 
 ( i works as index position) */
 int cv=0, i;
 
 // Inputing string
 printf("Enter any string : ");
 gets(str);
 
 // Checking if vowels and counting them
 for(i=0;str[i]!='\0';i++)
 {
  if(str[i]=='a' || str[i]=='A' || str[i]=='e' || str[i]=='E' || 
  str[i]=='i' || str[i]=='I' || str[i]=='o' || str[i]=='O' || 
  str[i]=='u' ||str[i]=='U')
  {
   cv++;
  }
 }
 
 // Printing total number of vowels
 printf("Entered String : %s",str);
 printf("\nTotal number of vowels : %d",cv);
 
 getch(); // Linux user - Remove this

 return 0;
}

23. Program to accept a string and display frequency of each vowel along with vowel
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
 // Declaring variable str=string 
 char str[50];
 
 // Declaring variable ca=count vowel a
 int ca=0, ce=0, ci=0, co=0, cu=0;
 
 // Declaring variable i=to iterate loop ( i works as index position)
 int i;
 
 // Inputing string
 printf("Enter any string : ");
 gets(str);
 
 // Determining frequency of each vowel
 for(i=0;str[i]!='\0';i++)
 {
  if(str[i]=='a' || str[i]=='A')
  ca++;
  
  if(str[i]=='e' || str[i]=='E')
  ce++;
  
  if(str[i]=='i' || str[i]=='I')
  ci++;
  
  if(str[i]=='o' || str[i]=='O')
  co++;
  
  if(str[i]=='u' || str[i]=='U')
  cu++;
  
 }
 
 // Printing string
 printf("Entered String : %s",str);
 
 // Printing frequency of string
 printf("\nFrequency of 'a' or 'A' : %d",ca);
 printf("\nFrequency of 'e' or 'E' : %d",ce);
 printf("\nFrequency of 'i' or 'I : %d",ci);
 printf("\nFrequency of 'o' or 'O' : %d",co);
 printf("\nFrequency of 'u' or 'U' : %d",cu);
 
 getch(); // Linux user - Remove this

 return 0;
}

24. Program to enter a word and check whether it is palindrome or not using string function
// palindrom - When word = reverse of word then it is called palindrome
// Example : madam - palindrome, Madam - not palindrome, MadaM - palindrome

#include<stdio.h>
#include<conio.h> // Linux user - Remove this

#include<string.h>

int main()
{
 // Declaring variable str=string 
 char word[50], word1[50];
 
 // Inputing string
 printf("Enter any word : ");
 scanf("%s",&word);

 // Copying word to word1
 strcpy(word1,word);
 
 // checking if palindrome or not
 if(strcmp(word,strrev(word1))==0)
 {
  printf("Entered word is a palindrome ");
 }
 
 else
 printf("Entered word is not palindrome ");
 
 getch(); // Linux user - Remove this

 return 0;
}


25. Program to enter a word and check whether it is palindrome or not without using string function
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
 // Declaring variable str=string 
 char word[50], revword[50];
 
 /* Declaring variable i=to iterate loop, l=length, 
 c=count the number of matched character */
 int i, j, l=0, c;
 
 // Inputing string
 printf("Enter any word : ");
 scanf("%s",&word);

 // finding length
 while(word[l]!='\0')
 l++;
 
 // Reversing string
 j=0;
 for(i=l-1;i>=0;i--)
 {
  revword[j]=word[i];
  j++;
 }
 revword[j]='\0';
 
 //checking if palindrome or not
 c=0;
 for(i=0;word[i]!='\0';i++)
 {
  if(revword[i]==word[i])
  c++;
 }
 
 if(c==l)
 {
  printf("Word is a palindrome ");
 }
 
 else
 printf("Word is not palindrome ");
 
 getch(); // Linux user - Remove this

 return 0;
}

C PROGRAMS : STRINGS

C PROGRAMS : STRINGS

C PROGRAMS : STRINGS
16. Program to convert all characters in a string to lower case using string function
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

#include<string.h>

int main()
{
 // Declaring variable str=string 
 char str[50];
 
 // Inputing string
 printf("Enter any string : ");
 gets(str);
 
 // Printing string
 printf("Entered String : %s\n",str);
 
 // Converting to lower case
 strlwr(str);
 
 // Printing converted string
 printf("Converted string : %s\n",str);
 
 getch(); // Linux user - Remove this

 return 0;
}

17. Program to convert all characters in a string to lower case without using string function
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

#include<string.h>

int main()
{
 // Declaring variable str=string, ch=character
 char str[50], ch;
 
 // Declaring variable i=to iterate loop
 int i;
 
 // Inputing string
 printf("Enter any string : ");
 gets(str);
 
 // Printing string
 printf("Entered String : %s\n",str);
 
  
 // Converting to lower case
 for(i=0;str[i]!='\0';i++)
    {
        ch=str[i];
        
        if(ch>=65 && ch<97)
        {
            ch+=32; // ch1=ch1+32
            str[i]=ch;
        }
    }
    
    // Printing converted string
 printf("Converted string : %s\n",str);
 
 getch(); // Linux user - Remove this

 return 0;
}
18. Program to convert all characters in a string to upper case using string function

#include<stdio.h>
#include<conio.h> // Linux user - Remove this

#include<string.h>

int main()
{
 // Declaring variable str=string 
 char str[50];
 
 // Inputing string
 printf("Enter any string : ");
 gets(str);
 
 // Printing string
 printf("Entered String : %s\n",str);
 
 // Converting to lower case
 strupr(str);
 
 // Printing converted string
 printf("Converted string : %s\n",str);
 
 getch(); // Linux user - Remove this

 return 0;
}

19. Program to convert all characters in a string to upper case without using string function
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

#include<string.h>

int main()
{
 // Declaring variable str=string, ch=character
 char str[50], ch;
 
 // Declaring variable i=to iterate loop
 int i;
 
 // Inputing string
 printf("Enter any string : ");
 gets(str);
 
 // Printing string
 printf("Entered String : %s\n",str);
 
  
 // Converting to lower case
 for(i=0;str[i]!='\0';i++)
    {
        ch=str[i];
        
        if(ch>=97 && ch<=122)
        {
            ch-=32; // ch1=ch1-32
            str[i]=ch;
        }
    }
    
    // Printing converted string
 printf("Converted string : %s\n",str);
 
 getch(); // Linux user - Remove this

 return 0;
}

20. Program to enter 5 string and print them with their length
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

#include<string.h>

int main()
{
 /* Declaring variable str[5][50]- This means that 5 string is to be input 
 with max size of each string = 50 */
 char str[5][50];
 
 // Declaring variable i=to iterate loop
 int i;
 
 // Inputing string
 for(i=0;i<5;i++)
 {
  printf("Enter %d string : ", i+1);
  gets(str[i]);
 }
 
 // Printing string with its length
 for(i=0;i<5;i++)
 {
  printf("\nString %d : %s ",i+1, str[i]);
  printf("\nString length : %d",strlen(str[i]));
 }
 
 getch(); // Linux user - Remove this

 return 0;
}

C PROGRAMS : STRINGS

C PROGRAMS : STRINGS

C PROGRAMS : STRINGS
10. Program for Compare two String using String Function
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

#include<string.h>

int main()
{
 // Declaring variable str=string1 , str2=string2
 char str1[50], str2[50];
 
 // Inputing string
 printf("Enter first string : ");
 gets(str1);
 
 printf("Enter second string : ");
 gets(str2);
 
 // Printing string
 printf("First String : %s\n",str1);
 printf("Second string : %s\n",str2);
 
 // Comparing string
 if(strcmp(str1,str2)==0)
 {
  printf("Same string or string are equal");
 }
 
 else if(strcmp(str1,str2)<0)
 {
  printf("First string is smaller ");
 }
 
 else if(strcmp(str1,str2)>0)
 {
  printf("Second string is smaller ");
 }
 
 getch(); // Linux user - Remove this

 return 0;
}

11. Program for Compare two String without using String Function
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
  // Declaring variable str1=string1, str2=string2
  char str1[50],str2[50];
  
  // Declaring variable ch1=character of string1, ch2=character of string2
  char ch1,ch2;
  
  // Declaring variable i=to iterate loop, flag = to indicate comparision
     int i,flag=0;
               
        // Inputing string       
        printf("\nEnter First String : " );
        gets(str1);
        
        printf("\nEnter Second String : " );
     gets(str2);
     
     // Comparing string
  for(i=0;str1[i]!='\0';i++)
        {
            ch1=str1[i];
            ch2=str2[i];
            if(ch1>=97 && ch1<=122)
            {
                ch1-=32; // ch1=ch1-32
            }
            if(ch2>=97 && ch2<=122)
            {
                ch2-=32; // ch2=ch2-32
            }
            if(ch1!=ch2)
            {
                flag=1;
                break;
            }
        }
               if(flag==0)
                   {
                        printf("\nString  '%s' and  '%s' are equal ",str1,str2);
                   }
               else
                   {
                        printf("\nString  '%s' and  '%s' are Not equal ",str1,str2);
                   }

 
 getch(); // Linux user - Remove this

 return 0;
}

12. Program for String Reverse with String Function
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

#include<string.h>

int main()
{
 // Declaring variable str=string 
 char str[50];
 
 // Inputing string
 printf("Enter any string : ");
 gets(str);

 // Printing string and reversed string
 printf("String : %s\n",str);
 printf("Reversed string : %s",strrev(str));
 
 getch(); // Linux user - Remove this

 return 0;
}

13. Program for String Reverse without String Function
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
 // Declaring variable str=string, revstr[50]=reverse string
 char str[50], revstr[50];
 
 // Declaring variable i=to iterate loop, l=length
 int i, j,l=0;
 
 // Inputing string
 printf("Enter any string : ");
 gets(str);

 // finding length
 while(str[l]!='\0')
 l++;
 
 // Reversing string
 j=0;
 for(i=l-1;i>=0;i--)
 {
  revstr[j]=str[i];
  j++;
 }
 revstr[j]='\0';
 
 // Printing string and reversed string
 printf("String : %s\n",str);
 printf("Reversed string : %s",revstr);
 
 getch(); // Linux user - Remove this

 return 0;
}

14. Program for String Concatenation with String Function
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

#include<string.h>

int main()
{
 // Declaring variable str=string1 , str2=string2
 char str1[50], str2[50];
 
 // Inputing string
 printf("Enter first string : ");
 gets(str1);
 
 printf("Enter second string : ");
 gets(str2);
 
 // Printing string
 printf("First String : %s\n",str1);
 printf("Second string : %s\n",str2);
 
 // Concatinating string
 strcat(str1,str2); // str2 is added to str1
 
 // Printing conactinated string
 printf("Concatinated string : %s\n",str1);
 
 getch(); // Linux user - Remove this

 return 0;
}

15. Program for String Concatenation without String Function
#include<stdio.h>
#include<conio.h> // Linux user - Remove this

int main()
{
 // Declaring variable str=string1 , str2=string2
 char str1[100], str2[50];
 
 // Declaring variable l=length of string1
 int l1=0;
 
 // Declaring variable i=to iterate loop
 int i;
 
 // Inputing string
 printf("Enter first string : ");
 gets(str1);
 
 printf("Enter second string : ");
 gets(str2);
 
 // Printing string
 printf("First String : %s\n",str1);
 printf("Second string : %s\n",str2);
 
 // Finding length of string1 and string2
 while(str1[l1]!='\0')
 l1++;
 
 // Concatinating string
 for(i=0;i<str2[i]!='\0';i++)
 {
  str1[l1++]=str2[i];
 }
 str1[l1]='\0';

 // Printing Concatinated stirng
  printf("Concatinated string : %s\n",str1);
 
 getch(); // Linux user - Remove this

 return 0;
}