Cse 135 Lab
Cse 135 Lab
Cse 135 Lab
ID: 193-15-2992
Section: PC-B
Array
Problem no 1
Problem name: Take 10 integer inputs from user and store them in an array and print them on screen.
Source Code:
#include<stdio.h>
int main()
int i,n;
// scanf("%d",&n);
int arr[10];
for(i=0;i<10;i++)
scanf("%d",&arr[i]);
for(i=0;i<10;i++)
printf("%d ",arr[i]);
}
Problem no 2
Problem name: Take 10 integer inputs from user and store them in an array. Again ask user to give a
number. Now, tell user whether that number is present in array or not.
Source Code:
#include<stdio.h>
int main()
int i,ask;
// scanf("%d",&n);
int arr[10];
for(i=0;i<10;i++)
scanf("%d",&arr[i]);
scanf("%d",&ask);
for(i=0;i<10;i++)
if(ask==arr[i])
break;
}
Problem no 3
Problem name: Take 20 integer inputs from user and print the following:
number of 0.
Source Code:
#include<stdio.h>
int main()
int i,poss_c=0,neg_c=0,odd_c=0,even_c=0,zero_c=0;
int arr[20];
for(i=0;i<20;i++)
scanf("%d",&arr[i]);
for(i=0;i<20;i++)
if(arr[i]>0)
poss_c++;
if(arr[i]<0)
neg_c++;
if(arr[i]%2==0&&arr[i]!=0)
even_c++;
if(arr[i]%2!=0)
odd_c++;
if(arr[i]==0)
zero_c++;
Problem no 4
Problem name: Take 10 integer inputs from user and store them in an array. Now, copy all the elements
in another array but in reverse order.
Source Code:
#include<stdio.h>
int main()
int i,arr[10],rev_arr[10],j;
for(i=0;i<10;i++)
scanf("%d",&arr[i]);
rev_arr[i]=arr[j];
}
for(i=0;i<10;i++)
printf("%d ",rev_arr[i]);
Problem no 5
Problem name: Write a program to find the sum of all elements of an array.
Source Code:
#include<stdio.h>
int main()
int i,arr[5],sum=0;
printf("Enter 5 elements\n");
for(i=0;i<5;i++)
scanf("%d",&arr[i]);
sum+=arr[i];
}
Pointer problem
#include<stdio.h>
int main()
int m=10,n,o,*z;
z=&m;
printf("-------------------------------------------------------\n");
//printf("\n");
#include<stdio.h>
int main()
int m=29;
printf("Address of m : %p\n",&m);
printf("Value of m : %d\n",m);
int *ab=&m;
m=34;
*ab=7;
printf("Address of m : %p\n",&m);
printf("Value of m : %d\n",m);
Write a program in C to demonstrate the use of &(address of) and *(value at address) operator.
#include<stdio.h>
int main()
int m=300;
float fx=300.600006;
printf("--------------------------------------------------------\n");
printf("Using & operator :\n");
printf("-----------------------\n");
printf("address of m = %p\n",&m);
printf("address of m = %p\n",&fx);
printf("address of m = %p\n",&cht);
printf("-----------------------------\n");
printf("----------------------------------\n");
int *ptr;
float *ptr2;
char *ptr3;
ptr=&m;
ptr2=&fx;
ptr3=&cht;
printf("address of m = %p\n",ptr);
printf("address of fx = %p\n",ptr2);
printf("----------------------------------\n");
int main()
int n,n1;
scanf("%d",&n);
scanf("%d",&n1);
int *p,*p1;
p=&n; p1=&n1;
int sum=*p+*p1;
#include<stdio.h>
int sum=*p+*p;
return sum;
int main()
int n,n1;
scanf("%d",&n);
scanf("%d",&n1);
int sumt=sum(&n,&n1);
Structure
Write a program to store and print the roll no., name , age and marks of a student using structures.
#include<stdio.h>
struct student
int id;
char name[10];
int age;
float mark;
}s;
int main()
scanf("%d",&s.id);
scanf("%s",&s.name);
scanf("%d",&s.age);
scanf("%f",&s.mark);
printf("Name: %s\n",s.name);
printf("Age: %d\n",s.age);
printf("Mark: %.2f\n",s.mark);
}
Write a program to store the roll no. (starting from 1), name and age of 5 students and then print the
details of the student with roll no. 2.
#include<stdio.h>
struct student
int id;
char name[10];
int age;
} s[5];
int main()
int i;
printf("Student %d information\n",i+1);
s[i].id=i+1;
printf("Name: ");
scanf("%s",&s[i].name);
printf("Age: ");
scanf("%d",&s[i].age);
printf("Student 2 information\n");
printf("Name: %s\n",s[1].name);
printf("Age: %d\n",s[1].age);
}
Write a program to store and print the roll no., name, age, address and marks of 15 students using
structure.
#include<stdio.h>
struct student
int id;
char name[10];
int age;
char adr[30];
float mark;
} s[15];
int main()
int i;
printf("Student %d information\n",i+1);
printf("ID: ");
scanf("%d",&s[i].id);
printf("Name: ");
scanf("%s",&s[i].name);
printf("Age: ");
scanf("%d",&s[i].age);
printf("Address: ");
scanf("%s",&s[i].adr);
printf("Mark: ");
scanf("%f",&s[i].mark);
printf("\n\n\n");
printf("-------------------------------\n");
printf("\n\n\n");
printf("Student %d information\n",i+1);
printf("Name: %s\n",s[i].name);
printf("Age: %d\n",s[i].age);
printf("Address: %s\n",s[i].adr);
printf("Mark: %.2f\n",s[i].mark);
Write a program to add two distances in inch-feet using structure. The values of the distances is to be
taken from the user.
#include <stdio.h>
struct distance
int feet;
int inch;
};
struct distance add(struct distance a, struct distance b)
struct distance d;
d.feet = a.feet+b.feet;
d.feet = d.feet+((a.inch+b.inch)/12);
d.inch = (a.inch+b.inch)-(((a.inch+b.inch)/12)*12);
else
d.inch = a.inch+b.inch;
return d;
int main()
printf("Feet: ");
scanf("%d",&dis.feet);
printf("Inch: ");
scanf("%d",&dis.inch);
printf("Feet: ");
scanf("%d",&dis2.feet);
printf("Inch: ");
scanf("%d",&dis2.inch);
return 0;
Enter the marks of 5 students in Chemistry, Mathematics and Physics (each out of 100) using a
structure named Marks having elements roll no., name, chem_marks, maths_marks and phy_marks
and then display the percentage of each student.
#include <stdio.h>
struct marks
int roll_no;
char name[15];
float chem_marks;
float maths_marks;
float phy_marks;
}s[5];
int main()
int i;
for(i=0;i<5;i++)
{
scanf("%d",&s[i].roll_no);
printf("Name: ");
scanf("%s",&s[i].name);
scanf("%f",&s[i].chem_marks);
scanf("%f",&s[i].maths_marks);
scanf("%f",&s[i].phy_marks);
printf("\n");
printf("****************************\n\n");
for(i=0;i<5;i++)