Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 16

Name: Abdul Wahid

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])

printf("Number %d is present in array",ask);

break;

}
Problem no 3

Problem name: Take 20 integer inputs from user and print the following:

number of positive numbers

number of negative numbers

number of odd numbers

number of even numbers

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++;

printf("Positive: %d\nNegative: %d\nOdd: %d\nEven: %d\nZero:


%d",poss_c,neg_c,odd_c,even_c,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]);

for(i=9,j=0; i>=0; i--,j++)

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];

printf("Sum of all elements of array %d\n",sum);

}
Pointer problem

Write a program in C to show the basic declaration of pointer.

#include<stdio.h>

int main()

int m=10,n,o,*z;

z=&m;

printf("Pointer : Show the basic declaration of pointer :\n");

printf("-------------------------------------------------------\n");

printf("Here is m=10, n and o are two integer variable and *z is an integer\n");

printf("z stores the address of m = %p\n",z);

printf("*z stores the value of m = %d\n",*z);

printf("&m is the address of m = %p\n",&m);

printf("&n stores the address of n = %p\n",&n);

printf("&o stores the address of o = %p\n",&o);

printf("&z stores the address of z = %p\n",&z);

//printf("\n");

Write a program in C to demonstrate how to handle the pointers in the program.

#include<stdio.h>

int main()

int m=29;
printf("Address of m : %p\n",&m);

printf("Value of m : %d\n",m);

printf("Now ab is assigned with the address of m.\n");

int *ab=&m;

printf("Address of pointer ab : %p\n",&ab);

printf("Content of pointer ab : %d\n",*ab);

printf("The value of m assigned to 34 now.\n");

m=34;

printf("Address of pointer ab : %p\n",&ab);

printf("Content of pointer ab : %d\n",ab);

printf("The pointer variable ab is assigned with the value 7 now.\n");

*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;

char cht = 'z';

printf("Pointer : Demonstrate the use of & and * operator :\n");

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("Using & and * operator :\n");

printf("-----------------------------\n");

printf("value at address of m = %d\n",*(&m));

printf("value at address of fx = %f\n",*(&fx));

printf("value at address of cht = %c\n",*(&cht));

printf("\n Using only pointer variable :\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("address of cht = %p\n",ptr3);

printf("\n Using only pointer operator :\n");

printf("----------------------------------\n");

printf("value at address of m = %d\n",*ptr);

printf("value at address of fx= %f\n",*ptr2);

printf("value at address of cht= %c\n\n",*ptr3);

Write a program in C to add two numbers using pointers.


#include<stdio.h>

int main()

int n,n1;

printf("Input the first number :\n");

scanf("%d",&n);

printf("Input the second number :\n");

scanf("%d",&n1);

int *p,*p1;

p=&n; p1=&n1;

int sum=*p+*p1;

printf("The sum of the entered numbers is : %d",sum);

5. Write a program in C to add numbers using call by reference.

#include<stdio.h>

int sum(int *p, int *p1)

int sum=*p+*p;

return sum;

int main()

int n,n1;

printf("Input the first number : \n");

scanf("%d",&n);

printf("Input the second number : \n");

scanf("%d",&n1);
int sumt=sum(&n,&n1);

printf("The sum of %d and %d is %d ",n,n1,sumt);

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("Roll no.: %d\n",s.id);

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;

for(i=0; i<5; 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("Roll no.: %d\n",s[1].id);

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;

for(i=0; i<15; 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");

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

printf("\n\n\n");

printf("Student %d information\n",i+1);

printf("Roll no.: %d\n",s[i].id);

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;

if(a.inch+b.inch >= 12)

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()

struct distance dis,dis2;

printf("Enter values for distance 1\n");

printf("Feet: ");

scanf("%d",&dis.feet);

printf("Inch: ");

scanf("%d",&dis.inch);

printf("\n\nEnter values for distance 2\n");

printf("Feet: ");

scanf("%d",&dis2.feet);
printf("Inch: ");

scanf("%d",&dis2.inch);

struct distance d = add(dis,dis2);

printf("\n\n%d feet and %d inch\n",d.feet,d.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++)
{

printf("Enter information for student %d\n",i+1);

printf("Roll no.: ");

scanf("%d",&s[i].roll_no);

printf("Name: ");

scanf("%s",&s[i].name);

printf("Chemistry markes: ");

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

printf("Math marks: ");

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

printf("Physics marks: ");

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

printf("\n");

printf("****************************\n\n");

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

printf("Roll no.: %d\tName: %s\tPercentage %.2f%%\n",s[i].roll_no,s[i].name,


(( s[i].chem_marks+s[i].maths_marks+s[i].phy_marks)/300.00)*100);

You might also like