0% found this document useful (0 votes)
6 views8 pages

C Programs for Basic Functions and Pointers

The document contains multiple C programs demonstrating various functions, including finding the maximum of two numbers, calculating the area of a circle, displaying array elements, computing factorials recursively, checking even or odd numbers, and using pointers for arithmetic operations. It also illustrates the use of local and global variables. Each program includes user input and prints the results accordingly.

Uploaded by

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

C Programs for Basic Functions and Pointers

The document contains multiple C programs demonstrating various functions, including finding the maximum of two numbers, calculating the area of a circle, displaying array elements, computing factorials recursively, checking even or odd numbers, and using pointers for arithmetic operations. It also illustrates the use of local and global variables. Each program includes user input and prints the results accordingly.

Uploaded by

Rajat singh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Maximum of two numbers-Function

#include <stdio.h>
int max(int a, int b) {
if(a > b)
return a;
else
return b;
}
int main() {
int x, y;
printf("Enter two integers: ");
scanf("%d %d", &x, &y);
printf("Max value = %d", max(x, y));
return 0;
}

Given function :float area(float


radius) ,compute area of circle
#include <stdio.h>
float area(float radius); // function declaration
int main() {
float r;
printf("Enter radius: ");
scanf("%f", &r);
printf("Area of circle = %f", area(r));
return 0;
}
float area(float radius) {
return 3.14 * radius * radius;
}

Program with function displayArray(int arr[],


int n)
#include <stdio.h>
void displayArray(int arr[], int n) {
printf("Array elements: ");
for(int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}

int main() {
int n;
printf("Enter number of elements: ");
scanf("%d", &n);

int arr[n];
printf("Enter %d integers:\n", n);
for(int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

displayArray(arr, n); //Function call


return 0;
}

Program with recursive factorial function


#include <stdio.h>
int factorial(int n) {
if(n == 0 || n == 1)
return 1;
else
return n * factorial(n - 1);
}

int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("Factorial of %d = %d\n", num,
factorial(num));
return 0;
}

Program with function checkEvenOdd(int n)


that returns 1 for even, 0 for odd
#include <stdio.h>

int checkEvenOdd(int n) {
if (n % 2 == 0)
return 1; // even
else
return 0; // odd
}

int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);

if (checkEvenOdd(num) == 1)
printf("%d is Even\n", num);
else
printf("%d is Odd\n", num);

return 0;
}

Program to add two integers using pointers


#include <stdio.h>

int main() {
int a, b, sum;
int *p1, *p2;

printf("Enter two integers: ");


scanf("%d %d", &a, &b);

p1 = &a;
p2 = &b;

sum = *p1 + *p2;

printf("Sum = %d\n", sum);

return 0;
}
Program to find the biggest of three integers using
pointers
#include <stdio.h>
int main() {
int a, b, c;
int *p1, *p2, *p3;
printf("Enter three integers: ");
scanf("%d %d %d", &a, &b, &c);
p1 = &a;
p2 = &b;
p3 = &c;
if (*p1 >= *p2 && *p1 >= *p3)
printf("Biggest = %d\n", *p1);
else if (*p2 >= *p1 && *p2 >= *p3)
printf("Biggest = %d\n", *p2);
else
printf("Biggest = %d\n", *p3);
return 0;
}

Program to find the largest among three


numbers using functions
#include <stdio.h>

int largest(int x, int y, int z) {


if (x >= y && x >= z)
return x;
else if (y >= x && y >= z)
return y;
else
return z;
}

int main() {
int a, b, c;

printf("Enter three numbers: ");


scanf("%d %d %d", &a, &b, &c);

printf("Largest = %d\n", largest(a, b, c));

return 0;
}

Program to Demonstrate Local and Global


Variables
#include <stdio.h>

int globalVar = 10; // Global variable

void display() {
int localVar = 5; // Local variable
printf("Inside display(): Global: %d, Local: %d\
n", globalVar, localVar);
}

int main() {
int localvar = 20; // Local variable of main
printf("Inside main() :Global: %d, Local: %d\n",
globalVar, localvar);

display();

return 0;
}

Output
Inside main() :Global: 10, Local: 20
Inside display(): Global: 10, Local: 5

You might also like