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

C Programming Functions and Examples

The document contains multiple C programming tasks, each with a specific function to implement. These include calculating the sum and average of integers, finding prime factors, raising a number to a power, calculating student marks, converting years to Roman numerals, determining leap years, and circularly shifting values. Each task is accompanied by a sample program demonstrating the required functionality.

Uploaded by

hamnakashifch17
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)
3 views8 pages

C Programming Functions and Examples

The document contains multiple C programming tasks, each with a specific function to implement. These include calculating the sum and average of integers, finding prime factors, raising a number to a power, calculating student marks, converting years to Roman numerals, determining leap years, and circularly shifting values. Each task is accompanied by a sample program demonstrating the required functionality.

Uploaded by

hamnakashifch17
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

 5.1. Write a function that is called by function main () and receives five integers.

The
function should print the sum of five integers and return the average to main. The
average is printed in main.
Program:
#include <stdio.h>
float getAverage(int a, int b, int c, int d, int e) {
int sum = a + b + c + d + e;
printf("Sum = %d\n", sum);
return sum / 5.0;
}
int main() {
int num1, num2, num3, num4, num5;
float average;
printf("Enter 5 integers: ");
scanf("%d %d %d %d %d", &num1, &num2, &num3, &num4, &num5);
average = getAverage(num1, num2, num3, num4, num5);
printf("Average = %.2f\n", average);
return 0;
}
Output:

 5.2. A positive integer is entered through the keyboard, write a program to obtain the
prime factors of the number.
Program:
#include <stdio.h>
int main() {
int num, i;
printf("Enter a positive integer: ");
scanf("%d", &num);
printf("Prime factors of %d are: ", num);
while (num % 2 == 0) {
printf("2 ");
num = num / 2;
}
for (i = 3; i <= num; i = i + 2) {
while (num % i == 0) {
printf("%d ", i);
num = num / i;
}
}
return 0;
}
Output:

 5.3. Write a function to raise a floating point number to an integer power


Program:
#include <stdio.h>
float power(float base, int exponent) {
float result = 1;
int i;
for (i = 1; i <= exponent; i++) {
result = result * base;
}
return result;
}
int main() {
float base, result;
int exponent;
printf("Enter base (e.g., 2.5): ");
scanf("%f", &base);
printf("Enter exponent (e.g., 3): ");
scanf("%d", &exponent);
result = power(base, exponent);
printf("%.2f raised to power %d is %.2f\n", base, exponent, result);
return 0;
}
Output:

 5.4. Write a function that receives marks received by a student in three subjects and
returns the average and percentage of these marks. Call this function from main( ) and
print the results in main( ).
Program:
#include <stdio.h>
void calculate(float m1, float m2, float m3, float *avg, float *percent) {
*avg = (m1 + m2 + m3) / 3;
*percent = ((m1 + m2 + m3) / 300) * 100;
}
int main() {
float mark1, mark2, mark3;
float average, percentage;
printf("Enter marks in 3 subjects (out of 100): ");
scanf("%f %f %f", &mark1, &mark2, &mark3);
calculate(mark1, mark2, mark3, &average, &percentage);
printf("Average = %.2f\n", average);
printf("Percentage = %.2f%%\n", percentage);
return 0;
}
Output:

 6.1. Write a general-purpose function to convert any given year into its roman
equivalent. The following table shows the roman equivalents of decimal numbers:
Program:
#include <stdio.h>
void convertToRoman(int year) {
int decimal[] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
char *roman[] = {"m", "cm", "d", "cd", "c", "xc", "l", "xl", "x", "ix", "v", "iv", "i"};
int i = 0;
printf("Roman equivalent: ");
while (year > 0) {
while (year >= decimal[i]) {
printf("%s", roman[i]);
year = year - decimal[i];
}
i++;
}
printf("\n");
}
int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);
convertToRoman(year);
return 0;
}
Output:
 6.2. Any year is entered through the keyboard. Write a function to determine whether
the year is a leap year or not.
Program:
#include <stdio.h>
int isLeapYear(int year) {
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
return 1;
} else {
return 0; }
}
int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);
if (isLeapYear(year)) {
printf("%d is a leap year.\n", year);
} else {
printf("%d is not a leap year.\n", year);
}
return 0;
}
Output:
 6.3. Given three variables x, y, z write a function to circularly shift their values to right. In
other words if x = 5, y = 8, z = 10 after circular shift y = 5, z = 8, x =10 after circular shift y
= 5, z = 8 and x = 10. Call the function with variables a, b, c to circularly shift values.
Program:
#include <stdio.h>
void circularShift(int *x, int *y, int *z) {
int temp;
temp = *z;
*z = *y;
*y = *x;
*x = temp;
}
int main() {
int a, b, c;
printf("Enter three values (a b c): ");
scanf("%d %d %d", &a, &b, &c);
printf("Before shift: a = %d, b = %d, c = %d\n", a, b, c);
circularShift(&a, &b, &c);
printf("After shift: a = %d, b = %d, c = %d\n", a, b, c);
return 0;
}
Output:

You might also like