You are on page 1of 6

Unit 5

Functions
PROGRAMMING PRACTISE
_______________________________________________________________

// Reverse of two number using function


#include<stdio.h>
int reverseTwo();
main()
{
int rev= reverseTwo();
printf("rev is %d\n",rev);
}

int reverseTwo()
{
int n,rev,rem,d;
printf("\nEnter any two digit number only\n");
scanf("%2d", &n);
rem=n%10;
d=n/10;
rev=rem*10+d;
return rev;
}
// even odd using function with argument
#include<stdio.h>
#include<conio.h>

void evenodd(int);

int main()
{
int no;
printf("enter any number\n");
scanf("%d",&no);
evenodd(no);
}
void evenodd(int n)
{
int i;
for(i=1;i<=n;i++)
{
if(i%2 == 0)
{
printf("%d is even number\n",i);
}
else
{
printf("\n\t%d is odd number\n",i);
}
}
getch();
}

WAP to print factorial of a number using recursion


#include <stdio.h>
int fact(int n);
int main()
{
int n;
printf("Enter a positive integer: ");
scanf("%d", &n);
printf("Factorial of %d = %d", n, fact(n));
return 0;
}
int fact(int n)
{
if (n >= 1)
return n*fact(n-1);
else
return 1;
}
// Check a number is divisible by three
#include<stdio.h>
#include<conio.h>

void divisible3();

int main()
{
divisible3();

getch();
}
void divisible3()
{
int no; int i;
printf("enter any number\n");
scanf("%d",&no);
for(i=1;i<no;i++)
{
if(i%3 == 0)
{
printf("%d is divisible by three\n",i);
}
}
}

//command line argument


#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]) // command line arguments


{
if(argc!=5)
{
printf("Arguments passed through command line " \
"not equal to 5");
return 1;
}

printf("\n Program name : %s \n", argv[0]);


printf("1st arg : %s \n", argv[1]);
printf("2nd arg : %s \n", argv[2]);
printf("3rd arg : %s \n", argv[3]);
printf("4th arg : %s \n", argv[4]);

return 0;
}
// command line argument
#include <stdio.h>
#include <conio.h>

int main(int argc, char *argv[])


{
int i;
for(i = 0; i < argc; i++)
{

printf("%s\t", argv[i]);
}
return 0;
getch();
}

// convert a number to binary


#include<stdio.h>
#include<conio.h>
void binary();
int main()
{
binary();

}
void binary()
{
int no=0,rem=0,arr[5];
int i=0,j=0;
printf("Enter number\n");
scanf("%d",&no);
while(no !=0 )
{
rem=no % 2;
arr[i]=rem;
i++;
no= no /2;
}
i--;
for(j=i;j>=0;j--)
{
printf("%d",arr[j]);
}
getch();
}
// count positive, negative and zero in array using function
#include<stdio.h>
void countNumber();
void main()
{
countNumber();
}
void countNumber()
{
int m[5]={2,-8,0,-7,8};
int cntp=0,cntn=0,cntz=0;
int i;

for(i=0;i<5;i++)
{
if(m[i]>0)
cntp++;
else if(m[i]<0)
cntn++;
else
cntz++;
}
printf("Positive Count is %d \n",cntp);
printf("Negative is %d \n",cntn);
printf("zero count is %d \n",cntz);

// addition using cmd line arg


#include<stdio.h>
#include<stdlib.h>
int main(int argc, char *argv[])
{
int a,b,sum;
if(argc!=3)
{
printf("please use \"prg_name value1 value2 \"\n");
return -1;
}

a = atoi(argv[1]);
b = atoi(argv[2]);
sum = a+b;

printf("Sum of %d, %d is: %d\n",a,b,sum);

return 0;
}
//Develop a program to find diameter, circumference and area of
circle using function.
#include<stdio.h>
#include<conio.h>
void circle(float r)
{
float diameter,circumference,area;
diameter=2*r;
printf("\n Diameter=%f",diameter);
circumference=2*3.14*r;
printf("\n Circumference=%f",circumference);
area=3.14*r*r;
printf("\n Area=%f",area);
}
void main()
{
float radius;
clrscr();
printf("\n Enter radius:");
scanf("%f",&radius);
circle(radius);
getch();
}

Write a function to print Fibonacci series starting from 0, 1.


#include<stdio.h>
Void main()
{
Fibbo();
}
void Fibbo()
{
inta,b,c,limit,i;
printf("\n Enter number:");
scanf("%d",&limit);
a=0;
b=1;
printf("%d\t%d",a,b);
for(i=0;i<limit-2;i++)
{
c=a+b;
printf("\t%d",c);
a=b;
b=c;
}
}

You might also like