C Programs
All Basic C Programs
Fibbonacci series using recursion
Program:
#include<stdio.h>
int fibbonacci(int n);
int main()
{
int i,n;
printf("Enter no. of elements: ");
scanf("%d",&n);
printf("Fibbonacci of %d:\n " , n);
for(i=0;i<n;i++)
{
printf("%d ",fibbonacci(i));
}
return 0;
}
int fibbonacci(int n)
{
if(n == 0)
{
return 0;
}
else if(n == 1)
{
return 1;
}
else
{
return (fibbonacci(n-1) + fibbonacci(n-2));
}
}
Output:
#include<stdio.h>
int fibbonacci(int n);
int main()
{
int i,n;
printf("Enter no. of elements: ");
scanf("%d",&n);
printf("Fibbonacci of %d:\n " , n);
for(i=0;i<n;i++)
{
printf("%d ",fibbonacci(i));
}
return 0;
}
int fibbonacci(int n)
{
if(n == 0)
{
return 0;
}
else if(n == 1)
{
return 1;
}
else
{
return (fibbonacci(n-1) + fibbonacci(n-2));
}
}
Reverse string using function
Program:
#include<stdio.h>
#include<string.h>
void reverse(char s[]);
int main()
{
char str[100];
printf("Enter a string: ");
scanf("%s",str);
reverse(str);
printf("%s",str);
return 0;
}
void reverse(char s[])
{
int i,j;
char temp;
for(i=0,j=strlen(s)-1;i<strlen(s)/2;i++,j--)
{
temp=s[i];
s[i]=s[j];
s[j]=temp;
}
}
Output:
#include<stdio.h>
#include<string.h>
void reverse(char s[]);
int main()
{
char str[100];
printf("Enter a string: ");
scanf("%s",str);
reverse(str);
printf("%s",str);
return 0;
}
void reverse(char s[])
{
int i,j;
char temp;
for(i=0,j=strlen(s)-1;i<strlen(s)/2;i++,j--)
{
temp=s[i];
s[i]=s[j];
s[j]=temp;
}
}
Maximum no. from array elements using function
Program:
#include<stdio.h>
int max(int m[],int n);
int main()
{
int a[10],n,i;
printf("Enter No. of elements: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Maximum: %d",max(a,n));
return 0;
}
int max(int m[],int n)
{
int max=0,i;
for(i=0;i<n;i++)
{
if(m[i]>max)
{
max=m[i];
}
}
return max;
}
Output:
#include<stdio.h>
int max(int m[],int n);
int main()
{
int a[10],n,i;
printf("Enter No. of elements: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Maximum: %d",max(a,n));
return 0;
}
int max(int m[],int n)
{
int max=0,i;
for(i=0;i<n;i++)
{
if(m[i]>max)
{
max=m[i];
}
}
return max;
}
Subscribe to:
Posts (Atom)
x^y using recursion
Program: #include<stdio.h> int power(int a,int b); int main() { int x,y,ans; printf("Enter x and y:\n "); scanf(...
-
Program: #include<stdio.h> int main() { char s1[30],s2[30]; int i=0; printf("Enter a string: "); scanf("%s...
-
// 1² + 2² +3² +.............+ n² Program: #include<stdio.h> int main() { int n,i,sum=0; printf("Enter n: "); sc...
-
Program: #include<stdio.h> int main() { int a[4][4],b[4][4],c[4][4],m,n,p,q,i,j; printf("Enter no. of rows in matrix A: ...