0% found this document useful (0 votes)
0 views

Lab6_2025

The document provides examples of C programming concepts including user-defined functions, passing arguments, and array manipulation. It includes code snippets for displaying text, calculating sums, accessing array elements, and passing arrays as function arguments. Additionally, it presents programming problems for practice, such as calculating the area and volume of a sphere, computing averages, and reversing an array.
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)
0 views

Lab6_2025

The document provides examples of C programming concepts including user-defined functions, passing arguments, and array manipulation. It includes code snippets for displaying text, calculating sums, accessing array elements, and passing arrays as function arguments. Additionally, it presents programming problems for practice, such as calculating the area and volume of a sphere, computing averages, and reversing an array.
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/ 3

Computer Programming Lab

Printing text using user defined function: Passing argument and printing its value:
#include<stdio.h> #include<stdio.h>
void Display(); void printValue(int n);
int main() int main()
{
{
printValue(100);
Display(); printValue(25);
return 0; return 0;
} }
void Display() void printValue(int n)
{ {
printf("Hello Wolrd!\n"); printf("The number is %d\n", n);
}
}

Calculating sum by passing arguments and returning the result:


#include<stdio.h> int Sum(int x, int y)
int Sum(int x, int y); {
int main() int result = x + y;
{ return result;
int var1, var2; }
scanf("%d %d", &var1,
&var2);
printf("%d + %d = %d\n", var2,
var1,
Sum(var1, var2));
return 0;
}
Array declaration, input and output:

#include<stdio.h> #include<stdio.h> #include<stdio.h>


int main() int main() int main()
{ { {
int a[5]={2,4,8,3,5}; int a[2]; float a[2];
printf(“%d\n”,a[0]); scanf(“%d”,&a[0]); scanf(“%f”,&a[0]);
printf(“%d\n”,a[1]); scanf(“%d”,&a[1]); scanf(“%f”,&a[1]);
printf(“%d\n”,a[2]); printf(“%d\n”,a[0]); printf(“%.2f\n”,a[0]);
printf(“%d\n”,a[3]); printf(“%d\n”,a[1]); printf(“%.2f\n”,a[1]);
return 0; return 0; return 0;
} } }
Accessing array elements using loop:

#include<stdio.h> #include<stdio.h>
int main() int main()
{ {
int n; int n,sum=0;
printf("Enter the length of Array:"); printf("Enter the length of Array:");
scanf("%d",&n); scanf("%d",&n);
int a[n]; int a[n];
for(int i=0;i<n;i++) for(int i=0;i<n;i++)
{ {
scanf("%d",&a[i]); scanf("%d",&a[i]);
} }
for(int i=0;i<n;i++) for(int i=0;i<n;i++)
{ {
printf("a[%d]= %d\n",i,a[i]); sum=sum+a[i];
} }
return 0; printf("The sum is: %d",sum);
} return 0;
}
Passing arrays as function arguments:

#include<stdio.h> double getAverage(int arr[], int size)


double getAverage(int arr[], int size); {
int main () int i;
{ double avg;
int balance[5] = {1000, 2, 3, 17, 50}; double sum;
double avg;
for (i = 0; i < size; ++i)
{
avg = getAverage( balance, 5 ) ; sum += arr[i];
}
printf( "Average value: %f ", avg );
avg = sum / size;
return 0;
} return avg;
}
Problems:

1. Write two functions –


void findArea(int radius) to find the area of a sphere and
void findVolume(int radius) to find the volume of a sphere.
Functions should print the Area and the Volume. Call the two functions from main. Take input
inside main and pass those to the above functions.

2. Write the function int computeAverage(int a, int b) which takes 2 integers as input and return
their average. Call the function from main, store the average returned by the function and print it.

3. Write a program that takes 5 integers as input, stores them in an array and prints them in the
reverse order of the input.

Sample Output:

Enter array elements: 2 3 5 4 9


Reversed order: 9 4 5 3 2

4. Write a program that takes 5 integers as input, stores them in an array and finds the highest number in
the array.

Sample Output:

Enter array elements: 2 3 15 4 9


Largest element: 15

You might also like