l2 - Function Presentation
l2 - Function Presentation
#include<stdio.h>
#include<conio.h>
void main()
{
int yr;
clrscr();
printf(“Enter the year:”);
scanf(“%d”,&yr);
if((yr%400==0)||((yr%100!=0)&&(yr%4==0)))
printf(“given year is a leap year=%d”,yr);
else
printf(“given year is not a leap year=%d”,yr);
getch();
}
/*WAP to check whether a given year is leap year or not
using conditional or ternary operator*/
#include<stdio.h>
#include<conio.h>
void main()
{
int yr;
clrscr();
printf(“Enter the year:”);
scanf(“%d”,&yr);
((yr%400==0)||((yr%100!=0)&&(yr%4==0)))?(printf(“given
year is a leap year=%d”,yr)):(printf(“given year is not a leap
year=%d”,yr));
getch();
}
/*Enter a number from keyboard, and then pick up each digit
and add them up i.e n=153 then result will be 1+5+3=9*/
#include<stdio.h>
#include<conio.h>
void main()
{
int n,m,s=0;
clrscr();
printf(“Enter the number:”);
scanf(“%d”,&n);
while(n>0)
{
m=n%10;
s=s+m;
n=n/10;
}
printf(“sum=%d”,s);
getch();
}
/* assume n=153
i.e, 153 >0 always true control passes to the 1st line inside the
loop
m=153%10; means it stores remainder to m i.e 3
s=s+m; initially s=0 so,s=0+3;
n=n/10; as both are integer so after division n=153/10=15
Again 15>0, condition satisfied control passes to the 1st line
inside the loop
m=15%10; means it stores remainder to m i.e 5
s=s+m; s=3so,s=3+5;
n=n/10; as both are integer so after division n=15/10=1
Again 1>0, condition satisfied control passes to the 1st line
inside the loop
m=1%10; means it stores remainder to m i.e 1
s=s+m; s=8so,s=8+1;
n=n/10; as both are integer so after division n=1/10=0
Now 0>0 not satisfied,control passes outside of the loop*/
/*WAP to find the factorial of a given number
5!=5*4*3*2*1=125 */
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,f=1;
clrscr();
printf(“Enter the number:”);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
f=f*i;
printf(“factorial=%d”,f);
getch();
}
Assume that , n=5
Now, within for loop i=1 and i<=n condition satisfied, so
f=f*i will be executed i.e initial value of f=1
f=1*1=1. ‘i’ is incremented by 1 and becomes 2.Again
2<=n, condition satisfied ,so f=f*i will be executed i.e
modified value of f=1
f=1*2=2. ‘i’ is incremented by 1 and becomes 3. 2.Again
3<=n, condition satisfied ,so f=f*i will be executed i.e
modified value of f=2
f=2*3=6…this process will be continue untill the
condition becomes false i.e when the value of ‘i’ =6
condition not satisfied and control passes to the next
statement outside of the loop
/*WAP to evaluate the following series and store the
result into a variable and prints the result. Ex. If we
take x=1 and n=3 i.e upto 3rd term then result =3*/
#include<stdio.h>
#include<conio.h>
void main()
{
int x,r=0,n,i,t;
clrscr();
printf(“Enter the value of n i.e upto which term you want
to calculate:”);
scanf(“%d”,&n);
printf(“Enter the value of x”);
scanf(“%d”,&x);
t=x;
for(i=1;i<=n;i++)
{
r=r+t;
t*=t;
}
printf(“value=%d\n”,r);
getch();
}
Here ‘n’ is indicate the number of terms say 4 and ‘x’
is indicate any value say 2. so the series will be
case 4:exit(0);
}
getch();
}
Odd loop: when it is not known before hand how many
times body of the loop are to be executed,then the loop is
known as odd loop.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i;
do
{
printf(“enter the value of n ”);
scanf(“%d”,&n);
printf(“remainder=%d”,n%2);
printf(“Want to continue 0/1”);
scanf(“%d”,&i);
}while(i==1);
}
/*WAP to find /check whether a given number is palindrome or not*/
#include<stdio.h>
#include<conio.h>
void main()
{
int n,r,s=0,t;
printf(“enter the value of n ”);
scanf(“%d”,&n);
while(n>0)
{
r=n%10;
s=s*10+r;
n=n/10;
}
if(t==s)
printf(“palindrome=%d”,s);
else
printf(“not palindrome=%d”,s);
getch();
}
A palindrome number is a number that remains same
when its digits are reversed.
assume that n =121
So, 121>0 condition satisfied. Inside the while loop we
1st take the remainder and store it to r i.e 1.then
perform s=s*10+r. as s=0 initially so s=1.
n=n/10 so after division n=12, again condition satisfied
now r=2 after 12%10
S=s*10+r=1*10+2=12…..continue this process untill n
becomes zero
/*WAP to find /check whether a given number is amstrong or not*/
#include<stdio.h>
#include<conio.h>
void main()
{
int n,r,s=0,t;
printf(“enter the value of n ”);
scanf(“%d”,&n);
t=n;
while(n>0)
{
r=n%10;
s=s+r*r*r;
n=n/10;
}
if(t==s)
printf(“palindrome=%d”,s);
else
printf(“not palindrome=%d”,s);
getch();
}
Amstrong number : The sum of cubes of each digit is
equal to the number itself.
153=
assume that n =153
So, 153>0 condition satisfied. Inside the while loop we
1st take the remainder and store it to r i.e 3.then
perform s=s+r*r*r. as s=0 initially so s=27.
n=n/10 so after division n=15, again condition satisfied
now r=5 after 15%10
S=s+r*r*r=27+5*5*5=152…..continue this process untill
n becomes zero
/*WAP to find /check whether a given number is prime or not*/
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i=2;
printf(“enter the value of n ”);
scanf(“%d”,&n);
while(n>i)
{
if(n%i==0)
{
printf(“not prime=%d”,n);
getch();
break;
}
else
i++;
}
if(i==n)
printf(“prime=%d”,n);
getch();
}
/*WAP to print fibonacci series 0,1,1,2,3,5,8,13…….*/
#include<stdio.h>
#include<conio.h>
void main()
{
int n,a=0,b=1,c;
printf(“enter the value of n ”);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
{
printf(“%d”,a);
c=a+b;
a=b;
b=c;
}
getch();
}
Function:A function is a self-contained block of
statement that perform task of some kind.
Function elements:
1.Function declaration
2. Function definition
3.Function call
1.Function declaration: consists of return type, function
name and argument list.
2. Function definition: is an independent program module
that is specially written to implement the requirements
of the program.
3.Function call: In order to use function we need to
invoke it at required place in the program.
There are basically two types of function those are
1. Library function:System defined function can’t be
modified, it can only read and can be used. These
function are supplied with every C compiler
Source of these library function are pre complied and
only object code get used by the user by linking to the
code by linker
printf(),scanf()
2.User defined function:The user defined functions
defined by the user according to its requirement
fact(), func()
/*WAP to calculate the addition of two integer*/
#include<stdio.h>
#include<conio.h>
int func(int,int); /*function declaration/ prototype */
void main()
{
int a,b,c;
printf(“Enter the value of a & b”);
scanf(“%d%d”,&a,&b);
c=func(a,b); //function call
printf(“sum=%d”,c);
}
int func(int x,int y) //function definition
{
return(x+y);
}
We call the user defined function named ‘func’ from
‘main()’, so ‘main()’ is calling function and ‘func()’
is called function.
Actual argument: the data or arguments which are
sent by calling function.
Prev. program. a,b
Formal argument: the argument list of called
function that will receive the data sent by calling
function.
x,y
The type, order and number of actual and formal
arguments must always be same.
Category of functions: A function ,depending on
whether arguments are present or not ,whether value
is returned or not, may be belongs to one of the four
categories.
Function with no arguments and no return
value
#include<stdio.h>
void message();
void main()
{
printf(“inside main”);
message();
}
void message()
{
printf(“outside main”);
}
Function with arguments and no return value
#include<stdio.h>
#include<conio.h>
void swap(int,int);
void main()
{
int a,b;
printf(“enter the value of a,b”);
scanf(“%d%d”,&a,&b);
swap(a,b);
getch();
}
void swap(int x,int y)
{
int t;
t=x;
x=y;
y=t;
printf(“%d%d”,x,y);
}
Function with arguments and return value
#include<stdio.h>
int fact(int);
void main()
{
int n,c;
printf(“enter the value of n”);
scanf(“%d”,&n);
c=fact(n);
printf(“%d”,c);
}
int fact(int x)
{
int i,f=1;
for(i=1;i<=x;i++)
f=f*i;
return(f);
}
Function with no arguments and return value
#include<stdio.h>
int getnum();
void main()
{
int m;
m=getnum();
printf(“%d”,m);
}
int getnum()
{
int n;
scanf(“%d”,&n);
return(n);
}
Pointer: is a variable which stores the address of
another variable.
int i=3;
i
3 int *p,i=3; ‘&’ -“address of “operator
65520 p=&i; ‘*’-”value at address”
int *j-j is pointer that points to an integer
char *c-c is pointer that points to a character.
i.e a pointer always be an integer as it holds the
address.
int **k; k-is a pointer that’s points to an integer pointer.
void main()
{ i j k
int i=3,*j,**k;
65518 65522
j=&i 3