You are on page 1of 8

ASSIGNMENT

1. Question: Write a C program to store n numbers in an array and


print the elements using pointers. Also compute the sum of all the
elements of that array using pointers.

C code:
#include<stdio.h>

main()

int num[1000],n,i,*p,sum=0;

printf("How many numbers do you want to enter?\t");

scanf("%d",&n);

printf("Enter the numbers:\t");

p=&num[0];

for (i=0;i<n;i++)

scanf("%d",p);

p++;

p=&num[0];

printf("The numbers are:\n");

for (i=0;i<n;i++)
{

printf("%d\t",*p);

sum=sum+*p;

p++;

printf("\nSum of the numbers is %d",sum);

Output:

2. Question: Create a structure to specify data on students given


below—Roll no. Name, Department, Course, Year of joining. Assume
that there are not more than 100 students in the college. Write a
function to print the names of the students who joined in a particular
year. Write a function to print the data of a student where the roll
number is given.
C Code:

#include<stdio.h>

struct stud

int roll,yr;

char name[50],dept[50],course[10];
};

struct stud s[100];

void same_yr(struct stud str[],int n)

int i,year;

printf("\nEnter the year:");

scanf("%d",&year);

printf("\nStudents who joined the college in %d are:\n",year);

for(i=0;i<n;i++)

if (str[i].yr==year)

printf("%s\n",str[i].name);

void data(struct stud str[],int n)

int i,roll;

printf("\nEnter the roll no. of the student:");

scanf("%d",&roll);

for(i=0;i<n;i++)

if (str[i].roll==roll)
{

printf("Student Name\t:\t%s\n",str[i].name);

printf("Department\t:\t%s\n",str[i].dept);

printf("Course\t\t:\t%s\n",str[i].course);

printf("Year of Joining\t:\t%d",str[i].yr);

main()

int i,n;

printf("Enter number of students:");

scanf("%d",&n);

printf("\nEnter Roll no, Name, Department, Course, Year of joining:\n\n");

for(i=0;i<n;i++)

scanf("%d%s%s%s%d",&s[i].roll,&s[i].name,&s[i].dept,&s[i].course,&s[i].yr);

same_yr(s,n);

data(s,n);

}
Output:

3. Question: Write a program which accepts names, roll no. and marks
of 10 students in 6 subjects, stores it in an array of structure. Write a
separate function which prints in ascending order the rank list based on
the average of 6 subjects.
C Code:
#include<stdio.h>

struct stud

char name[20];

int roll;

float marks[6],avg;

};

struct stud s[10];

void rank(struct stud str[],int n)

int i,k;

for(i=0;i<n;i++)

for(k=i+1;k<n;k++)

if (str[i].avg<str[k].avg)

stud a;

a=str[i];

str[i]=str[k];
str[k]=a;

printf("\nRoll No.\tName\t\tAverage Marks\tRank\n");

for(i=0;i<n;i++)

printf("%d\t\t%s \t%.2f\t\t%d\n",str[i].roll,str[i].name,str[i].avg,i+1);

main()

int i,j,k,n;

float avg[6];

printf("Enter number of students:");

scanf("%d",&n);

printf("Enter Name, Roll No. and Marks in 6 subjects.\n");

for(i=0;i<n;i++)

float sum=0;

printf("Student %d:",i+1);
scanf("%s%d",&s[i].name,&s[i].roll);

for (j=0;j<6;j++)

scanf("%f",&s[i].marks[j]);

sum+=s[i].marks[j];

s[i].avg=sum/6;

rank(s,n);

Output:

You might also like