0% found this document useful (0 votes)
31 views

C ASSIGNMENT 3 (Functions)

This document contains 4 programming assignments submitted by Sakshat Lakhiani of section T, roll number 37 for the C programming course at GLA University. The assignments include programs to calculate the area of a triangle using Heron's formula, find the sum of even numbers from 1 to n, calculate the area of a circle, square and rectangle, and perform basic calculations like addition, subtraction, multiplication and division using different function types.

Uploaded by

Sakshat Lakhiani
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)
31 views

C ASSIGNMENT 3 (Functions)

This document contains 4 programming assignments submitted by Sakshat Lakhiani of section T, roll number 37 for the C programming course at GLA University. The assignments include programs to calculate the area of a triangle using Heron's formula, find the sum of even numbers from 1 to n, calculate the area of a circle, square and rectangle, and perform basic calculations like addition, subtraction, multiplication and division using different function types.

Uploaded by

Sakshat Lakhiani
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
You are on page 1/ 26

GLA UNIVERSITY

C - PROGRAMMING
ASSIGNMENT
(2-D ARRAY)

SUBMITTED BY-
SAKSHAT LAKHIANI
SEC- T
ROLL NO.- 37
1. Write a program to find area of triangle
using Heron’s Formula using functions with
arguments and with return type.
#include <stdio.h>
#include <math.h>
float triangle(float a,float b,float c)
{
float area,si;
s=(a+b+c)/2;
area=sqrt(s*(s-a)*(s-b)*(s-c));
return area;
}
int main()
{
float s1,s2,s3,area_of_triangle;
printf(" PROGRAM TO CALCULATE AREA OF TRIANGLE
(using herons formula)\n\n");
printf("\nEnter length of side 1: ");
scanf("%f",&s1);
printf("\nEnter length of side 2: ");
scanf("%f",&s2);
printf("\nEnter length of side 3: ");
scanf("%f",&s3);
if((s1+s2)>s3&&(s2+s3)>s1&&(s3+s1)>s2)
{
area_of_triangle=triangle(s1,s2,s3);
printf("\n\nAREA OF TRIANGLE= %.2f",area_of_triangle);
}
else
printf("\nTriangle not possible!!!");

return 0;
}

2. Write a program to find the sum of even


numbers from 1 to n using functions without
arguments and with return type.
#include <stdio.h>
int sum_of_even()
{
int n,i,sum=0;
printf("\nEnter the value of n: ");
scanf("%d",&n);
for(i=2;i<=n;i=i+2)
{
sum=sum+i;
}
return sum;
}
int main()
{
int sum;
printf(" PROGRAM TO PRINT SUM OF EVEN INTEGERS
FORM 1 TO N");
sum=sum_of_even();
printf("\nSum of EVEN integers is: %d",sum);

return 0;
}
3. Write a program to find the area of circle,
square and rectangle using functions without
arguments and with return type.
#include <stdio.h>
#define pi 3.14
float circle()
{
int r;
float area;
printf("\nEnter the radius of circle: ");
scanf("%d",&r);
area= pi*r*r;
return area;
}
int square()
{
int side,area;
printf("\nEnter the side of square: ");
scanf("%d",&side);
area=side*side;
return area;
}
int rectangle()
{
int l,b,area;
printf("\nEnter the length and breadth of rectangle: ");
scanf("%d%d",&l,&b);
area=l*b;
return area;
}
int main();
int main()
{
int choice,ar,c;
float ar1;
here:
printf("\nPROGRAM TO CALCULATE AREA:\n\n");
printf("1)CIRCLE\n2)SQUARE\n3)RECTANGLE\n");
printf("\nEnter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1: ar1=circle();
printf("\nArea of Circle= %.2f",ar1);
break;
case 2: ar=square();
printf("\nArea of square= %d",ar);
break;
case 3: ar=rectangle();
printf("\nArea of rectangle= %d",ar);
break;
default: printf("\nWRONG CHOICE");
}
printf("\n\nDo you want to continue?(1/2)\n1)YES\n2)NO\n");
scanf("%d",&c);
if(c==1)
{
goto here;
}
else
printf("\n END");
return 0;
}
4. Write a program to perform- addition,
subtraction, multiplication and division
(CALCULATOR) using functions of all types.
#include <stdio.h>
#include <conio.h>

//*************************Addition ke Functions***********************
void add1() //no return no arguments
{
printf("\n IN FUNCTION");
int a,b;
printf("\nEnter the numbers to be added: ");
scanf("%d%d",&a,&b);
printf("\nSum= %d",a+b);
printf("\nAT THE END OF FUNCTION");
}
void add2(int a,int b) //no return yes arguments(lol)
{
printf("\n IN FUNCTION");
printf("Sum=%d",a+b);
printf("\nAT THE END OF FUNCTION");
}
int add3() //yes return no arguments
{
printf("\n IN FUNCTION");
int a,b;
printf("\nEnter the numbers to be added: ");
scanf("%d%d",&a,&b);
printf("\nAT THE END OF FUNCTION");
return (a+b);
}
int add4(int a,int b) //yes return yes arguments
{
printf("\n IN FUNCTION");
int sum;
sum=a+b;
printf("\nAT THE END OF FUNCTION");
return sum;
}

//**********************Subtraction ke Functions*********************
void sub1() //no return no arguments
{
printf("\n IN FUNCTION");
int a,b;
printf("\nEnter the numbers to be subtracted: ");
scanf("%d%d",&a,&b);
if(a>b)
printf("\nDifference= %d",a-b);
else
printf("\nDifference= %d",b-a);
printf("\nAT THE END OF FUNCTION");
}
void sub2(int a,int b) //no return yes arguments(lol)
{
printf("\n IN FUNCTION");
if(b>a)
{
a=a+b;
b=a-b;
a=a-b;
}
printf("Difference= %d",a-b);
printf("\nAT THE END OF FUNCTION");
}
int sub3() //yes return no arguments
{
printf("\n IN FUNCTION");
int a,b;
printf("\nEnter the numbers to be subtracted: ");
scanf("%d%d",&a,&b);
if(b>a)
{
a=a+b;
b=a-b;
a=a-b;
}
printf("\nAT THE END OF FUNCTION");
return (a-b);
}
int sub4(int a,int b) //yes return yes arguments
{
printf("\n IN FUNCTION");
int diff;
if(b>a)
{
a=a+b;
b=a-b;
a=a-b;
}
diff=a-b;
printf("\nAT THE END OF FUNCTION");
return diff;
}

//*******************Multiplication ke Functions***********************

void mul1() //no return no arguments


{
printf("\n IN FUNCTION");
int a,b;
printf("\nEnter the numbers to be multiplied: ");
scanf("%d%d",&a,&b);
printf("\nProduct= %d",a*b);
printf("\nAT THE END OF FUNCTION");
}
void mul2(int a,int b) //no return yes arguments(lol)
{
printf("\n IN FUNCTION");
printf("Product=%d",a*b);
printf("\nAT THE END OF FUNCTION");
}
int mul3() //yes return no arguments
{
printf("\n IN FUNCTION");
int a,b;
printf("\nEnter the numbers to be multiplied: ");
scanf("%d%d",&a,&b);
printf("\nAT THE END OF FUNCTION");
return (a*b);
}
int mul4(int a,int b) //yes return yes arguments
{
printf("\n IN FUNCTION");
int product;
product=a*b;
printf("\nAT THE END OF FUNCTION");
return product;
}

//**************************Division ke Functions***********************

void divi1() //no return no arguments


{
printf("\n IN FUNCTION");
int a,b;
float d;
printf("\nEnter the numbers to be divided: ");
scanf("%d%d",&a,&b);
d=(float)a/b;
printf("\nResult after division= %.2f",d);
printf("\nAT THE END OF FUNCTION");
}
void divi2(int a,int b) //no return yes arguments(lol)
{
printf("\n IN FUNCTION");
float d=(float)a/b;
printf("\nResult after division= %.2f",d);
printf("\nAT THE END OF FUNCTION");
}
float divi3() //yes return no arguments
{
printf("\n IN FUNCTION");
int a,b;
printf("\nEnter the numbers to be divided: ");
scanf("%d%d",&a,&b);
float d=(float)a/b;
printf("\nAT THE END OF FUNCTION");
return (d);
}
float divi4(int a,int b) //yes return yes arguments
{
printf("\n IN FUNCTION");
float d=(float)a/b;
printf("\nAT THE END OF FUNCTION");
return d;
}

int menu()
{
int ch;
printf("\nWhich type of function working do you want?\n1).no
return type and no argument\n2).no return type but passing
arguments\n3).return type and no argument\n4).both Return type
and Passing arguments");
printf("\n\nEnter your choice: ");
scanf("%d",&ch);
return ch;
}

int main()
{
int num1,num2,p,c;
int choice,i;
float q;
here:
printf("\nInput your choice and get the operation performed:\n\n
1).ADDITION of two numbers\n 2).SUBTRACTION of two
numbers\n 3).MULTIPICATION of two numbers\n 4).DIVISION of
two numbers");
printf("\nEnter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\n ADDITION");
i=menu();
switch(i)
{
case 1: printf("\nYou chose no return type and no
argument:\n");
add1();
break;
case 2: printf("\nYou chose no return type but
passing arguments\n");
printf("\nEnter numbers to be added: ");
scanf("%d%d",&num1,&num2);
add2(num1,num2);
break;
case 3: printf("\nYou chose return type and no
argument(no pun intended)");
p=add3();
printf("\nSum= %d",p);
break;
case 4: printf("\nYou chose both Return type and
Passing arguments");
printf("\nEnter numbers to be added: ");
scanf("%d%d",&num1,&num2);
p=add4(num1,num2);
printf("\nSum= %d",p);
break;
default: printf("\nWrong Choice");
}
break;
case 2: printf("\n SUBTRACTION");
i=menu();
switch(i)
{
case 1: printf("\nYou chose no return type and no
argument:\n");
sub1();
break;
case 2: printf("\nYou chose no return type but
passing arguments\n");
printf("\nEnter numbers to be subtracted: ");
scanf("%d%d",&num1,&num2);
sub2(num1,num2);
break;
case 3: printf("\nYou chose return type and no
argument(no pun intended)");
p=sub3();
printf("\nDifference= %d",p);
break;
case 4: printf("\nYou chose both Return type and
Passing arguments");
printf("\nEnter numbers to be subtracted: ");
scanf("%d%d",&num1,&num2);
p=sub4(num1,num2);
printf("\nDifference= %d",p);
break;
default: printf("\nWrong Choice");
}
break;
case 3: printf("\n MULTIPICATION");
i=menu();
switch(i)
{
case 1: printf("\nYou chose no return type and no
argument:\n");
mul1();
break;
case 2: printf("\nYou chose no return type but
passing arguments\n");
printf("\nEnter numbers to be multiplied: ");
scanf("%d%d",&num1,&num2);
mul2(num1,num2);
break;
case 3: printf("\nYou chose return type and no
argument(no pun intended)");
p=mul3();
printf("\nProduct= %d",p);
break;
case 4: printf("\nYou chose both Return type and
Passing arguments");
printf("\nEnter numbers to be multiplied: ");
scanf("%d%d",&num1,&num2);
p=mul4(num1,num2);
printf("\nProduct= %d",p);
break;
default: printf("\nWrong Choice");
}
break;
case 4: printf("\n DIVISION");
i=menu();
switch(i)
{
case 1: printf("\nYou chose no return type and no
argument:\n");
divi1();
break;
case 2: printf("\nYou chose no return type but
passing arguments\n");
printf("\nEnter numbers to be divided: ");
scanf("%d%d",&num1,&num2);
divi2(num1,num2);
break;
case 3: printf("\nYou chose return type and no
argument(no pun intended)");
q=divi3();
printf("\nAnswer= %.2f",q);
break;
case 4: printf("\nYou chose both Return type and
Passing arguments");
printf("\nEnter numbers to be divided: ");
scanf("%d%d",&num1,&num2);
q=divi4(num1,num2);
printf("\nAnswer= %.2f",q);
break;
default: printf("\nWrong Choice");
}
break;
default: printf("\nWrong Choice");

}
printf("\n\nDo you want to continue?(1/2)\n1)YES\n2)NO\n");
scanf("%d",&c);
if(c==1)
{
goto here;
}
else
printf("\n END");
return 0;
}

5. Write a program to calculate the


percentage of a student using functions with
argument and with return type.
#include <stdio.h>

float percentage(float s1,float s2, float s3, float s4, float s5)
{
float per;
per=(s1+s2+s3+s4+s5)/5;
return per;
}
int main()
{
float m1,m2,m3,m4,m5,per;
printf("\nPROGRAM TO CALCULATE PERCENTAGE OF A
STUDENT");
printf("\n\nEnter the marks of 5 subjects:\n");
printf("\nSUBJECT 1: ");
scanf("%f",&m1);
printf("\nSUBJECT 2: ");
scanf("%f",&m2);
printf("\nSUBJECT 3: ");
scanf("%f",&m3);
printf("\nSUBJECT 4: ");
scanf("%f",&m4);
printf("\nSUBJECT 5: ");
scanf("%f",&m5);
per=percentage(m1,m2,m3,m4,m5);
printf("\n\nPERCENTAGE OF STUDENT= %.2f",per);
return 0;
}

6. Write a program to find X to the power N


using functions with arguments and without
return type.
#include <stdio.h>
#include <math.h>

void power(int x,int n)


{
int ans;
ans=pow(x,n);
printf("\nThe Answer is= %d",ans);
}
int main()
{
int x,n;
printf("\nPROGRAM TO FIND X TO THE POWER N\n");
printf("\nEnter the value of x: ");
scanf("%d",&x);
printf("\nEnter the value of n: ");
scanf("%d",&n);
power(x,n);

return 0;
}

You might also like