Function Examples(No Pass and Return)

Current Set : Set 10
Set 1     Set 2     Set 3     Set 4     Set 5     Set 6     Set 7     Set 8    Set 9    Set 10

Question:3
Write a function in C language to take input for 2 numbers calculate and print their difference?
Sol:

#include <stdio.h>
//function definition
int diff()
{
	int a,b;
	printf("Enter 2 nos ");
	scanf("%d %d",&a,&b);
	if(a>b)
	return(a-b);
	else
	return(b-a);
}
int main()
{
	int d;
	d=diff();
	printf("difference = %d\n",d);
	return 0;
}

Question:4
Write a function in C language to take input for 3 numbers check and print the largest number?
Sol:

#include <stdio.h>
//function definition
int largest()
{
	int a,b,c;
	printf("Enter 3 nos ");
	scanf("%d %d %d",&a,&b,&c);
	if(a>b && a>c)
	return(a);
	else
		if(b>c)
	  	return(b);
	  	else
	  	return(c);
}
int main()
{
	int d;
	d=largest();
	printf("max no = %d\n",d);
	return 0;
}

Current Set : Set 10
Set 1     Set 2     Set 3     Set 4     Set 5     Set 6     Set 7     Set 8    Set 9    Set 10

C Language Programming Tutorial

C Language Tutorial Home     Introduction to C Language     Tokens     If Condition      goto statement and Labelname     Switch Statements     For loop     While Loop     Do while loop     break and continue     Functions     Recursion     Inbuild Functions     Storage Classes     Preprocessor     Arrays     Pointers     Structures and Unions     File Handling     Projects