0% found this document useful (0 votes)
271 views15 pages

Fibonacci Sequence Generator Program

The document contains C program code snippets for various functions: 1. A function to calculate the factorial of a number using recursion. 2. A function to generate the Fibonacci sequence using recursion. 3. A function to calculate the roots of a quadratic equation. 4. Functions demonstrating different prototypes - with and without parameters and return values. 5. Additional functions for sorting arrays, matrix multiplication, string operations etc.

Uploaded by

kamran_sk07
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
271 views15 pages

Fibonacci Sequence Generator Program

The document contains C program code snippets for various functions: 1. A function to calculate the factorial of a number using recursion. 2. A function to generate the Fibonacci sequence using recursion. 3. A function to calculate the roots of a quadratic equation. 4. Functions demonstrating different prototypes - with and without parameters and return values. 5. Additional functions for sorting arrays, matrix multiplication, string operations etc.

Uploaded by

kamran_sk07
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

19)program to find the factorial of a given number

#include<stdio.h>
int fact(int n)
{
if((n==1)||(n==0))
return(1) ;
else
return(n*fact(n-1));
}
main()
{
int x;
printf("enter a no");
scanf("%d",&x);
printf("factorial =%d",fact(x));
getch();
}

1.b)program to generate fibbanaci using recursive function


#include<stdio.h>
#include<conio.h>
void fibo(int a,int b);
main()
{
int a,b,fn;
a=0;
b=1;
clrscr();
printf("\nfibannaci series\n");
printf("%d\t%d",a,b);
fibo(a,b);
getch();
}
void fibo(int a,int b)
{
int fn;
fn=a+b;
if(fn<50)
{
printf("\t%d",fn);
a=b;
b=fn;
fibo(a,b);
}
}

20)program to find the roots of a given quadratic expression

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int a,b,c,z;
float d,x1,x2;
clrscr();
printf("\nenter the values a,b,c");
scanf("%d%d%d",&a,&b,&c);
d=(b*b)-(4*a*c);
if(d>0)
z=1;
else if(d==0)
z=2;
else
z=3;
switch(z)
{
case 1:
printf("\nroots are real and unequal\n");
x1=((-b+sqrt(d))/(2*a));
x2=((-b-sqrt(d))/(2*a));
printf("the first root is %f",x1);
printf("\nthe second root is %f",x2);
break;
case 2:
printf("\n the roots are real and equal\n");
x1=((-b+sqrt(d))/(2*a));
x2=((-b-sqrt(d))/(2*a));
printf("the first rot is %f",x1);
printf("\n the second root is %f",x2);
break;
case 3:
printf("\n the roots are imagainary");
break;
}
getch();
}

21) write a c program to demonstrate the following


i)function which returns a value having parameters
ii)function which does not return a value having no parameters
iii)function which does not return a values having parameters
iv)function which return a value having no parameters
#include<stdio.h>
#include<conio.h>
void value();
int add(int ,int);
int add1();
void sort(int [],int);
main()
{
int a,i,m[5],x,y,z,d;
clrscr();
printf("1.with out parameteres and with out return values \n ");
printf("2.with return values having parameteres\n");
printf("3.with parameteres having no return values\n");
printf("4.with return values having no parameteres\n");
printf("enter ur choice");
scanf("%d",&a);
switch(a)
{
case 1:
{
printline();
printf("\n");
value();
printf("\n");
printline();
break;
}
case 2:
{
printf("enter two values");
scanf("%d",&x);
scanf("%d",&y);
z=add(x,y);
printf("the sum of two numbers is %d",z);
break;
}
case 3:
{
printf("enter 5 v alues for ma array");
for(i=0;i<5;i++)
scanf("%d",&m[i]);
printf("values before sorting are\n");
for(i=0;i<5;i++)
printf("%d\n",m[i]);
sort(m,5);
break;
}
case 4:
{
d=add1();
printf("the sum of three numbers \t%d",d);
break;
}

default:printf("invalid choice");
break;
}

getch();
}
void value()
{
int y,n;
float r,sum,p,si;
printf("enter the principal amount");
scanf("%f",&p);
printf("enter the rate of interest");
scanf("%f",&r);
printf("enter the number of years");
scanf("%d",&n);
si=(p*n*r/100);
sum=p+si;
printf("the sum is %f",sum);
}
printline()
{
int i;
i=1;
for(i=0;i<=35;i++)
printf("-");
}
int add(int x ,int y)
{
return(x+y);
}

int add1()
{
int a,b,c,d;
printf("enter three values for a,b,c");
scanf("%d",&a);
scanf("%d",&b);
scanf("%d",&c);
d=a+b+c;
return(d);
}
void sort(int a[],int n)
{
int i,j,t;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1;j++)
{
if(a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
printf("values after sorting");
for(i=0;i<n;i++)
printf("\n%d",a[i]);

22)program to fin out largest and smallest value in an array


#include<stdio.h>
#include<conio.h>
main()
{
int n,i,a[10],j,temp;
clrscr();
printf("\n enter the size of array");
scanf("%d",&n);
if(n>10)
printf("enter size less than 10");
else
{
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("the elements of the array are\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if(a[j]>a[j+1])
{
temp= a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("\nthe smallest element is \t%d",a[0]);
printf("\nthe biggest element is \t%d",a[n-1]);
}
getch();
}
23)i)program for bubble sort
ii)program to find selection sort
i)
#include<stdio.h>
#include<conio.h>
main()
{
int n,i,a[10],j,temp;
clrscr();
printf("\n enter the size of array");
scanf("%d",&n);
if(n>10)
printf("enter size less than 10");
else
{
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("the elements of the array are\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1;j++)
{
if(a[j]>a[j+1])
{
temp= a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("\nthe elements after sorting\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
}
getch();
}
ii)#include"stdio.h"
#include"conio.h"
void main()
{
int k,i,a[10],j,temp,n,min,pos,swap;
clrscr();
printf("enter the array size");
scanf("%d",&n);
printf("\nenter the elements into the array");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for (i = 0; i < n-1; i++)
{
min = i;
for (j = (i+1); j < n; j++)
{
if(a[j] < a[min])
{
min = j;
}
}
if (i != min) {
swap = a[i];
a[i] = a[min];
a[min] = swap;
}
}
printf("\n\nafter the complementation sort the array\n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
getch();
}

24)program to find multiplicate two matrices

#include"stdio.h"
#include"conio.h"
main()
{
int a[3][3],i,j,b[3][3],c[3][3],k,l,m,n,p,q;
clrscr();
printf("enter the the order of matrix a :");
scanf("%d%d",&m,&n);
printf("enter the order of second matrix b:");
scanf("%d%d",&p,&q);
if(n!=p)
printf("matrix multiplication not possible");
else
{
printf("\n elements of the array a");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
}
printf("\nenter the elements for the second array");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
scanf("%d",&b[i][j]);
}
clrscr();
printf("\n order of matrix a: %d /t %d",m,n);
printf("elements of array a\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("5d\t",a[i][j]);
}
printf("\n");
}
printf("\nthe order of matrix b is %d\t%d",p,q);
printf("\nelements of second array\n");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
printf("%d\t",b[i][j]);
}
printf("\n");
}
printf("\nthe order of matrix c is %d\t%d",m,q);
printf("\nelements of the array c is\n");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
c[i][j]=0;
for(k=0;k<p;k++)
c[i][j]=a[i][k]*b[k][j]+c[i][j];
}
}
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
printf("\n");
getch();}
}
25)program to transpose a given matrix
#include<stdio.h>
#include<conio.h>
main()
{
int a[10][10],c[10][10],b[10][10],i,j,k,n,m,p,q;
int ch;
clrscr();
printf("enter the size of first matrix");
scanf("%d",&m);
scanf("%d",&n);
printf("enter the elements for first array\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{

b[i][j]=a[j][i];
}
}

printf("the elements of the array are\n");


for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",b[i][j]);
}
printf("\n");
}
getch();
}

26)Program to
i) compare two strings,
ii)length of a string,
iii)concatenation of two strings

i)
#include"stdio.h"
#include"conio.h"
void main()
{
char str1[20],str2[20],ch;
int no,i=0;
clrscr();
printf("\nenter the first string\n");
scanf("%s",str1);
printf("\nenter the secon string\n");
scanf("%s",str2);
do
{
printf("\ncomparision of two strings");
printf("\n 1:built i9n type");
printf("\n2:manual type");
printf("\nenter u r choice);
scanf("%d",&no);
switch(no)
{
case 1:
if(strcmp(str1,str2)==0)
printf("\n both strings are equal");
else
printf("\nboth strings are not equal");
break;
case 2:
while(str1[i]==str2[i]&&str[1]!='\0' &&str[i]!='0')
i=i+1;
if(str1[i]=='\0' && str[i] =='\0')
printf("\nboth strings are equal");
else
printf("\n both strings are not equal");
break;
default:printf("\n wrong choice");
break;
}
printf("\n do u want to continue");
scanf("%s",&ch);
}while(ch=='y');
}

ii)
#include"stdio.h"
#include"conio.h"
void main()
{
char str1[20],str2[20],ch;
int no,i=0,len;
clrscr();
printf("\nenter the first string\n");
scanf("%s",str1);
printf("\nenter the secon string\n");
scanf("%s",str2);
do
{
printf("\nlength of two strings");
printf("\n 1:built in type");
printf("\n2:manual type");
printf("\nenter u r choice");
scanf("%d",&no);
switch(no)
{
case 1:
len=strlen(str1);
printf("\n length of first string %d",len);
len=strlen(str2);
printf("\nlength ofm seond string %d",len);
break;
case 2:
len=0;
i=0;
while(str1[i]!='\0' )
{
len++;
i=i+1;
}
printf("\n length of first string is %d",len);
i=0;
len=0;
while(str2[i]!='\0' )
{
len++;
i++;
}
printf("\nlength of second string %d",len);

break;
default:printf("\n wrong choice");
break;
}
printf("\n do u want to continue");
scanf("%s",&ch);
}while(ch=='y');
}

iii)
#include"stdio.h"
#include"conio.h"
void main()
{
char str1[20],str2[20],ch,c[40];
int no,i=0,len,j;
clrscr();
printf("\nenter the first string\n");
scanf("%s",str1);
printf("\nenter the secon string\n");
scanf("%s",str2);
do
{
printf("\nconcatenation of two strings");
printf("\n 1:built in type");
printf("\n2:manual type");
printf("\nenter u r choice");
scanf("%d",&no);
switch(no)
{
case 1:
printf("\n the first string is %s",str1);
printf("\n the second string %s",str2);
printf("\nconcatenation of two strings are
%s",strcat(str1,str2));
break;
case 2:
printf("\n the first string is %s",str1);
printf("\n the second string is %s",str2);
i=0;
while(str1[i]!='\0' )
{
c[i]=str1[i];
i=i+1;
}
c[i]=' ';
j=0;
while(str2[j]!='\0' )
{
c[i]=str2[j];
i++;
j++;
}
c[i+j+1]='\0';
printf("\nconcatenation of second string %s",c);

break;
default:printf("\n wrong choice");
break;
}
printf("\n do u want to continue");
scanf("%s",&ch);
}while(ch=='y');
}

27) write a program to demonstrate the details of a given number of


students using structures

#include"stdio.h"
#include"conio.h"
struct student
{
int sno;
char name[30];
int m1,m2,m3,m4,m5,m6;
int total;
float avg;
};
void main()
{
struct student s[100];
int n,i;
clrscr();
printf("enter how many student details ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nenter %d student details",i+1);
printf("\nenter the student number");
scanf("%d",&s[i].sno);
printf("\nenter the name of the student");
fflush(stdin);
scanf("%s",s[i].name);
printf("enter the marks in 6 subjects");
scanf("%d",&s[i].m1);
scanf("%d",&s[i].m2);
scanf("%d",&s[i].m3);
scanf("%d",&s[i].m4);
scanf("%d",&s[i].m5);
scanf("%d",&s[i].m6);
}
for(i=0;i<n;i++)
{
if(s[i].m1<35||s[i].m2<35||s[i].m3<35||s[i].m4<35||s[i].m5<35||
s[i].m6<35)
printf("the student has failed");
else
{
s[i].total=s[i].m1+s[i].m2+s[i].m3+s[i].m4+s[i].m5+s[i].m6;
s[i].avg=(float) (s[i].total)/6;
if(s[i].avg>=75)
printf("distinction");
else if (s[i].avg>60 && s[i].avg<75)
printf("first class");
else if(s[i].avg>50)
printf("second class");
else
printf("third");
printf(" \nthe total is %d",s[i].total);
printf("\n the average is %f",s[i].avg);
}
}
getch();
}
11)write a c program
i)program to demonstrate nesting of structure
ii)program to demonstrate passing of structures to functions

i)
#include"stdio.h"
#include"conio.h"
struct salary
{
char name[30];
char dept[20];
struct
{
int da;
int hra;
}allowance;
}employee ;
void main()
{
printf("\n enter the details of the employee");
printf("\n enter the name and department");
scanf("%s%s",employee.name,employee.dept);
printf("enter the details of allowance");
scanf("%d%d",&employee.allowance.da,&employee.allowance.hra);
clrscr();
printf("**************printing details*************");
printf("\n name is %s",employee.name);
printf("\n dept name %s",employee.dept);
printf("\n da is %d",employee.allowance.da);
printf("\n hra is %d",employee.allowance.hra);
getch();
}

ii)
#include"stdio.h"
#include"conio.h"
struct store
{
char name[30];
float price;
int qty;
}p;
struct store update(struct store,float,int);
float mul(struct store);
void main()
{
float o,value;
int q;
struct store item;
printf("\nenter the product name,price and quantity\n");
scanf("%s%f%d",p.name,&p.price,&p.qty);
printf("\nenter the value to be incremented");
scanf("%f%d",&o,&q);
item=update(p,o,q);
printf("\n the updated values of item\n");
printf("\n name is %s",item.name);
printf("\n price is %f",item.price);
printf("\n quantity is %d",item.qty);
value=mul(item);
printf("\n value of the itrem is %f",value);
getch();
}
struct store update(struct store pro,float p,int r)
{
pro.price+=p;
pro.qty+=r;
return(pro);
}
float mul(struct store s)
{
float p=s.qty*s.price;
return(p);
}
28)
Program to demonstrate i)unions
ii)enumerated data types

i)#include"stdio.h"
#include"conio.h"
union value
{
int i;
float f;
};
main()
{
union value x;
x.i=10;
x.f=144.45;
printf("\nfirst value is %d",x.i);
printf("\nsecond value is %f",x.f);
getch();
}
ii)
#include"stdio.h"
#include"conio.h"
enum week{sunday,monday,tuesday,thursday,friday,saturday};
void main()
{
enum week day1,day2;
clrscr();
day1=sunday;
day2=saturday;
printf("\n the first day id %d",day1);
printf("\n the last day is %d",day2);
day1=sunday+6;
day2=saturday-6;
printf("\n after performaincing arthemetic operators");
printf("\n first day %d",day1);
printf("\n the last day %d",day2);
getch();
}
29)write a program to find out the sum of diagonal elements in an
array

#include"stdio.h"
#include"conio.h"
void main()
{
int a[10][10],i,j,m,sum=0;
clrscr();
printf("\nenter the size of an arrya");
scanf("%d",&m);
printf("\n enter the elemens for an arrya");
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\n the elements of an arrya are\n");
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
printf("\n the sum of diagnol elements are\n");
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
if(i==j)
{
printf("%d\t",a[i][j]);
sum=sum+a[i][j];
}
}
}
printf("\n the sum of diagnl elements of an arrya is %d",sum);
getch();
}
30)program to count the number of words,letters,vowels,consonants,and
lines in a given string
#include "stdio.h"
#include"conio.h"
#include"string.h"
#include"ctype.h"
# define TAB 9
# define SPACE 32
main()
{
char newstring[80],string[80],s[80];
int i, vowels=0, cons=0, spchars=0, digits=0;
int tabs=0, spaces=0, words=0;

printf("enter the string\n");


gets(newstring);
strcpy(string,newstring);
for(i=0; i<strlen(string); i++)
{
if(string[i] == TAB)
{
tabs++;
if(string[i-1] != TAB && string[i-1] != SPACE && i !=0)
words++;
}
else if(string[i] == SPACE)
{
spaces++;
if(string[i-1] != SPACE && string[i-1] != TAB && i !=0)
words++;
}

else if(isdigit(string[i]))
digits++;
else if((isalpha(string[i])))
{
switch(string[i])
{
case 'A':
case 'a':
case 'E':
case 'e':
case 'I':
case 'i':
case 'O':
case 'o':
case 'U':
case 'u': vowels++; break;
default: cons++;break;
}
}
else spchars++;
}

strcpy(s,string);
if(strlen(s) !=0 && string[i-1] != SPACE && string[i-1] != TAB)
words++;

printf("\nWords %d" ,words);


printf("\nSpaces %d",spaces);
printf("\nTabs %d" ,tabs);
printf("\nDigits %d" ,digits);
printf("\nVowels %d" ,vowels);
printf("\nConsonents %d" ,cons);
printf("\nSpecial Characters %d ",spchars);
getch();
}
31)write a program to
1)check whether a given number is Armstrong or not
11)chech whether a given perfect or not
Iiicheck whether a given number is palindarome or not

i)#include"stdio.h"
#include"conio.h"
void main()
{
int n,r,rd,num;
rd=0;
r=0;
printf("\n enter any number");
scanf("%d",&num);
n=num;
while(num>0)
{
rd=num%10;
num=num/10;
r=r+(rd*rd*rd);
}
if(n==r)
printf("\n the given number %d is armstrong",n);
else
printf("\n enter the given number %d is not armstrong",n);
getch();

}
ii)
#include"stdio.h"
#include"conio.h"
void main()
{
int n,r,rd,num;
rd=0;
r=0;
printf("\n enter any number");
scanf("%d",&num);
n=num;
while(num>0)
{
rd=num%10;
num=num/10;
r=r+rd;
}
if(n==r)
printf("\n the given number %d is perfect number",n);
else
printf("\n enter the given number %d is not perfect number",n);
getch();

iii)
#include"stdio.h"
#include"conio.h"
void main()
{
int n,r,rd,num;
rd=0;
r=0;
printf("\n enter any number");
scanf("%d",&num);
n=num;
while(num>0)
{
rd=num%10;
num=num/10;
r=(r*10)+rd;
}
if(n==r)
printf("\n the given number %d is palindarome",n);
else
printf("\n enter the given number %d is not palinarome",n);
getch();
}

32)write a program to access the elements of an array using pointers


#include"stdio.h"
#include"conio.h"
main()
{
int a[10],i,j,m,*p;
printf("\n enter the size of the array <=10");
scanf("%d",&m);
printf("\n enter the elements of an aryay\n");
for(i=0;i<m;i++)
scanf("%d",&a[i]);
p=&a[0];
for(i=0;i<m;i++)
{
printf("\n the elements of the array are %d\t",*p);
p++;
}
getch();
}
18)write to evaluate the expression 1+1/2+1/3+……………………………+1/n
#include"stdio.h"
#include"conio.h"
main()
{
int i,j,l,m;
float v=0,n;
printf("\nenter the number of ters for the expression");
scanf("%d",&i);
for(j=1;j<=i;j++)
v=v+(1/(float) j);
printf("the value of the expression %f",v);
getch();
}

You might also like