0% found this document useful (0 votes)
37 views10 pages

CPGM 1

The document describes an experiment on swapping two numbers using functions in C programming. It includes the aim, algorithm, program code with comments, sample input/output and result. The program defines a swap function that takes two integer pointers as arguments, swaps their values using a temporary variable, and is called to swap the values of two integers entered by the user. It prints the numbers before and after the swap to show the values have been exchanged.

Uploaded by

sundariprabhu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views10 pages

CPGM 1

The document describes an experiment on swapping two numbers using functions in C programming. It includes the aim, algorithm, program code with comments, sample input/output and result. The program defines a swap function that takes two integer pointers as arguments, swaps their values using a temporary variable, and is called to swap the values of two integers entered by the user. It prints the numbers before and after the swap to show the values have been exchanged.

Uploaded by

sundariprabhu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Expt. No.

Date:

6. FACTORIAL OF A NUMBER

AIM

ALGORITHM

1
PROGRAM

#include<stdio.h>//defines standard IO operations


#include<conio.h>//used to access many built
built-in function for IO
void main()
{
int num,i;
int fact=1;
printf("\n\t\t\t\tFACTORIAL
tFACTORIAL OF A NUMBER");
printf("\n\t\t\t\t*********************");
t*********************");
printf("\nEnter The Number To Factorial:");
scanf("%d",&num);
if(num==0)
{
printf("The Factorial Of The Given Number Is '1'");
}
else
{
for(i=num;i>=1;i--)
{
fact=fact*i;
}
printf("The Factorial Of %d Is %d",num,fact);
}
getch();}

SAMPLE INPUT/OUTPUT

RESULT

2
Expt. No. Date:

7. FINDING AVERAGE OF FOUR NUMBERS

AIM

ALGORITHM

3
PROGRAM

#include<stdio.h>//defines standard IO operations


#include<conio.h>//used to access many built
built-in function for IO
void main()
{
int a,b,c,d;
float avg;
printf("\n\t\t\t\tFINDING
tFINDING AVERAGE OF FOUR NUMBERS");
printf("\n\t\t\t\t*******************************
t*************************************");
printf("\nEnter 4 integers:\nn ");
scanf("%d\t%d\t%d\t%d",t%d", &a,&b,&c,&d);
avg=(float)(a+b+c+d)/4.0;
printf("Average of %d, %d, %d and %d is = %0.2f", a,b,c,d,avg);
getch();
}
SAMPLE INPUT/OUTPUT

RESULT

4
Expt. No. Date:

8. ADDITION OF TWO MATRICES

AIM

ALGORITHM

5
PROGRAM

#include<stdio.h>//defines standard IO operations


#include<conio.h>//used to access many built-in function for IO
void main()
{
int n,a[3][3],b[3][3],c[3][3],i,j;
printf("\n\t\t\t\tADDITION OF TWO MATRICES");
printf("\n\t\t\t\t************************");
printf("\n Enter the elements of Matrix 1: \n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\n Enter the elements of Matrix 2: \n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("\nMatrix 1");
for(i=0;i<3;i++)
{
printf("\n");
for(j=0;j<3;j++)
{
printf("\t%d ",a[i][j]);
}
}
printf("\nMatrix 2");
for(i=0;i<3;i++)
{
printf("\n");
for(j=0;j<3;j++)

6
{
printf("\t%d ",b[i][j]);
}
}

for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
printf("\nSum of the two matrices is ");
for(i=0;i<3;i++)
{
printf("\n");
for(j=0;j<3;j++)
{
printf("\t%d ",c[i][j]);
}
}
getch();
}

7
SAMPLE INPUT/OUTPUT

RESULT

8
Expt. No. Date:

9.SWAPPING OF TWO UMBERS USING FUNCTIOS

AIM

ALGORITHM

9
PROGRAM

#include<stdio.h>//defines standard IO operations


#include<conio.h>//used to access many built
built-in function for IO
void swap(int *,int *);// function declaration
int main()
{
int n1,n2;
printf("\n\t\t\t\tSWAPPING
tSWAPPING USING FUNCTIONS");
printf("\n\t\t\t\t************************");
t************************");
printf("\nInput
nInput 1st number : ");
scanf("%d",&n1);
printf("\nInput
nInput 2nd number : ");
scanf("%d",&n2);
printf("\nBefore
nBefore swapping: n1 = %d, n2 = %d ",n1,n2);
swap(&n1,&n2); // function call
printf("\nAfter
nAfter swapping: n1 = %d, n2 = %d \n\n",n1,n2);
return 0;
}
void swap(int *p,int *q) // function
tion definition
{
int tmp;
tmp = *p;
*p=*q;
*q=tmp;
}
SAMPLE INPUT/OUTPUT

RESULT

10

You might also like