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

C Functions for Basic Operations

The document contains multiple C programming examples demonstrating the use of functions for various tasks such as calculating the area of a circle, raising a number to a power, checking if a number is odd, summing digits of a number, reversing a number, printing a multiplication table, converting characters to uppercase, and more. Each example includes a main function that accepts user input and calls a specific function to perform the desired operation. Additionally, there is a menu-driven program for basic arithmetic operations and a function for swapping two values using pointers.

Uploaded by

Arshadali Ansari
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)
10 views10 pages

C Functions for Basic Operations

The document contains multiple C programming examples demonstrating the use of functions for various tasks such as calculating the area of a circle, raising a number to a power, checking if a number is odd, summing digits of a number, reversing a number, printing a multiplication table, converting characters to uppercase, and more. Each example includes a main function that accepts user input and calls a specific function to perform the desired operation. Additionally, there is a menu-driven program for basic arithmetic operations and a function for swapping two values using pointers.

Uploaded by

Arshadali Ansari
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

(1) Accept radius of a circle.

Pass it to a function area( ) which should


calculate and print the area of the
circle

float area(float radius);


#include <stdio.h>
int main() {
float r, result;

printf("Enter the radius of the circle: ");


scanf("%f", &r);
// Call the function 'area' and pass the radius 'r'
// The function will return the calculated area, which we store in 'result'
result = area(r);

printf("The area of the circle is: %.2f\n", result);

return 0; // End of program


}
float area(float radius) {
float pi = 3.14;
float result;

result = pi * radius * radius;


return result;
}

(2) Accept two numbers from the users in a and b. Pass them to a function
pow( ) which should print a raised to b
#include <stdio.h>

int pow(int a, int b);

int main() {
int a, b, result;
printf("Enter values for a and b: ");
scanf("%d %d", &a, &b);
result = pow(a, b); // store the returned value
printf("Result: %d", result);
return 0;
}
int pow(int a, int b) { // function returns a value
int p = 1;
while (b >= 1) {
p = p * a;
b = b - 1;
}
return p; // returns the result
}

(3) Accept any number from the user. Pass it to a function isodd( ) which
checks whether the entered number is odd or not.
#include <stdio.h>
void isodd(int num);
int main()
{
int num;

printf("Enter any number: ");


scanf("%d", &num);
isodd(num);
isodd(10);
return 0;
}
void isodd(int num)
{
if (num % 2 != 0)
{
printf("%d is an Odd number.\n", num);
}
else
{
printf("%d is not an Odd number.\n", num);
}
}

(4) Accept any 5 digit number from the user. pass it to a function add( )
which should calculate and print the addition of its individual digits
#include <stdio.h>
void add(int no)
{
int sum = 0, digit;
while (no > 0)
{
digit = no % 10; // Get the last digit
sum = sum + digit; // Add it to sum
no = no / 10; // Remove last digit
}

printf("Addition of individual digits: %d\n", sum);


}
int main()
{
int no;

printf("Enter a 5-digit number: ");


scanf("%d", &no);

add(no); // Function call

return 0;
}

(5) Accept any 5 digit number from the user, pass it to a function rev( )
which should reverse the given number and print it.
#include <stdio.h>

void rev(int num) {


int reverse = 0;
while (num != 0) {
int digit = num % 10;
reverse = reverse * 10 + digit;
num = num / 10;
}

printf("Reverse = %d\n", reverse);


}

int main() {
int no;

printf("Enter a 5-digit number: ");


scanf("%d", &no);

rev(no);

return 0;
}

(6) Accept any number from the user. Pass it to a function tab( ) which
should print the table of the entered number.
#include <stdio.h>
void tab(int num) {
for (int i = 1; i <= 10; i++) {
printf("%d x %d = %d\n", num, i, num * i);
}
}

int main() {
int n;
printf("Enter any number: ");
scanf("%d", &n);
tab(n);
return 0;
}
(7) Accept any character from the user, pass it to a function upp( ) which
should convert the character into uppercase and print.
#include <stdio.h>

// Function to convert to uppercase


void upp(char ch) {
if (ch >= 'a' && ch <= 'z') {
ch = ch - 32; // Convert to uppercase manually
printf("Uppercase = %c\n", ch);
}
else
printf(“Enter a lowercase value”);

int main() {
char ch;

printf("Enter any character: ");


scanf(" %c", &ch); // Notice the space before %c to consume any leftover
whitespace

upp(ch);

return 0;
}

(8) accept any character from the user. Pass it to a function chk( ) which
should check whether the entered character is an alphabet, digit or special
symbol
#include <stdio.h>

// Function to check character type


void chk(char ch) {
if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')) {
printf("The character is an alphabet.\n");
}
else if (ch >= '0' && ch <= '9') {
printf("The character is a digit.\n");
}
else {
printf("The character is a special symbol.\n");
}
}

int main() {
char ch;

printf("Enter any character: ");


scanf(" %c", &ch); // Note the space before %c to avoid input issues

chk(ch);

return 0;
}

(9) Write a function, which accepts a character and integer n as parameter


and displays the next n characters.
#include <stdio.h>

// Function to print next n characters


void nextChars(char ch, int n) {
for (int i = 1; i <= n; i++) {
ch = ch + 1;
printf("%c ", ch);
}
printf("\n");
}

int main() {
char ch;
int n;
printf("Enter a character: ");
scanf(" %c", &ch); // Note the space before %c

printf("Enter the number of next characters to display: ");


scanf("%d", &n);

nextChars(ch, n);

return 0;
}

(10) Write a menu driven program to calculate :-


1. Addition
2. Subtraction
3. Multiplication
4. Division
of two numbers by using function
#include <stdio.h>

// Function declarations
void add(float a, float b);
void subtract(float a, float b);
void multiply(float a, float b);
void divide(float a, float b);

int main() {
int choice;
float num1, num2;

printf("Enter two numbers: ");


scanf("%f %f", &num1, &num2);

printf("\nChoose an operation:\n");
printf("1. Addition\n");
printf("2. Subtraction\n");
printf("3. Multiplication\n");
printf("4. Division\n");
printf("Enter your choice (1-4): ");
scanf("%d", &choice);

switch (choice) {
case 1:
add(num1, num2);
break;
case 2:
subtract(num1, num2);
break;
case 3:
multiply(num1, num2);
break;
case 4:
divide(num1, num2);
break;
default:
printf("Invalid choice.\n");
}

return 0;
}

// Function definitions
void add(float a, float b) {
printf("Result = %.2f\n", a + b);
}

void subtract(float a, float b) {


printf("Result = %.2f\n", a - b);
}

void multiply(float a, float b) {


printf("Result = %.2f\n", a * b);
}
void divide(float a, float b) {
if (b != 0)
printf("Result = %.2f\n", a / b);
else
printf("Error: Division by zero.\n");
}

(11) WAP to accept 2 values in variables a and b, pass them to a function


swap which would swap the contents of a and b. Print the result in main
#include <stdio.h>

// Function prototype
void swap(int *x, int *y);

int main() {
int a, b;

// Accepting input from user


printf("Enter value of a: ");
scanf("%d", &a);

printf("Enter value of b: ");


scanf("%d", &b);

// Display values before swapping


printf("\nBefore swapping: a = %d, b = %d\n", a, b);

// Call the swap function


swap(&a, &b);

// Display values after swapping


printf("After swapping: a = %d, b = %d\n", a, b);

return 0;
}
// Function to swap two numbers using pointers
void swap(int *x, int *y) {
int temp;

temp = *x;
*x = *y;
*y = temp;
}

You might also like