0% found this document useful (0 votes)
143 views6 pages

NTH Term in The Series

The document describes two series: 1) A series that alternates between even numbers in ascending order for odd terms and values derived by dividing the previous term by 2 for even terms. 2) A Fibonacci series where each term is the sum of the previous two terms. The problem is to write a program that takes an input N and outputs the Nth term of a mixed series that takes odd terms from the Fibonacci series and even terms from the prime numbers series.

Uploaded by

Anand Amsuri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
143 views6 pages

NTH Term in The Series

The document describes two series: 1) A series that alternates between even numbers in ascending order for odd terms and values derived by dividing the previous term by 2 for even terms. 2) A Fibonacci series where each term is the sum of the previous two terms. The problem is to write a program that takes an input N and outputs the Nth term of a mixed series that takes odd terms from the Fibonacci series and even terms from the prime numbers series.

Uploaded by

Anand Amsuri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Nth term in the series

Consider the below series : 0,0,2,1,4,2,6,3,8,4,10,5,12,6,14,7,16,8 This series is a


mixture of 2 series all the odd terms in this series form even numbers in ascending
order and every even terms is derived from the previous term using the formula
(x/2) .Write a program to find the nth term in this series. The value n in a positive
integer that should be read from STDIN the nth term that is calculated by the program
should be written to STDOUT. Other than the value of the nth term no other characters
/strings or message should be written to STDOUT.
For example if n=10,the 10 th term in the series is to be derived from the 9th term in
the series. The 9th term is 8 so the 10th term is (8/2)=4. Only the value 4 should be
printed to STDOUT. You can assume that the n will not exceed 20,000.

Input (stdin)
10
Output (stdout)
4

int main() {
int n;
scanf("%d", &n);
if(n % 2 == 1)
{
int a = 1;
int r = 2;
int term_in_series = (n+1)/2;
int res = 2 * (term_in_series - 1);
printf("%d ", res);
}
else
{
int a = 1;
int r = 3;
int term_in_series = n/2;
int res = term_in_series - 1;
printf("%d ", res);
}
return 0;
}
Nth term in the fibonacci series
Look at the series below:1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,.....This series is
formed as below: 1.term(1)=1 2.term(2)=2 3.term(N)=term(N-1)+term(N-2)for N>2 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 that is calculated by the program should be
written to STDOUT, other than the value of nth term no other characters /strings and
messages should be written to STDOUT.
Sample Input:
15
Sample Output:
987

#include<stdio.h>
int main()
{
//Type your code here
int t1=1,t2=2,t3,i,n;
scanf("%d",&n);
if(n==1)
printf("1");
else if(n==2)
printf("2");
else{
for(i=3;i<=n;i++){
t3=t1+t2; //3
t1=t2; //t1=2
t2=t3; //t2=3
}
printf("%d",t3);
}
return 0;
}

Nth term in the series


Consider the below series : 0,0,2,1,4,2,6,3,8,4,10,5,12,6,14,7,16,8 This series is a
mixture of 2 series all the odd terms in this series form even numbers in ascending
order and every even terms is derived from the previous term using the formula
(x/2) .Write a program to find the nth term in this series. The value n in a positive
integer that should be read from STDIN the nth term that is calculated by the program
should be written to STDOUT. Other than the value of the nth term no other characters
/strings or message should be written to STDOUT.
For example if n=10,the 10 th term in the series is to be derived from the 9th term in
the series. The 9th term is 8 so the 10th term is (8/2)=4. Only the value 4 should be
printed to STDOUT. You can assume that the n will not exceed 20,000.
Input (stdin)
10
Output (stdout)
4

int main() {
int n;
scanf("%d", &n);
if(n % 2 == 1)
{
int a = 1;
int r = 2;
int term_in_series = (n+1)/2;
int res = 2 * (term_in_series - 1);
printf("%d ", res);
}
else
{
int a = 1;
int r = 3;
int term_in_series = n/2;
int res = term_in_series - 1;
printf("%d ", res);
}
return 0;
}

Q.
For Example, consider the given 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 form a Fibonacci
series and all the even terms are the prime numbers in ascending order. Now write a
program to find the Nth term in this series.
Input: 14 Output: 17

#include<stdio.h>
#define MAX 1000
void fibonacci(int n)
{
int i, t1 = 0, t2 = 1, nextTerm;
for (i = 1; i<=n; i++)
{
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
}
printf("%d", t1);
}
void prime(int n)
{
int i, j, flag, count =0;
for (i=2; i<=MAX; i++)
{
flag = 0;
for (j=2; j<i; j++)
{
if(i%j == 0)
{
flag = 1;
break;
}
}
if (flag == 0)
if(++count == n)
{
printf("%d", i);
break;
}
}
}

int main()
{
int n;
scanf("%d", &n);
if(n%2 == 1)
fibonacci (n/2 + 1);
else
prime(n/2);
return 0;
}

You might also like