Aug 12, 2020

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





Step by step logic of the given program:


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


2. After that store address of no1 in pointer ptr1 and address of no2 in pointer ptr1 using reference operator (&):

ptr1=&no1;

ptr2=&no2;


3. Using if statement find maximum number between two numbers by using dereference operator(*).




Program:


#include<stdio.h>

int main()

{

int no1,no2;

int *ptr1,*ptr2;

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

scanf("%d",&no1);

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

scanf("%d",&no2);

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

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

if(*ptr1>*ptr2)

{

printf("Maximum number is %d",*ptr1);

}

else

{

printf("Maximum number is %d",*ptr2);

}

        return 0;

}



Above program shows the following output:




Share This:    Facebook Twitter

0 comments:

Post a Comment

If you have any doubts, please discuss here...