an ecommerce website wishes to find the lucky customer who will be eligible for full value cashback. for this purpose, a number n is fed to the system. it will return another number that is calculated by an algorithm. in an algorithm, a sequence is generated, in which each number is the sum of the two preceding numbers. initially, the sequence will have two 1's in it. the system will return the nth number from the generated sequence which is treated as the order id. the lucky customer will be the one who has placed that order. write an algorithm to help the website find the lucky customer.
#include <stdio.h>
int lucky(int n){
if (n <= 2)
return 1;
int currentNumber = 1;
int previousNumber = 1;
int x = 2;
while (n > x){
currentNumber = currentNumber + previousNumber;
previousNumber = currentNumber;
x += 1;
}
return currentNumber;
}
int main()
{
int n;
scanf("%d",&n);
printf("order id :%d",lucky(n));
return 0;
}
Comments
Leave a comment