CSE151 Labsheet4

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

CSE-151-COMPUTER PROGRAMMING SCHOOL OF ENGINEERING- B.

TECH

LABSHEET -4

Objectives
 Understand the concepts of modular programming and its
effectiveness

 Understand the concept of structures and unions

Example 1
Mr. Aditya is given the task to write the code to find the nCr. He knows the he has to find out the
factorial of the number. Help Mr. Aditya to code to get the required answer.
Requirements:
1. Capture the value of n and r
2. Compute the factorial of n
3. Compute the factorial of r
4. Compute the factorial of n-r
5. Compute nCr
6. Display the value of n C r

#include<stdio.h>
int main()
{
int i,n,r,ncr,fact1,fact2,fact3;
printf("enter the value of n and r\n");
scanf("%d%d",&n,&r);
if(n<r)
printf("Cant computer ncr\n");
else if(n==r)
printf("The value of ncr is 1\n");
else
{
fact1=1;
for(i=1;i<=n;i++)
fact1=fact1*i;
fact2=1;
for(i=1;i<=r;i++)
fact2=fact2*i;
fact3=1;
for(i=1;i<=n-r;i++)
fact3=fact3*i;
ncr=fact1/(fact2*fact3);
printf("The ncr value is %d\n",ncr);
}
}

1 LABSHEET-3 PRESIDENCY UNIVERSITY


CSE-151-COMPUTER PROGRAMMING SCHOOL OF ENGINEERING- B.TECH

In the above program we are writing 3 tines the same logic with different values so to avoid
multiple times writing we can write the code once and reuse the same code when ever it is required,
which is the main advantage of modular programming.

The modular programming has 4 different categories


1. Without parameter, without return type
2. Without parameter, with return type
3. With parameter, without return type
4. With parameter, with return type

Let us see all these 4 ways for finding the factorial of a number

Category 1:
Without parameter and without return type

#include<stdio.h>
void fact()
{
int n,res,i;
printf("enter the number\n");
scanf("%d",&n);
res=1;
for(i=1;i<=n;i++)
res=res*i;
printf("Factorial of %d is %d\n",n,res);
}
int main()
{
fact();
}

Category 2:
Without parameter and with return type

#include<stdio.h>
int fact()
{
int n,res,i;
printf("enter the number\n");
scanf("%d",&n);
res=1;
for(i=1;i<=n;i++)
res=res*i;
return res;

}
int main()
2 LABSHEET-3 PRESIDENCY UNIVERSITY
CSE-151-COMPUTER PROGRAMMING SCHOOL OF ENGINEERING- B.TECH

{
int res;
res=fact();
printf("Factorial =%d\n",res);
}

Category 3:
With parameter and without return type
#include<stdio.h>
void fact(int n)
{
int res,i;
res=1;
for(i=1;i<=n;i++)
res=res*i;
printf("Factorial of %d is %d\n",n,res);
}
int main()
{
int n;
printf("enter the number\n");
scanf("%d",&n);
fact(n);
}

Category 4:
With parameter and with return type

#include<stdio.h>
int fact(int n)
{
int res,i;
res=1;
for(i=1;i<=n;i++)
res=res*i;
return res;
}
int main()
{
int n,res;
printf("enter the number\n");
scanf("%d",&n);
res=fact(n);
printf("Factorial of %d is %d\n",n,res);
}

3 LABSHEET-3 PRESIDENCY UNIVERSITY


CSE-151-COMPUTER PROGRAMMING SCHOOL OF ENGINEERING- B.TECH

Program 6:
Update the program 1 to find ncr using functions

#include<stdio.h>
int fact(int n)
{
int res,i;
res=1;
for(i=1;i<=n;i++)
res=res*i;
return res;
}
int main()
{
int i,n,r,ncr,fact1,fact2,fact3;
printf("enter the value of n and r\n");
scanf("%d%d",&n,&r);
if(n<r)
printf("Cant computer ncr\n");
else if(n==r)
printf("The value of ncr is 1\n");
else
{
fact1=fact(n);
fact2=fact(r);
fact3=fact(n-r);
ncr=fact1/(fact2*fact3);
printf("The ncr value is %d\n",ncr);
}
}

Arrays as parameter to the function


Program 7:
Write a c program to read the values to the array and pass the array to the function with the
signature void prime(int a[],int n) which checks whether the elements of the array is prime or not.
If the array element is not prime set that value to 0. And in the main program display the array
elements which are prime

#include<stdio.h>
int count=0;
void prime(int a[],int n)
{
int i,j;
for(i=0;i<n;i++)
{
for(j=2;j<a[i];j++)
{
4 LABSHEET-3 PRESIDENCY UNIVERSITY
CSE-151-COMPUTER PROGRAMMING SCHOOL OF ENGINEERING- B.TECH

if(a[i]%j==0)
{
a[i]=0;
count++;
}
}
}
}
int main()
{
int i,n,a[50];
printf("Enter the size of array\n");
scanf("%d",&n);
printf("Enter the array elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
prime(a,n);
if(count==n)
printf("ther are no prime num\n");
else
{
printf("The prime num are\n");
for(i=0;i<n;i++)
{
if(a[i]!=0)
printf("%d\n",a[i]);
}
}
}

Program 8: Structures

Write a C program with a structure name called student with the members name- string, idnumber-
string, 3 subject marks- int, average- float. Declare the structure variable, read the student details,
calculate the average marks and display the same.

#include<stdio.h>
struct student
{
char name[20];
char idnum[20];
int m1,m2,m3;
float avg;
};
int main()
{
struct student s1;
printf("enter the name of student\n");
5 LABSHEET-3 PRESIDENCY UNIVERSITY
CSE-151-COMPUTER PROGRAMMING SCHOOL OF ENGINEERING- B.TECH

gets(s1.name);
printf("Enter the id number of student\n");
gets(s1.idnum);
printf("Enter the marks of student\n");
scanf("%d%d%d",&s1.m1,&s1.m2,&s1.m3);
s1.avg=(s1.m1+s1.m2+s1.m3)/3.0;
printf("name=%s\n",s1.name);
printf("Id number=%s\n",s1.idnum);
printf("average marks=%f\n",s1.avg);
}

Program 9:
C program to illustrate about structure and unions:
Write a c program creating structure and union with the members name-string, age-int, makrs-float,
declare the variable for structure and union and initialize the values to that. Display the size of
struct and union variable and display the details stored in those variable.

#include<stdio.h>
struct student
{
char name[24];
int age;
float marks;
};
union student1
{
char name[24];
int age;
float marks;
};
int main()
{
struct student s1={"mehul",20,95.5};
union student1 u1={"mohak"};
printf("Size of struct variable is %d\n",sizeof(s1));
printf("Size of union variable is %d\n",sizeof(u1));
printf("The stucture variable details are\n");
printf("Name\tage\tmarks\n");
printf("%s\t%d\t%f\n",s1.name,s1.age,s1.marks);
printf("The union variable details are\n");
printf("Name=%s\n",u1.name);
u1.age=20;
printf("age=%d\n",u1.age);
u1.marks=98.5;
printf("marks=%f\n",u1.marks);
}

6 LABSHEET-3 PRESIDENCY UNIVERSITY

You might also like