PROGRAMS ON FUNCTIONS
[Link] for sum of two numbers using Functions
#include<stdio.h>
// Function Declaration
Int add(int, int);
// Function Definition
int add(int x, int y) // return type = int, parameters = x, y
{
int sum;
sum = x + y;
return sum; // return statement
}
int main()
{
int result;
result = add(4, 6); // function call
printf("Sum = %d", result);
return 0;
}
[Link] on multiply of two numbers using Functions
return_type function_name(data_type1, data_type2, ...);
#include <stdio.h>
// Function Prototype
int multiply(int, int);
int main()
{
int result;
// Function Call
result = multiply(3, 4);
printf("Product = %d", result);
return 0;
}
// Function Definition
int multiply(int a, int b)
{
return a * b;
}
[Link] to perform arthematic operations
#include<stdio.h>
// Function declarations
float add(float a, float b);
float subtract(float a, float b);
float multiply(float a, float b);
float divide(float a, float b);
// Function definitions
float add(float a, float b)
{
return a + b;
}
float subtract(float a, float b)
{
return a - b;
}
float multiply(float a, float b)
{
return a * b;
}
float divide(float a, float b)
{
return a / b;
}
int main()
{
float num1, num2;
// Input
printf("Enter two numbers: ");
scanf("%f %f", &num1, &num2);
// Function calls and output
printf("\nAddition = %.2f", add(num1, num2));
printf("\nSubtraction = %.2f", subtract(num1, num2));
printf("\nMultiplication = %.2f", multiply(num1, num2));
if(num2 != 0)
printf("\nDivision = %.2f", divide(num1, num2));
else
printf("\nDivision not possible");
return 0;
}
[Link] of a Number
#include<stdio.h>
int factorial(int n);
// Function definition
int factorial(int n)
{
int i, fact = 1;
for(i = 1; i <= n; i++)
{
fact = fact * i;
}
return fact;
}
int main()
{
int num, result;
// Input
printf("Enter a number: ");
scanf("%d", &num);
// Function call
result = factorial(num);
// Output
printf("Factorial of %d = %d", num, result);
return 0;
}
[Link] or not
#include<stdio.h>
// Function prototype
int isPrime(int n);
// Function definition
int isPrime(int n)
{
int i;
if(n <= 1)
return 0;
for(i = 2; i < n; i++)
{
if(n % i == 0)
return 0;
}
return 1;
}
int main()
{
int num;
// Input
printf("Enter a number: ");
scanf("%d", &num);
// Function call and output
if(isPrime(num))
printf("%d is a Prime number", num);
else
printf("%d is not a Prime number", num);
return 0;
}
6. sum of first n natural numbers.
#include<stdio.h>
// Function Prototype
int sumN(int);
// Function Definition
int sumN(int num)
{
int i, sum = 0;
for (i = 1; i <= num; i++)
{
sum += i;
}
return sum;
}
int main()
{
int n, result;
printf("Enter a number: ");
scanf("%d", &n);
// Function Call
result = sumN(n);
printf("Sum of first %d natural numbers = %d", n, result);
return 0;
}
[Link] of a number
#include<stdio.h>
int cube(int);
int cube(int n)
{
return n * n * n;
}
int main()
{
int num, result;
printf("Enter a number: ");
scanf("%d", &num);
result = cube(num);
printf("Cube of %d is %d", num, result);
return 0;
}
[Link] of digits[functions]
#include<stdio.h>
int sumOfDigits(int n) {
int digit, sum = 0;
while(n > 0) {
digit = n % 10;
sum = sum + digit;
n = n / 10;
}
return sum;
}
int main() {
int number, result;
printf("Enter a number: ");
scanf("%d", &number);
result = sumOfDigits(number);
printf("Sum of digits = %d", result);
return 0;
}
[Link][functions]
#include<stdio.h>
int fibonacci(int n) {
int a = 0, b = 1, c, i;
if(n == 1)
return a;
if(n == 2)
return b;
for(i = 3; i <= n; i++) {
c = a + b;
a = b;
b = c;
}
return c;
}
int main() {
int n, i;
printf("Enter number of terms: ");
scanf("%d", &n);
printf("Fibonacci Series:\n");
for(i = 1; i <= n; i++) {
printf("%d ", fibonacci(i));
}
return 0;
}
[Link] of marks
#include<stdio.h>
int rameshMarks = 80;
int sureshMarks = 90;
void exchangeMarks() {
int temp;
temp = rameshMarks;
rameshMarks = sureshMarks;
sureshMarks = temp;
}
int main() {
printf("--- Before Mutual Exchange ---\n");
printf("Ramesh: %d, Suresh: %d\n", rameshMarks, sureshMarks);
exchangeMarks();
printf("Ramesh: %d, Suresh: %d\n", rameshMarks, sureshMarks);
return 0;
}
[Link] of a two numbers using call by value[FUNCTIONS]
#include<stdio.h>
// Function prototype
void swap(int a, int b);
// Function definition
void swap(int a, int b)
{
int temp;
temp = a;
a = b;
b = temp;
printf("\n\nInside swap function:");
printf("\na = %d, b = %d", a, b);
}
int main()
{
int num1, num2;
// Input
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
printf("\nBefore swapping:");
printf("\nnum1 = %d, num2 = %d", num1, num2);
// Function call
swap(num1, num2);
printf("\nAfter function call in main:");
printf("\nnum1 = %d, num2 = %d", num1, num2);
return 0;
}
11. Write a C program to swap two numbers using call by reference [FUNCTIONS]
#include<stdio.h>
void swap(int *a, int *b);
// Function definition
void swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
int main()
{
int num1, num2;
// Input
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
printf("\nBefore swapping:");
printf("\nnum1 = %d, num2 = %d", num1, num2);
// Function call using addresses
swap(&num1, &num2);
printf("\nAfter swapping:");
printf("\nnum1 = %d, num2 = %d", num1, num2);
return 0;
}
12. Write a C program that prints the value and address of variables.[Passing
parameter to function]
#include<stdio.h>
int main()
{
int a = 10;
float b = 5.5;
char c = 'A';
// Printing values
printf("Value of a = %d\n", a);
printf("Value of b = %.2f\n", b);
printf("Value of c = %c\n\n", c);
// Printing addresses
printf("Address of a = %p\n", &a);
printf("Address of b = %p\n", &b);
printf("Address of c = %p\n", &c);
return 0;
}
[Link] to Read student marks • Calculate average • Assign grade based on
average • Display result
#include<stdio.h>
// Function to read marks
void readMarks(int marks[], int n) {
for(int i = 0; i < n; i++) {
printf("Enter marks for subject %d: ", i + 1);
scanf("%d", &marks[i]);
}
}
// Function to calculate average
float calculateAverage(int marks[], int n) {
int sum = 0;
for(int i = 0; i < n; i++) {
sum += marks[i];
}
return (float)sum / n;
}
// Function to assign grade
char assignGrade(float avg) {
if(avg >= 90)
return 'A';
else if(avg >= 75)
return 'B';
else if(avg >= 60)
return 'C';
else if(avg >= 50)
return 'D';
else
return 'F';
}
// Function to display result
void displayResult(float avg, char grade) {
printf("\nAverage Marks = %.2f\n", avg);
printf("Grade = %c\n", grade);
}
// Main function
int main() {
int n;
printf("Enter number of subjects: ");
scanf("%d", &n);
int marks[n];
readMarks(marks, n);
float avg = calculateAverage(marks, n);
char grade = assignGrade(avg);
displayResult(avg, grade);
return 0;
}
14. Explain how local and global variables are accessed through pointers in the
program[scope of variables function]
#include<stdio.h>
int globalVar = 100;
void modifyValue(int *ptr)
{
int localVar = 20;
*ptr = *ptr + localVar;
printf("Inside Function\n");
printf("Local Variable = %d\n", localVar);
printf("Modified Global Variable = %d\n", *ptr);
}
int main()
{
int *p;
p = &globalVar;
printf("Before Modification = %d\n", globalVar);
modifyValue(p);
printf("After Modification = %d\n", globalVar);
return 0;
}
15.[scope of variable functions-additional]
#include<stdio.h>
// Global variable
int globalVar = 100;
// Function definition
void display()
{
// Local variable
int localVar = 50;
printf("Inside display function:\n");
printf("Global Variable = %d\n", globalVar);
printf("Local Variable = %d\n", localVar);
}
int main()
{
// Local variable in main
int mainLocal = 25;
printf("Inside main function:\n");
printf("Global Variable = %d\n", globalVar);
printf("Main Local Variable = %d\n\n", mainLocal);
// Function call
display();
return 0;
}
16. In a company their are 3 employees and 1 manager. Manager can see all the
employees salary details. Analyze this scenario and demonstrate global variables
across multiple functions.[scope of variables]
#include<stdio.h>
// GLOBAL VARIABLES: Declared outside main, accessible by all functions
float emp1_sal = 3000.0;
float emp2_sal = 4500.0;
float emp3_sal = 2800.0;
void displayManagerReport() {
printf("\n--- Manager's Salary Overview ---\n");
printf("Employee 1 Salary: $%.2f\n", emp1_sal);
printf("Employee 2 Salary: $%.2f\n", emp2_sal);
printf("Employee 3 Salary: $%.2f\n", emp3_sal);
float total = emp1_sal + emp2_sal + emp3_sal;
printf("Total Company Expenditure: $%.2f\n", total);
}
void updateSalary() {
// Directly modifying global variables
emp1_sal += 500.0;
printf("\n[System Update] Employee 1 received a $500 raise.\n");
}
int main() {
printf("System Boot: Loading Employee Database...\n");
// Manager checks details initially
displayManagerReport();
// A separate function updates the data
updateSalary();
// Manager checks again - the data has changed without a 'return' value
displayManagerReport();
return 0;
}
POINTERS:
[Link] variable value and address using pointer
#include<stdio.h>
int main()
{
int a = 25;
// Pointer declaration
int *ptr;
// Pointer initialization
ptr = &a;
// Displaying values
printf("Value of a = %d\n", a);
printf("Address of a = %p\n", &a);
printf("Value stored in ptr = %p\n", ptr);
printf("Value pointed by ptr = %d\n", *ptr);
return 0;
}
[Link] on how variables are stored and accessed[pointers]
#include<stdio.h>
int main()
{
int a = 10;
float b = 5.5;
// Printing values
printf("Value of a = %d\n", a);
printf("Value of b = %.2f\n\n", b);
// Printing memory addresses
printf("Address of a = %p\n", &a);
printf("Address of b = %p\n\n", &b);
// Accessing values using pointers
int *p1 = &a;
float *p2 = &b;
printf("Value of a using pointer = %d\n", *p1);
printf("Value of b using pointer = %.2f\n", *p2);
return 0;
}
[Link] of variables before and after using pointer
#include<stdio.h>
int main()
{
int a = 10, b = 20;
int *ptr1, *ptr2;
ptr1 = &a;
ptr2 = &b;
printf("Before Modification\n");
printf("a = %d\n", a);
printf("b = %d\n", b);
*ptr1 = *ptr1 + 5;
*ptr2 = *ptr2 + 10;
printf("\nAfter Modification\n");
printf("a = %d\n", a);
printf("b = %d\n", b);
return 0;
}
[Link]
#include<stdio.h>
int main()
{
int a = 25; // Normal variable
int *ptr; // Pointer variable
// Storing address of a in pointer
ptr = &a;
// Printing value and address
printf("Value of a = %d\n", a);
printf("Address of a = %p\n", &a);
// Printing pointer value
printf("Pointer ptr stores address = %p\n", ptr);
// Accessing value using pointer
printf("Value using pointer = %d\n", *ptr);
return 0;
}
5. Construct a C program using pointers to perform arithmetic operations on two
numbers using indirect addressing. [Understanding the computer memory]
#include<stdio.h>
int main()
{
int a, b;
int *p1, *p2;
// Input
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
// Assigning addresses to pointers
p1 = &a;
p2 = &b;
// Arithmetic operations using indirect addressing
printf("\nAddition = %d", (*p1 + *p2));
printf("\nSubtraction = %d", (*p1 - *p2));
printf("\nMultiplication = %d", (*p1 * *p2));
printf("\nDivision = %d", (*p1 / *p2));
return 0;
}
6. Write a C program to swap two numbers using call by reference with
pointers[pointers]
#include<stdio.h>
void swap(int *a, int *b);
// Function definition
void swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
int main()
{
int num1, num2;
// Input
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
printf("\nBefore swapping:");
printf("\nnum1 = %d, num2 = %d", num1, num2);
// Function call using addresses
swap(&num1, &num2);
printf("\nAfter swapping:");
printf("\nnum1 = %d, num2 = %d", num1, num2);
return 0;
}
7. Analyze the effect of multiple pointer operations on array elements by comparing
pointer addresses, dereferenced values, and modified data at different stages of
program execution.[declaring pointer variables]
#include<stdio.h>
int main()
{
int arr[5] = {10, 20, 30, 40, 50};
int *ptr;
ptr = arr;
printf("Stage 1\n");
printf("Address = %p\n", ptr);
printf("Value = %d\n", *ptr);
ptr++;
printf("\nStage 2\n");
printf("Address = %p\n", ptr);
printf("Value = %d\n", *ptr);
*ptr = *ptr + 5;
printf("\nStage 3\n");
printf("Modified Value = %d\n", *ptr);
ptr = ptr + 2;
printf("\nStage 4\n");
printf("Address = %p\n", ptr);
printf("Value = %d\n", *ptr);
return 0;
}
8. Analyze and write c program for this scenario: In college M201 had 50 students
and M202 had 50 students, college need total count of students from these two
rooms using Room Names.[Pointers]
#include<stdio.h>
int getRoomTotal(int *ptr1, int *ptr2) {
int total = *ptr1 + *ptr2;
return total;
}
int main() {
int M201 = 50;
int M202 = 50;
int totalCount;
printf("College Registrar: Pointer-Based Student Count\n");
totalCount = getRoomTotal(&M201, &M202);
printf("Room M201 Address: %p | Students: %d\n", (void*)&M201, M201);
printf("Room M202 Address: %p | Students: %d\n", (void*)&M202, M202);
printf("\nConsolidated Total: %d students\n", totalCount);
return 0;
}
9. Analyze and write c program using pointer for this scenario: In a quiz competition
maximum of 2 member, team captain will be decided based on their age. Display
captain details.[pointers]
#include<stdio.h>
int getCaptainAge(int *age1, int *age2) {
if (*age1 > *age2) {
return *age1;
} else {
return *age2;
}
}
int main() {
int member1Age, member2Age;
int captainAge;
printf("Quiz Competition: Captain Selection\n");
printf("Enter Age of Member 1: ");
scanf("%d", &member1Age);
printf("Enter Age of Member 2: ");
scanf("%d", &member2Age);
captainAge = getCaptainAge(&member1Age, &member2Age);
printf("Member 1 Age: %d\n", member1Age);
printf("Member 2 Age: %d\n", member2Age);
printf("The Captain's Age is: %d\n", captainAge);
return 0;
}
10. In a company their are 3 employees and 1 manager. Manager can see all the
employees salary details. Analyze this scenario and demonstrate global variables
across multiple functions.[Pointers]
#include<stdio.h>
int main() {
int marks[5];
int *p = marks;
int topper, lowest;
int topIndex = 0, lowIndex = 0;
printf("Enter marks of 5 students:\n");
for(int i = 0; i < 5; i++) {
printf("Student %d: ", i + 1);
scanf("%d", (p + i));
}
topper = *p;
lowest = *p;
for(int i = 1; i < 5; i++) {
if(*(p + i) > topper) {
topper = *(p + i);
topIndex = i;
}
if(*(p + i) < lowest) {
lowest = *(p + i);
lowIndex = i;
}
}
printf("\nTopper : Student %d with %d marks\n",
topIndex + 1, topper);
printf("Lowest Scorer : Student %d with %d marks\n",
lowIndex + 1, lowest);
return 0;
}