0% found this document useful (0 votes)
5 views2 pages

C Functions for Basic Operations

The document contains several C programming functions demonstrating basic operations such as addition, factorial calculation, swapping numbers, finding the maximum of two numbers, checking if a number is positive, negative, or zero, and printing numbers from 1 to N. Each function is defined with a main function that calls it to execute the respective operation. The code snippets illustrate fundamental programming concepts and syntax in C.

Uploaded by

shobhakhatal1980
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)
5 views2 pages

C Functions for Basic Operations

The document contains several C programming functions demonstrating basic operations such as addition, factorial calculation, swapping numbers, finding the maximum of two numbers, checking if a number is positive, negative, or zero, and printing numbers from 1 to N. Each function is defined with a main function that calls it to execute the respective operation. The code snippets illustrate fundamental programming concepts and syntax in C.

Uploaded by

shobhakhatal1980
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

Function to Add Two Numbers int main() {

#include <stdio.h> printNumbers(5);


void add() { return 0;
int a = 10, b = 20; }
printf("Sum = %d\n", a + b); Function to Calculate Factorial
} #include <stdio.h>
int main() { void factorial(int n) {
add(); int fact = 1;
return 0; for(int i = 1; i <= n; i++)
} fact *= i;
Function with Parameters (Addition) printf("Factorial = %d\n", fact);
#include <stdio.h> }
void add(int a, int b) { int main() {
printf("Sum = %d\n", a + b); factorial(5);
} return 0;
int main() { }
add(5, 7); Function to Swap Two Numbers (Using Temporary
return 0; Variable)
} #include <stdio.h>
Function to Find Square of a Number void swap(int a, int b)
#include <stdio.h> {
void square(int n) { int temp = a;
printf("Square = %d\n", n * n); a = b;
} b = temp;
int main() { printf("After Swap: a=%d b=%d\n", a, b);
square(6); }
return 0; int main() {
} swap(3, 7);
Function to Find Maximum of Two Numbers return 0;
#include <stdio.h> }
void max(int a, int b) { Function to Check Positive, Negative or Zero
if (a > b) #include <stdio.h>
printf("Max = %d\n", a); void checkNumber(int n)
else {
printf("Max = %d\n", b); if(n > 0)
} printf("Positive\n");
int main() { else if(n < 0)
max(10, 25); printf("Negative\n");
return 0; else
} printf("Zero\n");
Function to Print Numbers from 1 to N }
#include <stdio.h> int main() {
void printNumbers(int n) { checkNumber(-5);
for(int i = 1; i <= n; i++) return 0;
printf("%d ", i); }
}

You might also like