Saturday, November 3, 2018

Prime and Fibonacci in alternative way in C programming

Consider the below series
1,2,1,3,2,5,3,7,5,11,8,13,13,17,.....

This series is a mixture of 2 series - all the odd terms in this series from a fibonacci series and all the even terms are the prime numbers in ascending order.


Write a program to find the Nth term in this series.

The value N is a positive integer that should be read from STDIN. The Nth term is calculated by the program should be written to STDOUT. Other than the value of Nth term, no other character/string or message should be written to STDOUT.

For example, When N=14, the 14th term in the series is 17. So only the value 17 should be printed to STDOUT.

Program:-

#include<stdio.h>
int prime(int x);
int giveprime(int x);
int feb(int x);
int main()
{
    int n,res,i,count=0;
    //printf("Enter nth Term=");
    scanf("%d",&n);
    if(n<=0)
    {
        //printf("\nEnterd number is negative");
        return 0;
    }
    if(n%2==0)
    {
        res=giveprime(n/2);
        printf("%d\n",res);
    }
    else{
        res=feb((n/2)+1);
        //printf("\n%dth Term number is=%d\n",n,res);
        printf("%d\n",res);
    }
    return 0;
}
int prime(int x)
{
    int i;
    for(i=2;i<x;i++)
    {
          if(x%i==0)
        return 0;
    }
    return 1;
}
int giveprime(int x)
{
    int i=2,count=0,res;
    while(i)
    {
        res=prime(i);
        if(res==1)
            count++;
        if(count==x)
            return i;
        i++;
    }
}
int feb(int x)
{
    int i,first=1,second=1,next=0,temp;
    if(x>0){
    for(i=0;i<x;i++)
    {
        temp=first;
        next=first+second;
        first=second;
        second=next;
    }
    }
    else{
    return 0;
    }
    return temp;
}

Output: -


No comments:

Post a Comment

Acer Nitro 5 Ryzen 7 4800H - Blender Open Data Benchmark | Blender BMW Benchmark

Hello friends in this video I have perform benchmark of Acer Nitro 5 Ryzen 7 4800H laptop using Blender Open data Benchmark tool. In this to...