Function And String Management Programs
Prepared By Hitesh Vataliya
For More Details : Click Here
1. Function and types of functions.
#include<stdio.h>
#include<conio.h>
// Function without any argument and without return type.
void funsum()
{
int a,b,sum=0;
a=11;
b=10;
sum=a+b;
printf("\n\nFunsum () The addition is %d",sum);
}
// Function with two arguments and without return type.
void fun2(int x,int y)
{
int sum=x+y;
printf("\n\nfun2() The sum is %d",sum);
}
// Function with two arguments and with return type.
int fun3(int n1,int n2)
{
return n1+n2;
}
void main()
{
int s;
clrscr();
funsum(); // call of function.
fun2(10,11);
s=fun3(10,11);
printf("\n\nfun3() The sum is %d",s);
getch();
}
2. Create a program for calculator with help of functions for add,sub,mul,div.
#include<stdio.h>
#include<conio,h>
int funadd(int n1,int n2)
{
return n1+n2;
}
int funsub(int n1,int n2)
{
return n1-n2;
}
int funmul(int n1,int n2)
{
return n1*n2;
}
float fundiv(int n1,int n2)
{
return ((float)n1)/n2;
}
void main()
{
int ans,a,b;
float d;
clrscr();
printf("Enter 1 for addition \n\t Enter 2 for substraction \n\t Enter 3 for multiplication \n\t Enter 4 for division \n\t Enter you choise : ");
scanf("%d",&c);
switch(c)
{
case 1: ans=funsum(a,b);
printf("The addition is %d",ans);
break;
case 2: ans=funsub(a,b);
printf("The substraction is %d",ans);
break;
case 3: ans=funmul(a,b);
printf("The multiplication is %d",ans);
break;
case 4: d=fundiv(a,b);
printf("The division is %f",d);
break;
default: printf("Invalid Choise");
break;
}
getch();
}
3. Write a program to find factorial of given number using recursive function.
#include<stdio.h>
#include<conio.h>
//function calls itself is known as recursion.
int fun1(int n); //factorial using recursion
void main()
{ int ans;
clrscr();
ans=fun1(5);
printf("\n\n\t\tThe factorial of 5 is %d",ans);
getch();
}
int fun1(int n)
{
if(n==0||n==1)
{
return 1;
}
else
{
return n*fun1(n-1);
}
}
4. Write a program to get fibonacci series as output using recursion.
#include<stdio.h>
#include<conio.h>
void fib(int a,int b,int n)
{
int c;
if(n>0)
{
c=a+b;
a=b;
b=c;
printf("%d ",c);
fib(a,b,n-1);
}
}
void main()
{
clrscr();
printf("\n\n\t\t0 1 ");
fib(0,1,6);
getch();
}
5. Write a program to find occurrence of character's from given string.
if name is "aakash"
then output will be
char occurrence
a 3
h 1
k 1
s 1
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char name[]="Software Programming";
int count[26]={0};
char k[26];
int l,i,j;
clrscr();
l=strlen(name);
for(i=0;i<26;i++)
{
k[i]=i+65;
}
strupr(name); // used to convert name to upper case
for(i=0;i<l;i++)
{
for(j=0;j<26;j++)
{
if(k[j]==name[i])
{
count[j]++;
break;
}
}
}
printf("\n\n\t\tCharacter\t\tOccurances\n");
for(j=0;j<26;j++)
{
if(count[j]>0)
{
printf("\n\t\t%c\t\t\t\t%d",k[j],count[j]);
}
}
getch();
}
If you are interested for learning C Programming Language. VISIT : WEBSITE