Computer Concept & Programming In C - Unit III - 10

Q.19 Write a C program to print nth Fibonacci number.                       (AKTU. 2009-10)
Ans. /* Write a C program to print the n-th Fibonacc1 number  */
 #include<stdio.h>
 #include<conio.h>
 long fibonacci(int);
 void main()
 {
  int n;
  clrscr();
  printf("\nEnter the number of Fibonacci term\n");
  scanf("%d",&n);
  printf("\nThe %d Fibonacci number is %ld",n,fibonacci(n));
  getch();
 }
 long fibonacci(int a)
 {
  if((a == 0)||(a == 1))
    return a;
  else if( a > 1)
return(fibonacci( a - 1) + fibonacci( a - 2));
       else
{
  printf("\nPrint wrong value\n");
  return -1;
}
 }

Q.20. Write an algorithm to print the first 100 Fibonacci numbers and their sum.                                                                                             (AKTU. 2010 - 11)
Ans.
1. f 0 ¬ 0     
2. sum ¬ 1
3. f 1 ¬ 1
4. Write “First 100 Fibonacci numbers”
5. for i ¬ 3 to 100                                                              
6.                     do            t ¬ f 0 + f 1 
7.                                     Write t, newline
8.                                     sum ¬ sum + t                           
9.                                     f 0 ¬ f 1
10.                                   f 1 ¬ t

11. Write “Sum of first 100 Fibonacci terms”, sum

Q.21 Draw the flowchart and write a program in C to calculate the sum of Fibonacci series upto 100 terms.                                                                 (AKTU. 2010-11)
Ans.
#include<stdio.h>
#include<conio.h>
void main()
{
  double i = 0.0, j = 1.0, k, sum = 1.0;

  int l;
  clrscr();
  printf("\nThe sum of first 100 Fibonacci Numbers:\n");
  for( l = 0; l < 100; l++)
    {
      k = i + j;
      sum = sum + k;
      i = j;
      j = k;
    }
   printf("\n%lf", sum );
  getch();
}
The flowchart is shown as follows :



Q.22. Write a program in C to read a five digit number if it is even then add up the digits otherwise multiply then and print the result.                                      (AKTU. 2011 - 12)
Ans.
# include<stdio.h>
# include<conio.h>
        {
long n, s, p;
clrscr ( );
printf(“\n Enter a 5 digit number”);
scanf(“%ld”, & n);
         if ((n%2)==0)//Even
{
  // adding digit
  s = 0;
  while (n > 0)
{
            s + = (n - 10*(n/10));
           n /= 10;
       }
            printf(“\n Number is even”);
            printf(“\n sum of its digit=%ld”,s);
          }
     else //odd
        {
p = 1;
while (n>0)
  {
p*=(n-10*(n/10));
n/=10;
  }
printf(“\n Number is odd”);
printf(“\n product of its digit = %ld”,p);
 }
getch( );
}