Home » Sometech » Write a menu driven program for first n Fibonacci series numbers and prime numbers.

Write a menu driven program for first n Fibonacci series numbers and prime numbers.

#include <stdio.h>

#include <conio.h>

#include <math.h>

void main()

{

                 int t,count,num,s,i,choice,f1=0,f2=1,f3;

                 clrscr();

                 printf(“\nHow many terms you need ?  “);

                 scanf(“%d”,&t);

                 printf(“\n*******************************”);

                 printf(“\n* 1. Prime numbers            *”);

                 printf(“\n* 2. Fibonacci series numbers *”);

                 printf (“\n* 3. Exit                     *”);

                 printf(“\n*******************************”);

            do

{

                 printf(“\n\nEnter choice =>”);

                 scanf(“%d”,&choice);

                 switch(choice)

{

         case 1:

                      printf(“\n%d Prime numbers are :\n”,t);

                      printf(“%8d”,2); // 2 is the first prime number

                      count=1;

                      num=3;

                      while(count<t)

{

               s=sqrt(num);

                       i=2;

                       while(i<=s)

{

              if(num%i==0)

                  break;

                  i++;

}

                    if(i>s)

{

                  printf(“%8d”,num);

                        count++;

}

    num=num+2;

}

break;

       case 2:

                  printf(“\n%d Fibonacci numbers are :\n”,t);

                       printf(“%8d%8d”,f1,f2); // 0 and 1 are first 2 fibonacci numbers

                  count=3;

            while(count<=t)

{

                f3=f1+f2;

                printf(“%8d”,f3);

                f1=f2;

                f2=f3;

               count++;

}

              break;

          case 3: exit();

          default:

                   printf(“\nChoice is wrong!!!”);

                getch();

}

}

          while(choice<=3);

}

Advertisements

SoME Tech

Error: Twitter did not respond. Please wait a few minutes and refresh this page.

%d bloggers like this: