Programs using pointers
Topics Covered:
- Programs using pointers
- To stores the record of passed students
- To find sum of n numbers
- To print the series
- To print the pattern of numbers
//program using pointers
#include<conio.h>
#include<stdio.h>
int main()
{
int x,*ptr;
x=10;
ptr=&x;
printf(“x=%d\n”,x);//value of x
printf(“ptr=%d\n”,ptr);//address of x
printf(“*ptr=%d”,*ptr);//value of *ptr
return 0;
}
//to stores the record of passed students
#include<conio.h>
#include<stdio.h>
int main()
{
int i,n,sn,m1,m2,m3;
char name[30];
FILE *f1;
f1=fopen(“student.dat”,”w”);
printf(“enter no. of students”);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
{
printf(“please enter symbol no.”);
scanf(“%d”,&n);
printf(“please enter name”);
scanf(“%s”,&name);
printf(“please enter marks of three subjects”);
scanf(“%d%d%d”,&m1,&m2,&m3);
if(m1>=40&&m2>=40&&m3>=40)
{
fprintf(f1,”%d\t%s\t%d\t%d\t%d\n”,sn,name,m1,m2,m3);
}
}
fclose(f1);
return 0;
}
//to find sum of n numbers
#include<conio.h>
#include<stdio.h>
int main()
{
int n,i,sum=0;
printf(“please enter number you want”);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
{
sum=sum+i;
}
printf(“sum=%d”,sum);
return 0;
}
//to print the series
#include<conio.h>
#include<stdio.h>
int main()
{
int i,n,a=1,b=1,sum;
printf(“enter number of terms”);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
{
printf(“%d\t”,a);
sum=a+b;
a=b;
b=sum;
}
return 0;
}
//to print the pattern of numbers
#include<conio.h>
#include<stdio.h>
int main()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
printf(“%d\t”,i);
}
printf(“\n”);
}
return 0;
}