EXERCISE-10
1. Write a program in C to demonstrate the use of & (address of) and *(value at address) operator.
SOURCE CODE:
#include<stdio.h>
int main() {
int num1, *p1;
float num2, *p2;
char ch1, *p3;
printf("Enter int, float and char values : ");
scanf("%d %f %c",&num1, &num2, &ch1 );
p1=&num1;
p2=&num2;
p3=&ch1;
printf("Given int value : %d\n", *p1);
printf("Given float value : %f\n", *p2);
printf("Given char value : %c\n", *p3);
return 0;
}
OUTPUT:
Enter int, float and char values : 97 3.24 I
Given int value : 97
Given float value : 3.240000
Given char value : I
2. Write a program in C to add two numbers using pointers.
SOURCE CODE:
#include<stdio.h>
#include<stdlib.h>
int main(){
int *num1, *num2;
num1 = (int*)malloc(sizeof(int));
num2 = (int*)malloc(sizeof(int));
printf("Enter two numbers : ");
scanf("%d%d",num1,num2);
printf("Sum of %d and %d is %d\n",*num1,*num2,*num1+*num2);
return 0;
}
OUTPUT:
Enter two numbers : 5 9
Sum of 5 and 9 is 14
SOURCE CODE:
#include<stdio.h>
int main() {
int num1, *p1;
float num2, *p2;
char ch1, *p3;
printf("Enter int, float and char values : ");
scanf("%d %f %c",&num1, &num2, &ch1 );
p1=&num1;
p2=&num2;
p3=&ch1;
printf("Given int value : %d\n", *p1);
printf("Given float value : %f\n", *p2);
printf("Given char value : %c\n", *p3);
return 0;
}
OUTPUT:
Enter int, float and char values : 97 3.24 I
Given int value : 97
Given float value : 3.240000
Given char value : I
2. Write a program in C to add two numbers using pointers.
SOURCE CODE:
#include<stdio.h>
#include<stdlib.h>
int main(){
int *num1, *num2;
num1 = (int*)malloc(sizeof(int));
num2 = (int*)malloc(sizeof(int));
printf("Enter two numbers : ");
scanf("%d%d",num1,num2);
printf("Sum of %d and %d is %d\n",*num1,*num2,*num1+*num2);
return 0;
}
OUTPUT:
Enter two numbers : 5 9
Sum of 5 and 9 is 14
No comments