Monday, November 17, 2014

Fibonacci Series C Programming



C Program to Display same Source Code as Output

#include
#include 
int main()
{
   FILE *fp;
   char ch;
   fp = fopen(__FILE__, "r");
   do
  {
      ch = getc(fp);
      putchar(ch);
   }
   while (ch != EOF);
   fclose(fp);
   return 0;
}

Output

#include
#include
int main()
 {
   FILE *fp;
   char ch;
   fp = fopen(__FILE__, "r");
   do
     {
      ch = getc(fp);
      putchar(ch);
      }
    while (ch != EOF);
   fclose(fp);
   return 0;
}

C Program to Add two Numbers Using Function in C Programming


#include
#include
int main()
{
   int num1, num2, res;
   printf("\nEnter the two numbers : ");
   scanf("%d %d", &num1, &num2);
   res = sum(num1, num2);
   printf("\nAddition of two number is : ");
   return (0);
}
int sum(int num1, int num2)
{
   int num3;
   num3 = num1 + num2;
   return (num3);
}

Output

Enter the two numbers : 12 15
Addition of two number is : 27

To Display of message using function in C Programming

#include
#include
void main()
{
message();
}
void message()
{
printf("Hi, How are you? ");
}

Output
         
         Hi, How are you?

C Program to generate the Fibonacci Series starting from any two numbers

#include
#include 
int main()
{
   int first, second, sum, num, counter = 0;
   printf("Enter the term : ");
   scanf("%d", &num);
   printf("\nEnter First Number : ");
   scanf("%d", &first);
   printf("\nEnter Second Number : ");
   scanf("%d", &second);
    printf("\nFibonacci Series : %d  %d  ", first, second);
    while (counter < num)
     {
      sum = first + second;
      printf("%d  ", sum);
      first = second;
      second = sum;
      counter++;
   }
    return (0);
}

Output

Enter the term : 5
Enter First Number : 1

Enter Second Number : 3

Fibonacci Series : 1 3 4 7 11 18 29

No comments:

Post a Comment