Arrays, Pointers, Functions Experiments
Arrays, Pointers, Functions Experiments
Sample Output:
enter total number of elements 6
enter values : 34, 23, 56, 12 , 24, 78
AFTER SORTING 12 , 23 , 24, 34 , 56, 78
Actual Output :
#include<stdio.h>
int main()
{
int i,j,a[3][3],b[3][3],c[3][3];
printf("Enter first matrix values\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter second matrix values\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",a[i][j]+b[i][j]);
}
}
return 0;
}
Actual Output :
#include<stdio.h>
int main()
{
int i,j,a[3][3],b[3][3],c[3][3];
printf("Enter first matrix values\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter second matrix values\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",a[i][j]*b[i][j]);
}
}
return 0;
}
Actual Output:
Entered Matrix:
2 3 4
5 6 4
Transpose of Matrix:
2 5
3 6
4 4
ACTUAL OUTPUT :
Aim : To develop C program to find factorial of an integer using non recursive function
Software used : TurboC++/DevC++
Program:
#include<stdio.h>
void main()
{
int n,fact=1,i;
printf("enter number to find factorial");
scanf("%d",&n);
if(n>0)
{
for(i=1;i<=n;i++)
{
fact=fact*i;
}
printf("factorial of %d is %d",n,fact);
}
Sample output:
enter number to find factorial 5
factorial of 5 is 120
Actual Output :
#include<stdio.h>
long factorial(int);//
int main()
{
int number;
long fact = 1;
printf("Enter a number to calculate its factorial\n");
scanf("%d", &number);
printf("%d! = %ld\n", number, factorial(number));
return 0;
}
long factorial(int n)
{
int c;
long result = 1;
for (c = 1; c <= n; c++)
{
result = result * c;
}
return result;
}
Sample Output:
Enter a number to calculate its factorial 6
6! = 720
Actual Output :
Aim : To develop C program to find GCD(Greatest Common Divisor) using non recursive
function
Software used : TurboC++/DevC++
Program:
#include<stdio.h>
void main()
{
int a,b,x,y;
printf("enter numbers to GCD");
scanf("%d%d",&a,&b);
x=a;
y=b;
while(a!=b)
{
if(a<b)
{
b=b-a;
}
else
{
a=a-b;
}
}
printf("gcd of %d , %d is %d",x,y,a);
}
Sample Output:
Enter numbers to GCD 81, 153
Gcd of 81, 153 is 9
Actual Output :
#include<stdio.h>
void main()
{
int a,b,res;
printf("enter numbers to GCD");
scanf("%d%d",&a,&b);
res=gcd(a,b);
printf("gcd of %d , %d is %d",a,b,res);
}
int gcd(int a,int b)
{
if(a==b)
{
return a;
}
else
{
if(a<b)
{
return gcd(a,b-a);
}
else
{
return gcd(a-b,b);
}
}
}
Sample Output:
enter numbers to GCD 81 153
Gcd of 81, 153 is 9
Actual Output :
Aim : To develop C program for reading elements using pointer into array and display the
values using array.
Software used : TurboC++/DevC++
Program:
#include <stdio.h>
int main()
{
int data[5], i;
printf("Enter elements: ");
for(i = 0; i < 5; ++i)
{
scanf("%d", data + i);
}
printf("You entered: \n");
for(i = 0; i < 5; ++i)
{
printf("%d\n", *(data + i));
}
return 0;
}
Sample Output
Enter elements: 1 2 3 5 4
You entered: 1 2 3 5 4
Actual Output :
SAMPLE OUTPUT:
Enter the size of array : 5
Enter 5 integers into array : 11 22 33 44 55
Elements of array in reverse order are :
Element 4 is : 55
Element 4 is : 44
Element 4 is : 33
Element 4 is : 22
Element 4 is : 11
ACTUAL OUTPUT:
******LEAVE AN EMPTY SPACE FOR OUTPUT*****************
Experiment 11 : Write a program for compute sum of n elements from array using pointer.
Aim : To develop a C a program for compute sum of n elements from array using pointer
Software used : TurboC++/DevC++
Program :
#include<stdio.h>
#include<conio.h>
void main()
{
int numArray[10];
int i, sum = 0;
int *ptr;
printf("\nEnter 10 elements : ");
for (i = 0; i < 10; i++)
{
scanf("%d", &numArray[i]);
}
ptr = numArray;
for (i = 0; i < 10; i++)
{
sum = sum + *ptr;
ptr++;
}
printf("The sum of array elements : %d", sum);
getch();
}
SAMPLE OUTPUT:
ENTER 10 elements 1 2 3 4 5 6 7 8 9
The sum of array elements 55
ACTUAL OUTPUT:
******LEAVE AN EMPTY SPACE FOR OUTPUT*****************
Experiment 12 : Write C programs to compute x^n using non recursive function
Aim : To develop a C program to compute x^n using non recursive function
Software used : TurboC++/ DevC++
Program :
#include<stdio.h>
void main()
{
int x,n,res=1;
printf("enter numbers x,n");
scanf("%d%d",&x,&n);
while(n>0)
{
res=res*x;
n--;
}
printf("result is %d",res);
}
Sample Output:
Enter numbers x , n 2 , 3
Result is 8
Actual Output :