Lab 7 Report
Lab 7 Report
Subject: C Programming
Lab Report 7
Tittle: Functions
Checked by
Content of Lab Report:
Background Information
C Programming
Editor Used
Compiler
C User Defined Function
Analysis
Conclusion
Background Information
What is C Programming?
C programming is a general-purpose, procedural, imperative computer programming
language developed in 1972 by Dennis M. Ritchie at the Bell Telephone Laboratories to
develop the UNIX operating system. C is the most widely used computer language.
Why to Learn C Programming?
• Easy to learn
• Structured language
• It produces efficient programs
• It can handle low-level activities
• It can be compiled on a variety of computer platforms
Editor
Here, I have used Visual Studio Code as my editor. You can download the editor from
Download Visual Studio Code - Mac, Linux, Windows . Select your operating system
and download it.
Compiler
Here, I have used gcc as my compiler provided by MinGWw64. You can download it via
Download MinGW-w64 - for 32 and 64 bit Windows from SourceForge.net. Your
download will start automatically. Run the downloaded .exe file. After, you have
installed MinGW-w64, you need to configure it.
• In the Windows search bar, type 'settings' to open your Windows Settings.
• Search for Edit environment variables for your account.
• Choose the Path variable and then select Edit.
• Select New and add the Mingw-w64 destination folder path to the system
path. The exact path depends on which version of Mingw-w64 you have
installed and where you installed it. If you used the settings above to install
Mingw-w64, then add this to the path: C:\Program Files\mingw-
w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin.
• Select OK to save the updated PATH. You will need to reopen any console
windows for the new PATH location to be available.
Check your installation
Open command prompt or power shell and type:
Function prototype
A function prototype is simply the declaration of a function that specifies function's
name, parameters and return type. It doesn't contain function body.
A function prototype gives information to the compiler that the function may later be
used in the program.
Syntax of function prototype
returnType functionName(type1 argument1, type2 argument2, ...);
The function prototype is not needed if the user-defined function is defined before the
main() function.
Calling a function
Control of the program is transferred to the user-defined function by calling it.
Syntax of function call
functionName(argument1, argument2, ...);
Function definition
Function definition contains the block of code to perform a specific task. In our example,
adding two numbers and returning it.
Syntax of function definition
returnType functionName(type1 argument1, type2 argument2, ...){ //body of the
function}
When a function is called, the control of the program is transferred to the function
definition. And, the compiler starts executing the codes inside the body of a function.
1.Write a program to create a function float add(int ,float). The task of this function
is to calculate the sum of passed value and return it to the calling function. Call this
function from main() and display result
Source Code:
/*Header Files*/
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
/*Function defination*/
float add(int, float);
int main()
{
/*Variable Declaration*/
int number_one;
float number_two, sum;
/*Taking input*/
printf("Number one:");
scanf("%d", &number_one);
printf("Number two:");
scanf("%f", &number_two);
/*Function call*/
sum = add(number_one, number_two);
/*Function body*/
float add(int a, float b)
{
return (float)a + b;
}
Output:
/*Function Defination*/
int sumOfDigits(int);
int main()
{
/*Varaible Declaration*/
int number, sum;
/*Taking input*/
printf("Number: ");
scanf("%d", &number);
sum = sumOfDigits(number);
getch();
return 0;
}
/*Function Body*/
int sumOfDigits(int a)
{
if (a < 0)
{
return 0;
}
else
{
while (a != 0)
{
3.Write a program to read a non negative integer in main(). Pass this integer to a
function fact() having return type unsigned integer. The function calculate the
factorial of the received number and return to main() to display it.
Source Code:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
/*Taking input*/
printf("Number : ");
scanf("%u", &number);
getch();
return 0;
}
Output:
4.Write a program to check a function void check_prime(): The Task of this program
is to read a number and check whether the number is prime or not and display the
appropriate message. Be sure that a real number cannot be either prime or
composite.What about negative number
Source Code:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
/*Function*/
void check_prime(int a)
{
for (int i = 2; i < a; i++)
{
if (a % i == 0)
{
printf("\nIt is compostite number");
exit(0);
}
}
printf("\nIt is prime number");
}
int main()
{
/*Variable Declaration*/
int number;
system("cls");/*clear the screen*/
/*Taking input*/
printf("Number: ");
scanf("%d", &number);
check_prime(number);/*Function call*/
getch();
return 0;
}
Output:
5.Combine Question 1 2 3 4 using switch statement. For this display a menu on the
screen to prompt user whether he wants to sum two number or sumof digits of an
integer or calculate factorial of an integer or to know whether number is prime or not
Source Code:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int sumOfDigits(int a)
{
if (a < 0)
{
return 0;
}
else
{
while (a != 0)
{
void check_prime(int a)
{
for (int i = 2; i < a; i++)
{
if (a % i == 0)
{
printf("\nIt is compostite number");
exit(0);
}
}
printf("\nIt is prime number");
}
/*Function to display Option*/
void menu()
{
printf("<--------------Menu-------------->\n");
printf("%5s%s\n", "", "1.Sum of Numbers");
printf("%5s%s\n", "", "2.Sum of Digits");
printf("%5s%s\n", "", "3.Factorial");
printf("%5s%s\n", "", "4.Check Prime or Composite");
}
int main()
{
/*Variable Declaration*/
int one_number_one;
float one_number_two, one_sum;
int four_number;
int choice;
/*Taking input*/
printf("Choice: ");
scanf("%d", &choice);
/*Simple switch case and in every case code copy from Program 1,2,3,4*/
switch (choice)
{
case 1:
/*Taking input*/
printf("Number one:");
scanf("%d", &one_number_one);
printf("Number two:");
scanf("%f", &one_number_two);
/*Function call*/
one_sum = add(one_number_one, one_number_two);
case 2:
system("cls");
printf("Number: ");
scanf("%d", &two_number);
two_sum = sumOfDigits(two_number);
printf("Sum of digits in %d is %d", two_number, two_sum);
case 3:
system("cls");
printf("Number : ");
scanf("%u", &three_number);
three_factorial = fact(three_number);
break;
case 4:
system("cls");
printf("Number: ");
scanf("%d", &four_number);
check_prime(four_number);
default:
printf("You didn't choose right option.");
break;
}
getch();
return 0;
}
Output:
6.Write a program to read an unsigned integer in main() pass it to function.(void
countsDigits(int*,int *);). This function counts the number of odd and even digits in
it. Display the count from main. Use concept of passing argument by refrence
Source Code:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
/*Function definition*/
void countsDigits(int *, int *);
int main()
{
/*Variable declaration*/
int digits, even_count = 0, odd_count = 0;
/*Taking input*/
printf("Number: ");
scanf("%d", &digits);
even_count = digits;
/*Function call*/
countsDigits(&even_count, &odd_count);
/*Meaningful result*/
printf("\nEven Digits: %d\n", even_count);
printf("Odd Digits: %d", odd_count);
getch();
return 0;
}
/*Function Body*/
void countsDigits(int *e, int *o)
{
int number = *e;
*e = 0;
int even = 0, odd = 0;
while (number != 0)
{
if (number % 2 == 0)
{
even++;
}
else if (number % 2 == 1)
{
odd++;
}
number /= 10;
}
*e = even;
*o = odd;
}
Output:
/*Function definition*/
int findLowest(int, int, int);
int findHighest(int, int, int);
int main()
{
/*Variable Declaration*/
int number_one, number_two, number_three;
/*Taking Input*/
printf("Number One: ");
scanf("%d", &number_one);
printf("Number Two: ");
scanf("%d", &number_two);
printf("Number Three: ");
scanf("%d", &number_three);
getch();
return 0;
}
/*Function body*/
int findLowest(int a, int b, int c)
{
int low;
(a < b && a < c) ? ((low = a)) : ((b < c) ? ((low = b)) : ((low = c)));
return low;
}
int findHighest(int a, int b, int c)
{
int high;
(a > b && a > c) ? ((high = a)) : ((b > c) ? ((high = b)) : ((high = c)));
return high;
}
Output:
/*Printing result*/
(number < 0) ? ((printf("Invalid number."))) : (printf("Factorial of number is %d",
factorial(number)));
getch();
return 0;
}
Output:
8.2Compute x^n
Source Code:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int main()
{
/*Variable declaration*/
int x, n;
/*Meaningful Output*/
printf("\nValue of %d^%d is %d", x, n, powerOfBase(x, n));
getch();
return 0;
}
Output:
8.3HCF of number
Source Code:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
/*Below if just swap the value of a and b.For more clarity, once look
Swap_two_numbers.c*/
if (a < b)
{
b = a + b - (b = a);
}
/*Declaration of variable*/
int quotient;
int remainder;
/*Explained Above*/
quotient = a / b;
remainder = a - quotient * b;
/*Calling to it self*/
HCF(b, remainder);
}
int main()
{
/*Variable declaration*/
int a, b;
/*Taking input*/
printf("Number One: ");
scanf("%d", &a);
printf("Number Two: ");
scanf("%d", &b);
/*Meaningful result*/
printf("HCF of %d and %d is %d", a, b, HCF(a, b));
getch();
return 0;
}
Output:
8.4Sum from 1 to n
Source Code:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
/*Fucntion declaration*/
int sum(int n)
{
if (n == 0)
{
return 0;
}
else
{
return n + sum(n - 1);
}
}
int main()
{
/*Varaible declaration*/
int n;
/*Meaningful output*/
printf("\nSum from 1 to %d is %d.", n, sum(n));
getch();
return 0;
}
Output:
9.Write a program using recursive function to compute series 1^2-2^2 +3^2
..................(-1)^(n+1).n^2
You cannot use pow function
Source Code:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
/*Function declaration*/
float series(int n)
{
if (n == 1)
{
return 1;
}
else
{
return (n % 2 == 1) ? (n * n + series(n - 1)) : (-1 * n * n + series(n - 1));
}
}
int main()
{
/*Variable declaration*/
int n;
/*Output*/
printf("\nValue of series is %f.", series(n));
getch();
return 0;
}
Output:
Analysis:
Here we learn about declaring function in C language how to user it. Along with declaration
of function we also came to know the working nature of function. We also came how does
pass by value and pass by reference works.
Conclusion
Here we learn to work with function.