1.
Program: Write a program to find biggest among three numbers
using pointer.
Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int a,b,c;
int*ptra=&a,*ptrb=&b,*ptrc=&c;
printf("enter three values");
scanf("%d%d%d",ptra,ptrb,ptrc);
printf("a=%d\n b=%d\n c=%d\n",*ptra,*ptrb,*ptrc);
if((*ptra>*ptrb && *ptra>*ptrc))
printf("biggest number=%d",*ptra);
else if((*ptrb>*ptra && *ptrb>*ptrc))
printf("biggest number =%d",*ptrb);
else
printf("biggest number=%d",*ptrc);
getch();
return 0;
}
2. C program to swap two number using call by reference
*/
#include <stdio.h>
void swap(int * num1, int * num2);
int main()
{
int num1, num2;
printf("Enter two numbers: ");
scanf("%d%d", &num1, &num2);
printf("Before swapping in main n");
printf("Value of num1 = %d \n", num1);
printf("Value of num2 = %d \n\n", num2);
swap(&num1, &num2);
printf("After swapping in main n");
printf("Value of num1 = %d \n", num1);
printf("Value of num2 = %d \n\n", num2);
return 0;
}
void swap(int * num1, int * num2)
{
int temp;
temp = *num1;
*num1= *num2;
*num2= temp;
printf("After swapping in swap function n");
printf("Value of num1 = %d \n", *num1);
printf("Value of num2 = %d \n\n", *num2);
}
3./*C program to print a string using pointer.*/
#include <stdio.h>
int main()
{
char str[100];
char *ptr;
printf("Enter a string: ");
gets(str);
//assign address of str to ptr
ptr=str;
printf("Entered string is: ");
while(*ptr!='\0')
printf("%c",*ptr++);
return 0;
}
[Link] a program to Swapping the values of the two variables using call
by reference method.
#include <stdio.h>
void swap(int *x, int *y){
int temp = *x;
*x = *y;
*y = temp;
}
int main(){
int x = 10;
int y = 11;
printf("Values before swap: x = %d, y = %d\n", x,y);
swap(&x,&y);
printf("Values after swap: x = %d, y = %d", x,y);
}
5. Write a program in C to take details of 3 students as input and display
the result by passing the structure to a function.
#include <stdio.h>
struct student {
char firstname[64];
char lastname[64];
char id[64];
int score;
};
void displayDetail(struct student std);
int main(void) {
struct student stdArr[3];
int i;
for (i = 0; i < 3; i++) {
printf("Enter detail of student #%d\n", (i+1));
printf("Enter First Name: ");
scanf("%s", stdArr[i].firstname);
printf("Enter Last Name: ");
scanf("%s", stdArr[i].lastname);
printf("Enter ID: ");
scanf("%s", stdArr[i].id);
printf("Enter Score: ");
scanf("%d", &stdArr[i].score);
}
for (i = 0; i < 3; i++) {
printf("\nStudent %d Detail:\n", (i+1));
displayDetail(stdArr[i]);
}
return 0;
}
void displayDetail(struct student std)
{
printf("Firstname: %s\n", [Link]);
printf("Lastname: %s\n", [Link]);
printf("ID: %s\n", [Link]);
printf("Score: %d\n", [Link]);
}
[Link] a program to store Information and Display it Using Structure
#include <stdio.h>
struct student {
char name[50];
int roll;
float marks;
} s;
int main() {
printf("Enter information:\n");
printf("Enter name: ");
scanf(“%s”,&[Link]);
printf("Enter roll number: ");
scanf("%d", &[Link]);
printf("Enter marks: ");
scanf("%f", &[Link]);
printf("Displaying Information:\n");
printf("Name: ");
printf("%s", [Link]);
printf("Roll number: %d\n", [Link]);
printf("Marks: %f\n", [Link]);
return 0;
}