C program to print inverted half right side of pyramid star pattern

 Ex: Write a C program to print inverted half right side of pyramid star pattern. How to write a C program to print inverted half right side of pyramid star pattern. C program to print inverted half right side of 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 i to no which shows columns in pattern:

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


4. Inside the inner loop print star:

printf("*");


5. For next row print \n.





Program:


#include<stdio.h>

void main()

{

int i,j,no;

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

scanf("%d",&no);

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

{

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

{

printf("*");

}

printf("\n");

}

}



Above program shows the following output:


C program to print inverted half right side of pyramid star pattern



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

 Ex: Write a C program to print inverted half left side of the pyramid star pattern. How to write a C program to print inverted half left side of the pyramid star pattern. C program to print inverted 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 for loop from 1 to no which shows number of rows:

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


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

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

{

printf(" ");

}


4. Run another one inner for loop from i to no to print star:

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

{

printf("*");

}


5. For next row print \n.




Program:


#include<stdio.h>

void main()

{

int i,j,k,no;

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

scanf("%d",&no);

/*Run loops*/

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

{

                /* Print spaces from second row, in incremented manner*/

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

{

printf(" ");

}

                /* Print stars in decremented manner*/

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

{

printf("*");

}

                /* For next row*/

printf("\n");

}

}



Above program shows the following output:


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

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


C Program to Perform All Arithmetic Operations using Pointers

Ex: Write a C program to perform all arithmetic operations using pointers. How to write a C program to perform all arithmetic operations using pointers. C program to perform all arithmetic operations using pointers.


Input from user:

Enter Number1: 20

Enter Number2: 10


Expected output:

Sum=30

Subtraction=10

Multiplication=200

Division=2.0000


Before learning how to write a C program to perform all arithmetic operations we need to know what is arithmetic operators means.


  What is Arithmetic Operators:

Arithmetic Operators are the operators which are used to perform various mathematical operations such as addition, subtraction, division, multiplication etc.

For Examples: 

1. Plus (+): is used for addition like 5 + 5 = 10, 35 + 35 = 70, 10 + 13 + 25 = 48  etc.

2. Minus Operator (-): is used for subtract one number from another number like 23 - 8 = 15, 65 - 25 = 40.

3. Multiplication Operator (*) : is represented as an asterisk (*) symbol and it returns product of  given numbers like 5 * 5 = 25, 7 * 12 = 84 etc.

4. Division Operator (/) : is denoted by  forward slash (/)  symbol and it divides first number by the second number like 35 / 5 = 7.


5. Modulus Operator (%) : is denoted by percentage (%) symbol and it returns remainder. for ex: 25 / 5 = 0, 45 / 6 = 3 etc.


In below example we have performed addition, subtraction, multiplication and division operations using pointers in C. 


  Quick Links:


  Step by step logic of the given program:


1. Accept two numbers from user store it in variable say no1 and no2.

2. Store address of no1 in pointer variable ptr1 and store address of no2 in variable ptr2 using reference operator (&):

ptr1=&no1;

ptr2=&no2;

3. After that calculate addition, subtraction, multiplication and division using dereference operator(*):

sum=(*ptr1) + (*ptr2);

sub=(*ptr1) - (*ptr2);

mult=(*ptr1) * (*ptr2);

div=(*ptr1) / (*ptr2);

4. Last print sum, sub, mult and div on the output screen.



  C Program to Perform All Arithmetic Operations using Pointers:

#include<stdio.h>

int main()

{

int no1,no2;

int *ptr1,*ptr2;

        int sum,sub,mult;

        float div;   

    printf("Enter number1:\n");

    scanf("%d",&no1);

    printf("Enter number2:\n");

    scanf("%d",&no2);

    

    ptr1=&no1;//ptr1 stores address of no1

    ptr2=&no2;//ptr2 stores address of no2

    

    sum=(*ptr1) + (*ptr2);

    sub=(*ptr1) - (*ptr2);

    mult=(*ptr1) * (*ptr2);

    div=(*ptr1) / (*ptr2);

    

    printf("sum= %d\n",sum);

    printf("subtraction= %d\n",sub);

    printf("Multiplication= %d\n",mult);

    printf("Division= %f\n",div);


    return 0;

}



Above program shows the following output:


C Program to Perform All Arithmetic Operations using Pointers



Feel free to Share your thoughts below in the comment sections.


    You May Also Like: