0% found this document useful (0 votes)
19 views35 pages

l2 - Function Presentation

The document contains multiple C programming examples demonstrating various concepts such as checking for leap years, calculating factorials, summing digits, and evaluating series. It also covers control structures like loops and switch statements, as well as function definitions and pointers. Additionally, it explains the difference between call by value and call by reference in function calls.

Uploaded by

bssphoenixphp
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)
19 views35 pages

l2 - Function Presentation

The document contains multiple C programming examples demonstrating various concepts such as checking for leap years, calculating factorials, summing digits, and evaluating series. It also covers control structures like loops and switch statements, as well as function definitions and pointers. Additionally, it explains the difference between call by value and call by reference in function calls.

Uploaded by

bssphoenixphp
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/ 35

/*WAP to check whether a given year is leap year or not*/

#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

Now we have to find the result. We took ‘t’ as temporary


variable and store 2 to it. Within for loop i=1,i<=4
condition satisfied, so r=r+t, t*=t both statement will
be executed.
initially r=0 so, value of r=0+2=2 and t=x*x=2*2=4
‘i’ is incremented by 1 becomes 2 ,2<=4condition
satisfied , so r=r+t, t*=t both statement will be
executed.
so, value of r=2+4=6 and t=x*x=4*2=8…..
 Switch statement: C built-in multi-way decision statement known
as switch. The switch statement test the value of given variable
against the list of case values and when match is found, a block
associated with the case is executed.
Syntax:
switch(constant expression either integer or character)
{
case value1: statement1;
break;
case value2: statement2;
break;
.
.
.
.
.
.
default: statement:
break;
}
/* WAP a menu driven program which has following options
Addition
Subtraction
Multiplication
Exit.
*/
#include<stdio.h>
#include<conio.h>
#include<process.h>
void main()
{
int i,a,b,c;
while(1)
{
printf(“Enter the number 1. For addition \n2.for
subtraction\n 3.for multiplication\n 4. For exit”);
scanf(“%d”,&i);
switch(i)
{
case 1: printf(“Enter the value of a and b”);
scanf(“%d%d”,&a ,&b);
c=a+b;
printf(“c=%d”,c);
break;

case 2: printf(“Enter the value of a and b”);


scanf(“%d%d”,&a ,&b);
c=a-b;
printf(“c=%d”,c);
break;
case 3: printf(“Enter the value of a and b”);
scanf(“%d%d”,&a ,&b);
c=a*b;
printf(“c=%d”,c);
break;

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

k=&j; 65518 65522 65524


printf(“%d”,i);
printf(“%u”,&i);
printf(“%d”,*j);
printf(“%u”,j);
printf(“%u”,&j);
printf(“%d”,**k);
printf(“%u”,k);
printf(“%u”,&k);
printf(“%u”,*(&k));
getch();
}
 Call by value: when we pass the value of variable as arguments.
#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);
}
 Call by reference: when we pass the address of variable rather than value,
that is known as call by reference.
#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);
}
 Difference between call by value and call by reference
Call by value
In the call by value copy of the actual argument is passed to
the formal argument and the operation is done on formal
argument.
When the function is called by ‘call by value’ method, it
doesn’t affect content of the actual argument.
Separate memory location is allocated for actual and formal
parameters
Call by reference
Instead of passing the value of variable, address or reference
is passed and the function operate on address of the
variable rather than value.
Here formal argument is alter to the actual argument, it
means formal arguments calls the actual arguments
Same memory location is allocated for actual and formal
parameters
 Recursion: When a function call itself.
#include<stdio.h>
#include<conio.h>
int fact(int);
void main()
{
int n,x;
printf(“Enter the value of x”);
scanf(“%d”,&x);
n=fact(x);
printf(“factorial=%d”,n);
getch();
}
int fact(int r)
{
if(r==0||r==1)
return 1;
else
return(r*fact(r-1));
}
/* assume x=5, when we call function fact(),’r’ holds the
same value as x i.e 5.Now initially ‘r’is neither 0 nor 1
so control passes to the else block and
return(5*fact(4)) where fact(4) returns(4*fact(3))…..
*/

You might also like