You are on page 1of 10

SUBMITTED TO:

Ma'am Amber Zeb

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 func(int a, int b, int c, int d, int e, float *sum, float


*avg, float *std_dev);

void main()
{
int a,b,c,d,e;
float sum, avg, std_dev;

printf("Enter 1st number: ");


scanf("%d", &a);
printf("Enter 2nd number: ");
scanf("%d", &b);
printf("Enter 3rd number: ");
scanf("%d", &c);
printf("Enter 4th number: ");
scanf("%d", &d);
printf("Enter 5th number: ");
scanf("%d", &e);

func(a,b,c,d,e,&sum,&avg,&std_dev);

printf("The Sum: %f\n", sum);


printf("The Average: %f\n", avg);
printf("The Standard Deviation: %f", std_dev);

void func(int a, int b, int c, int d, int e, float *sum, float


*avg, float *std_dev)
{
*sum = a+b+c+d+e;
*avg = *sum/5.0;
*std_dev = sqrt (((a-*avg)*(a-*avg))+((b-*avg)*(b-
*avg))+((c-*avg)*(c-*avg))+((d-*avg)*(d-*avg))+((e-*avg)*(e-
*avg))/5.0);

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);


void main()
{
int num;
printf("Enter an Integer: ");
scanf("%d", &num);
prime(num);
}

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:

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


Program:
#include <stdio.h>
#include <stdlib.h>

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>

void func(int a, int b, int c, float *avg, float *per);


void main()
{
int a, b, c;
float avg, per;

printf("Marks in 1st Subject: ");


scanf("%d", &a);
printf("Marks in 2st Subject: ");
scanf("%d", &b);
printf("Marks in 3st Subject: ");
scanf("%d", &c);

4 | Page CSC141Lab:08
func(a, b, c, &avg, &per);

printf("\n The Average: %.2f", avg);


printf("\n The Percentage: %.2f%%", per);
}
void func(int a, int b, int c, float *avg, float *per)
{
*avg = (a+b+c)/3.0;
*per = ((a+b+c)/300.0)*100;
}

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>

int find(int a);


int main()
{
int y,l;
printf("Enter Year: ");
scanf("%d",&y);
l=find(y);
if(l==1)
printf("%d Year is a LEAP YEAR ",y);
else
printf("%d Year is NOT a LEAP YEAR ",y);
return 0;
}
int find(int a)
{
int i=0;
if((a % 4 && a % 100 && a % 400)==0)
i=1;
else

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>

int shift(int *a, int *b, int *c);


void main()
{
int x, y, z;
printf("Enter X: ");
scanf("%d", &x);
printf("Enter Y: ");
scanf("%d", &y);
printf("Enter Z: ");
scanf("%d", &z);
printf("Before Shift:\tX: %d\tY: %d\tZ: %d",x,y,z);

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

You might also like