C program to print half left side of the pyramid star pattern

 Ex: Write a C program to print half left side of the pyramid star pattern. How to write a C program to print half left side of the pyramid star pattern. C program to print half left side of the pyramid star pattern.


Input from user:

Enter the number:

5


Expected output:

        *

      **

    ***

  ****

*****



  Step by step logic of the given program:


1. Accept input number from user declare variable say no.


2. Run outer loop from 1 to no which shows the number of rows in pattern:

for(i=1;i<=no;i++)


3. Run first inner loop from i to no-1 to print spaces:

for(j=i;j<no;j++)


4. Run second inner loop from 1 to i to print star:

for(k=1;k<=i;k++)


5. For next row print \n.




Program:


#include<stdio.h>

void main()

{

int no,i,j,k;

printf("Enter the number:\n");

scanf("%d",&no);

/*Run loops*/

for(i=1;i<=no;i++)

{

for(j=i;j<no;j++)

{

    printf(" ");

}

for(k=1;k<=i;k++)

{

    printf("*");

}

/*For new row*/

        printf("\n");

}


}


Above program shows the following output:


C program to print half left side of the pyramid star pattern



C program to print half right side of pyramid star pattern

 Ex: Write a C program to print half right side of  the pyramid star pattern. How to write a C program to print half right side of the pyramid star pattern. C program to print half right side of the pyramid star pattern.



Input from user:

Enter the number:

5


Expected output:

*

* *

* * *

* * * *

* * * * *



  Step by step logic of the given program:


1. Accept input number from user declare variable say no.


2. Run outer for loop from 1 to no which shows rows in pattern:

for(i=1;i<=no;i++)


3. Run inner for loop from 1 to  i which shows columns in pattern:

for(j=1;j<=i;j++)


4. Inside the inner loop print star:

printf("*");


5. For next row print \n.





Program:


#include<stdio.h>

void main()

{

int no,i,j;

printf("Enter the number:\n");

scanf("%d",&no);

/*Run loops*/

for(i=1;i<=no;i++)

{

for(j=1;j<=i;j++)

{

/* print star*/

printf("*");

}

/*For new row*/

printf("\n");

}

}



Above program shows the following output:


C program to print half right side of pyramid star pattern





C program to print hollow square star pattern

 Ex: Write a C program to print hollow square star pattern. How to write a C program to print hollow square star pattern. C program to print hollow square star pattern.


Input from user:

Enter the number:

7


Expected output:

C program to print hollow square star pattern




  Step by step logic of the given program:

1. Accept input number from user declare variable say no.


2. Run outer loop from 1 to no which will shows rows of pattern:

for(i=1;i<=no;i++)


3. Run inner loop from 1 to no which will shows columns of pattern:

for(j=1;j<=no;j++)


4. Using if statement give conditions to print stars:

if(i==1||i==no||j==1||j==no)


5. Otherwise print spaces (to print it like hollow).


6. For next row use new line (\n).




Program:


#include<stdio.h>

void main()

{

int no,i,j;

printf("Enter the number:\n");

scanf("%d",&no);

/*Run loops from 1 to no*/

for(i=1;i<=no;i++)

{

for(j=1;j<=no;j++)

{

/*Give conditions to print stars*/

                if(i==1||i==no||j==1||j==no)

{

printf("*");

}

else

{

/*Otherwise print spaces*/

                             printf(" ");

}

}

/*For new row*/

        printf("\n");

}

}



Above program shows the following output:


C program to print hollow square star pattern


C program to print Rhombus star pattern

Ex: Write a C program to print Rhombus star pattern. how to print Rhombus star pattern using for loop. C Program to print Rhombus star Pattern.
In below code we  print Rhombus star pattern.



Input from user:

Enter the number: 5

Expected output:

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




  Step by step logic to print Rhombus star pattern.

1. Accept input (number) from user declare variable say no.


2. Run a loop(outer loop), to iterate through rows and increment i by 1:

for(i=1; i<=no; i++)


3. After that to print spaces run another loop(inner loop):

for(j=1;j<=no-i; j++)
{
printf(" ");
}


4. To print stars run another loop(inner loop) from 1 to no. Print star inside this loop:

for(k=1; k<=no; k++)

 {

printf("*");

 }


5. After printing all columns of a row, print new line (\n), for move it to next line.





Program:

/***C program to print Rhombus star pattern***/

#include<stdio.h>
void main()
{
int i,j,k,no;

printf("Enter the number:\n");/*take input number from user which denotes number of rows*/
scanf("%d",&no);

for(i=1;i<=no;i++)
{
for(j=1;j<=no-i; j++)
{
printf(" ");
}
for(k=1;k<=no;k++)
    {
      printf("*");
    }
    printf("\n");//for new line
}
}

Above program shows the following output:

C program to print Rhombus star pattern

C program to print mirrored rhombus star pattern

Ex: Write a C program to print mirrored rhombus star pattern. How to write a C program to print mirrored rhombus star pattern. C program to print mirrored rhombus star pattern.


Input from user:

Enter the number:

5


Expected output:

*****

 *****

  *****

   *****

    *****



  Step by step logic of the given program:


1. Accept input (number) from user declare variable say no.


2. Run a loop(outer loop), to iterate through rows and increment i by 1:

for(i=1; i<=no; i++)


3. After that to print spaces run another loop(inner loop):

for(j=1; j<i; j++)

{

printf(" ");

}


4. To print stars run another loop(inner loop) from 1 to no. Print star inside this loop:

for(k=1; k<=no; k++)

 {

printf("*");

 }


5. After printing all columns of a row, print new line (\n), for move it to next line.





Program:


#include<stdio.h>

void main()

{


 int i,j,k,no;

   /*Take input number from user which denotes number of rows and columns in pattern*/

   printf("Enter the number:\n");

   scanf("%d",&no);

   for(i=1; i<=no; i++)

   {

     /*Print spaces*/

     for(j=1; j<i; j++)

{

printf(" ");

}

     for(k=1; k<=no; k++)

{

printf("*");

}

     printf("\n");//for new line

 }

}


Above program shows the following output:


C program to print mirrored rhombus star pattern


C program to concatenate strings using pointers

 Ex: Write a C program to concatenate strings using pointers. How to write a C program to concatenate strings using pointers. C program to concatenate strings using pointers.



Input from user:

Enter first string:

codefor

Enter second string:

hunger


Expected output:

Concatenated string:

codeforhunger




  Step by step logic of the given program:


1. Accept two strings from user declare variables say str1 and str2.


2. Run while loop till end of first string:

while(*s1)

     {

  s1++;

     }


3. Run while loop till end of second string and inside the while loop copy second string to first string:

while(*s2)

        {

*s1=*s2;

s1++;

s2++;

}


4. After that end first string with NULL(\0).


5. Last, print concatenated string str1.




  C program to concatenate two strings using pointers:


#include<stdio.h>

#define max 100//declare max size 100

int main()

{

char str1[max],str2[max];

        /*declare pointers for str1 and str2*/

char *s1=str1,*s2=str2;

printf("Enter first string:\n");

gets(str1);

printf("Enter second string:\n");

gets(str2);

while(*s1)/*till end of the first string*/

{

s1++;/*point to the next character*/

}

while(*s2)/*till end of the second string*/

{

*s1=*s2;

s1++;

s2++;

}

/*Make sure that str1 end with NULL*/

    *s1='\0';

    printf("Concatenated string is:\n%s",str1);

return 0;

}



Above program shows the following output:


C program to concatenate strings using pointers



C Program to Find Length of String Using Pointers

Ex: Write a C program to find length of string using pointers. How to write a C program to find length of string using pointers. C program to find length of string using pointers.


Input from user:

Enter the string: 

Hi this is codeforhunger


Expected output:

Length of string= 24



  Step by step logic of the given program:


1. Accept input(string) from user store it in some variable say str.


2. After that use while loop(here you can use any other loop also) to iterate string through last element:

while(*ptr!='\0')


3. Inside the loop increment counter by 1(to calculate length of string). i.e, count++;


4. After that print value of count(length of string) on the output screen.




  Program to find length of string using pointers:


#include<stdio.h>

int main()

{

char str[100];//declare string size 100

char *ptr=str;/*declare pointer which points to string(str)*/

int count=0;

printf("Enter the string:\n");

gets(str);

/*Iterate string through last element*/

while(*ptr!='\0')

{

count++;

ptr++;

}

printf("Length of string= %d",count);

return 0;

}


Above program shows the following output:


C program to calculate length of the string using pointers, what are pointers in C programming language, how to learn c programming pointes

C Program to Print Array Elements Using Pointers

Ex: Write a C program to print array elements using pointers. How to write a C program to print array elements using pointers. C program to print array elements using pointers.


Input from user:

Enter how many elements you are going to enter: 5

Enter elements:

10

20

30

40

50


Expected output:

Array elements are:

10

20

30

40

50




  Step by step logic of the given program:


1. Accept limit from user declare variable say no.


2. Accept elements using pointer variable ptr and move pointer to next location by using ptr++.


3. After that set pointer back to first array element by using:

ptr=arr;


4. Last, print pointed values by using  dereference operator (*):

 *ptr.





  C Program to Print Array Elements Using Pointers:


#include<stdio.h>

int main()

{

int arr[100],i,no;

int *ptr=arr;//declare pointer to arr[0]

printf("Enter how many elements you are going to enter:\n");

scanf("%d",&no);

printf("Enter elements:\n");

for(i=0;i<no;i++)

{

scanf("%d",ptr);

/*move pointer to next array element*/

ptr++;

}

       /* Set pointer back to first array element*/

ptr=arr;

printf("Array elements are:\n");

{

for(i=0;i<no;i++)

{

//print pointed values

printf("arr[%d]=%d\n",i,*ptr);

/*move pointer to next array element*/

ptr++;

}

}

return 0;

}


Above program shows the following output:


C Program to Print Array Elements Using Pointers

C Program to Swap Two Numbers Using Pointers

 Ex: Write a C program to swap two numbers using pointers. How to write a C program to swap two numbers using pointers. C program to swap two numbers using pointers.


Input from user:

Enter first number: 10

Enter second number: 20


Expected output:

Numbers after swapping:

Number1=20

Number2=10



In this program we will use two pointer operators:

1. Reference operator (&)

2. Dereference operator (*)




  Step by step logic of the given program:


1. Accept two numbers from user declare variable say no1 & no2.


2. Use third variable(temp) to swap two numbers, shown as follow:

temp=*ptr1;

*ptr1=*ptr2;

*ptr2=temp;


3. Last print swapped numbers.




  C Program to Swap Two Numbers Using Pointers :


#include<stdio.h>

int main()

{


int no1,no2,temp;      

int *ptr1=&no1,*ptr2=&no2;

printf("Enter first number:\n");

scanf("%d",&no1);

printf("Enter second number:\n");

scanf("%d",&no2);  


         temp=*ptr1;

*ptr1=*ptr2;

*ptr2=temp;


printf("Numbers after swapping:\nNumber1=%d\nNumber2=%d",*ptr1,*ptr2);


        return 0;


}


Above program shows the following output:


C Program to Swap Two Numbers Using Pointers


C Program to Find Maximum Between Two Numbers Using Pointers

Ex: Write a C program to find maximum between two numbers. How to write a C program to find maximum between two numbers. C program to find maximum between two numbers.


Input from user:

Enter first number: 25

Enter second number: 35


Expected output:

Maximum number is 35