Professional Documents
Culture Documents
SUBMITTED BY:
Muhammad Saad
REGISTRATION NO.:
CIIT/FA20-BEE-137/ISB
SEMESTER-SECTION:
2-C
COURSE:
Computer
Programming CSC141
Lab Report: 8
BS Electrical Engineering
Lab 8 – Functions
LAB TASKS
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>
#include <stdlib.h>
void main()
{
int a,b,c,d,e;
float sum, avg, std_dev;
func(a,b,c,d,e,&sum,&avg,&std_dev);
1 | Page CSC141Lab:08
}
Output:
2. A positive integer is entered through the keyboard, write a program to obtain the prime
factors of the number.
Program:
#include <stdio.h>
#include <stdlib.h>
int prime(int x)
{
int a;
for(a=2;a<=x;a++)
{
if(x%a==0)
{
2 | Page CSC141Lab:08
printf("%d x ",a);
x = x/a;
a--;
}
}
}
Output:
int main()
{
int base, exp;
long long result = 1;
printf("Enter Base: ");
scanf("%d", &base);
printf("Enter Exponent: ");
scanf("%d", &exp);
while (exp != 0)
{
result *= base;
--exp;
3 | Page CSC141Lab:08
}
printf("Answer = %lld", result);
return 0;
}
Output:
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>
#include <stdlib.h>
4 | Page CSC141Lab:08
func(a, b, c, &avg, &per);
Output:
POST-LAB TASKS
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>
#include <stdlib.h>
5 | Page CSC141Lab:08
void roman(int year);
int main()
{
int year;
printf("Enter Year: ");
scanf("%d", &year);
roman(year);
}
roman(int year)
{
if(year>=1000)
{
printf("M");
roman(year-1000);
}
else if(year>=500)
{
printf("D");
roman(year-500);
}
else if(year>=100)
{
printf("C");
roman(year-100);
}
else if(year>=50)
{
printf("L");
roman(year-50);
}
else if(year>=10)
{
printf("X");
roman(year-10);
}
else if(year>=5)
{
printf("V");
roman(year-5);
}
else if(year>=1)
{
printf("I");
roman(year-1);
}
6 | Page CSC141Lab:08
}
Output:
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>
#include <stdlib.h>
7 | Page CSC141Lab:08
i=0;
return i;
}
Output:
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>
#include <stdlib.h>
8 | Page CSC141Lab:08
shift(&x, &y, &z);
printf("\nAfter Shift:\tX: %d\tY: %d\tZ: %d",x,y,z);
}
int shift(int *a, int *b, int *c)
{
int temp;
temp = *c;
*c = *b;
*b = *a;
*a = temp;
}
Output:
9 | Page CSC141Lab:08