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:

C Program to Add Two Numbers using Pointers

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


Input from user:

Enter Number1: 10

Enter Number2: 20


Expected output:

Sum= 30





  Step by step logic of the given program:


1. Accept input (numbers) from user.


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 add two numbers using dereference operator(*):

sum=*ptr1+*ptr2;


4. Last print sum on the output screen.




  Program to Add Two Numbers using Pointers:


#include<stdio.h>

int main()

{

int no1,no2,sum;

int *ptr1,*ptr2;

printf("Enter number1:\n");

scanf("%d",&no1);

printf("Enter number2:\n");

scanf("%d",&no2);

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

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

sum=*ptr1+*ptr2;// it adds values at address no1 & no2

printf("Sum= %d",sum);

return 0;

}


Above program shows the following output:


C Program to Add Two Numbers using Pointers



C Program to Show How to Handle Pointers

If you have ever found yourself puzzled by pointers, don't worry; you are not alone because pointers might sound tricky. But fear not! In this post, we are going to explore the basics of handling pointers in C through a simple and easy-to-follow program which shows how to handle pointers in C language.


  Step by Step logic of the given program:


1. First declare integer pointer variable a, declare integer variable z assign z=10.


2. Print value of z and address of z using &z.


3. After that assign a with address of z using:

a=&z;


4. Print address of pointer a using %p. It prints the address of z because we already stored address of z in variable a.


5. After that print content of pointer a using *a  it will print value of z=10.


6. After that assign z=30.


7. Print address of a using %p it will print address of z as it is. After that print content of pointer a using *a it will print changed value of z that means it will print 30.


8. After that assign value 5 to the pointer variable *a. Then print address of z and print value of z it will print value 5 because a contain the address of z.





  Program to Show Working of Pointers:


#include<stdio.h>

int main()

{

int* a;

int z;

z=10;

printf("How works pointer and how to handle pointers-C program:\n\n");

printf("Value of z = %d\n",z);

printf("Address of z = %p\n",&z);

a=&z;

//Now, a is assigned with the address of z.

printf("\nNow a is assigned with the address of z:\n");

printf("Address of pointer a = %p\n",a);

printf("content of pointer a = %d\n\n",*a);

z=30;//now value of z is 30

printf("Now, The value of z assigned to 30:\n");

printf("Address of pointer a = %p\n",a);

   printf(" Content of pointer a = %d\n\n",*a);

    *a=5;

   printf("Now, pointer variable a is assigned the value 5\n");

   printf("Address of z = %p\n",&z);

   /*here, a contain the address of z, so *a changed the value of z (now z=7)*/

   printf("Value of z = %d",z);

   return 0;

}



Above program shows the following output:


C Program to Show How to Handle Pointers


You can explore more examples of pointers 👉 Here..

Pointers in C Exercises and Solutions

In this tutorial you will learn pointer syntax, working, advantages and disadvantages with lot of pointer exercises and examples with their solution's.


Definition: In simple words pointer is a variable that stores address (memory location) of another variable's. This means that a pointer holds the memory address of another variable.



There are two main operator's used in pointer:


1. Reference operator & 
2. Dereference operator *



  Exercises and Solutions of Pointers in C:

keep visiting...keep learning... :)

Pointer in C Definition, Syntax & Working with Advantages & Disadvantages

Definition: In simple words pointer is a variable that stores address (memory location) of another variable's. This means that a pointer holds the memory address of another variable.



   There are two main operator's used in pointer:


1. Reference operator & 
2. Dereference operator *


1. Reference operator (&):


 The & is a unary operator that returns the memory address of its operand.



   Syntax of reference operator &:

&variable-name;

 
  • For Example:

If you have any variable var in your program, &var will give you it's memory address.

We have already used address(&) numerous times while using the scanf() function:

scanf("%d",&var);




  Examples program to print address of variable:


Here, %p is format specifier which is used to print the address of a pointer.
 

#include<stdio.h>
int main()
{
// declare variable 
int var;

printf("Address is: %p\n", &var);

return 0;
}


Above program shows the following output:

Pointer in C Definition, Syntax & Working with Advantages & Disadvantages



2. Dereference operator (*):


The * is a unary operator that returns the value of the variable at the address specified by its operand.

   Syntax of dereference operator (*):

*memory-address or pointer-variable;




  Example program of dereference operator (*):


#include<stdio.h>
int main()
{
int var=10;
/* It prints value pointed by (&var)*/
printf("Pointed value is: %d",*(&var));
return 0;
}

Above program shows the following output:

Example program of dereference operator



   Declaration syntax of pointer in C:



This is the general syntax of pointer. 

  • Here, * symbol specifies it is a pointer variable. You must prefix * before declare varible to it is pointer.
  • pointer_variable_name is the name of pointer variable.


Datatype of a pointer must be same as the data type of the variable to which the pointer variable is pointing.


  • For Example:

int *i; // pointer to intiger variable
char *c; //pointer to character variable
float *f; // pointer to float variable



  Example Program of Pointer:


#include <stdio.h>
int main()
{ 

    int num = 10;
    int *ptr = &num;
    // Here,  pointer points the value of num

    printf("Value of num = %d \n", num);   
 printf("Address of num = %x \n\n", &num);

    printf("Value of ptr = %x \n",ptr);
 printf("Address of ptr = %x \n", &ptr);
   

 return 0;

}


In this program %x is used to print hexadecimal values.


Above program shows the following output:

Example program of pointer




  Advantages of using pointer:


1. Pointer provide direct memory access.

2. It reduces the execution time of the program.

3. It reduces the storage space and complexity of the program.

4. Pointer provides an alternate way to access array elements.

5. Using pointers, arrays and structures can be handled in more efficient way.



  Disadvantages of using pointer:


1. In pointer if sufficient memory is not available for the storage of pointers during runtime, the program may crash.

2. Pointers are slower than normal variables.

3. Uninitialized pointers might cause segmentation fault.


Learn Pointers with more Examples....

String in C Exercises & Solutions

String are mainly defined as an array of characters. In simple words it is one dimensional array of characters which is terminated by null ('/0') character.


C Program to Toggle Case of Each Character of a String

Ex: Write a C program to toggle case of each character of a string. How to write a C program to toggle case of each character of a string. C program to toggle case of each character of a string.

Input from user:


Enter the string:

welcome To Codeforhunger.Com

Expected output:


Toggled string:

WELCOME tO cODEFORHUNGER.cOM





  Step by step logic of the given program:


1. Accept string from user declare varible say str.

2. After that calculate length of string using strlen() function and store it in variable say len.

3. Run for loop from 0 to length-1 of string, as follows:
for(i=0;i<len;i++)

4. Inside the body of loop check condition if character is lowercase alphabet then subtract 32 to make it capital and character is Capital alphabet then add 32 to make it small(lowercase), as follows:
if(str[i]>='a'&&str[i]<='z')
{
str[i]-=32;
}
else if(str[i]>='A'&&str[i]<='Z')
{
str[i]+=32;
   }


5. Last outside the block of for loop print toggled string.





  Program to toggle case of each character of a string:


#include<stdio.h>
#include<string.h>

void main()
{
char str[100];
int i,len;

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

len=strlen(str);//calculate length of string

for(i=0;i<len;i++)
{
if(str[i]>='a'&&str[i]<='z')
{
str[i]-=32;//subtract 32 to make it capital
}
else if(str[i]>='A'&&str[i]<='Z')
{
str[i]+=32;//add 32 to make it small
}
}

printf("Toggled string:\n%s",str);
}


Above program shows the following output:


C program to toggle case of each character of a string


C program to print given string in reverse order without using strrev()

Ex: Write a C program to print given string in reverse order without using strrev(). How to write a C program to print given string in reverse order without using strrev(). C program to print given string in reverse order without using strrev(). 

Input from user:

Enter the string:
codeforhunger

Expected output:

Given string in reverse order:
regnuhrofedoc




  Step by step logic of the given program:


1. Accept string from user declare varible say str.

2. After that calculate length using strlen() function and store it in variable len.

3. Run for loop from length of string to 0 which is looks like:
for(i=len;i>=0;i--)

4. Inside the for loop print string characters one by one.





  Program to print given string in reverse order without using strrev():

#include<stdio.h>
#include<string.h>
#define max 100

void main()
{
char str[max];
int i,len=0;

    /*Accept string*/
printf("Enter the string:\n");
gets(str);

len=strlen(str);

printf("Given string in reverse order: \n");
/*print given string in reverse order*/
for(i=len;i>=0;i--)
{
printf("%c",str[i]);
}
}



Above program shows the following output:


C program to print given string in reverse order without using strrev(), C Program to reverse a String




C Program to Copy one String to Another String without using strcpy() Function

Ex: Write a C program to copy one string to another string without using strcpy() function.
How to copy one string to another string without using strcpy() function. C program to copy one string to another string without using strcpy() function.


Input from user:

Enter the string:
I love codeforhunger

Expected output:

First string: I love codeforhunger

Second string: I love codeforhunger




  Step by step logic of the given program:


1. Accept string from user declare varible say str1.

2. Declare another variable to store copy of first string, varible say str2.

3. Run while loop from 0 to end of string.

4. Inside the while loop for each character in str1 copy to str2 using:
str2[i]=str1[i];

5. After loop make sure the copied string ends with NULL character using: 
str2[i]= '\0';





  Program to copy one string to another string without using strcpy() function:

#include<stdio.h>
#include<string.h>
#define max 100

void main()
{
char str1[max],str2[max];//declare string of max size 100
int i=0;
    

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

/*Iterate loop till end of string*/
 while(str1[i]!='\0')
{
/*copy the string */
str2[i]=str1[i];
i++;
}
/*make sure that string is Null terminated*/ 
     str2[i]='\0';

printf("\nFirst string: %s",str2);
printf("\nSecond string: %s",str2);
}



Above program shows the following output:


C program to print given string in reverse order without using strrev()



C Program to Count Total Number of Words in a String

Ex: Write a C program to count total number of words in a string. How to  count total number of words in a string. C Program to count total number of words in a string.

Input from user:

Enter the string:
This is codeforhunger.com

Expected output:

Number of words are: 3


    Required Knowledge:




  Step by step logic of the given program:

1. Accept string from user declare varible say str.

2. Run while loop till end of the string.

3. Inside the while loop check whether the current character is space or new line using:
if(str[i]== ' '||str[i]=='\n')
{
count++;
}

4. After that Print value of  count on the output screen.




  Program to count total number of words in a string:


#include<stdio.h>
#include<string.h>

void main()
{
char str[100];//declare string of max size 100
int i=0,count=1;

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

/*Iterate loop till end of string*/ while(str[i]!='\0')
{
/* check whether the current character is space or new line*/
if(str[i]== ' '||str[i]=='\n')
{
count++;
}
i++;
}
printf("Number of words are:%d",count);
}


Above program shows the following output:


C Program to count total number of words in a string