Telegram: https://t.
me/campusdrive
CTS AUTOMATA FIX
PREVIOUS ERROR
DEBUGGING
Cognizant Telegram Group: Click to join
https://t.me/cognizant_exam
1) PRINT THE FOLLOWING PATTERN
11
111
1111
CODE GIVEN BY THEM :
#include<stdio.h>
void patternPrint(int num)
int print=1,i,j;
for(i=0;i<num;i++)
for(j=0;j<=i;j++)
Telegram: https://t.me/campusdrive
{
printf("%d",print);
printf("\n");
CORRECTION :
NO CORRECTION REQUIRED
2) The function/method checkBirthDay return an integer 1 if it is her birthday else
returns 0. The function/method checkBirthDay accepts two arguments - month, a string
representing the month of her birthday and day an integer representing the date of her
birthday,
The function/method checkBirthDay compiles successfully but fails to return the desired
result for some test cases. Your task is to fix the code so that it passes all the test cases.
CODE GIVEN BY THEM :
int checkBirthDay(char month, int day)
if((strcmp(month,"July")) || (day==5))
return 1;
else
return 0;
Telegram: https://t.me/campusdrive
Correction Required:
int checkBirthDay(char month, int day)
if(!(strcmp(month,"July")) &&(day==5))
return 1;
else
return 0;
Reason:
The function strcmp return 0 if strings are equal .So we need to keep ! .Also we need to
use && instead of || as both the conditions need to be satisfied
3) Same element count
CODE GIVEN BY THEM:
#include <stdio.h>
int sameElementCount(int size,int inputlist)
int count=0;
for(int i=0;i<size-1;i++)
if((inputlist[i]%2==0) &&(inputlist[i]==inputlist[i++]))
Telegram: https://t.me/campusdrive
count++;
return count;
CORRECTION:
#include <stdio.h>
int sameElementCount(int size,int inputlist)
int count=0;
for(int i=0;i<size-1;i++)
if((inputlist[i]%2==0) &&(inputlist[i]==inputlist[i+1]))
count++;
return count;
REASON:
Array element inputlist[i+1] should be used not inputlist[i++] ,i++ is an increment
operator
4) COUNTOCCURENCES
Telegram: https://t.me/campusdrive
GIVEN CODE
int countOccurance(int len, int value, int arr)
int i=0,count=0;
while(i<len)
if(arr[i]==value)
count+=1 ;
return count;
CORRECTION
int countOccurance(int len, int value, int arr)
int i=0,count=0;
while(i<len)
if(arr[i]==value)
count+=1 ;
i++;
return count;
REASON: while loop must be incremented by 1 every time
Telegram: https://t.me/campusdrive
5)
void manchester(int len,int arr)
int res=(int )malloc(sizeof(int) len);
res[0]=!(arr[0]==0);
for(int i=1;i<len;i++)
res[i]=!(arr[i]==arr[i-1]);
for(int i=0;i<len;i++)
printf("%d ",res[i]);
Reason:
In the program the comparison of first element with 0 is not done
Telegram: https://t.me/campusdrive
Also if elements are equal 0 must be printed but not 1 so must include not symbol
6)
Sol:
int multiplyNumber(int numA,int numB,int numC)
int result,min,max,mid;
max=(numA>numB?(numA>numC?numA:numC):(numB>numC?numB:numC));
min=(numA<numB?(numA<numC?numA:numC):(numB<numC?numB:numC));
mid=numA+numB+numC-(max+min);
result= mid max;
return result;
Reason: syntax errors
Telegram: https://t.me/campusdrive
7) GCD OF TWO NUMBERS
GIVEN CODE
CORRECTION
Telegram: https://t.me/campusdrive
8)
sol:
void printPattern(int num)
int i,j;
for(i=1;i<=num;i++)
for(j=1;j<=2 i;j++)
printf("%d ",i);
printf("\n");
Reason:The loops are given in wrong way
Telegram: https://t.me/campusdrive
9) MAX REPLACE:
GIVEN CODE:
CORRECTION:
Telegram: https://t.me/campusdrive
10) FIND SUM
CORRECTION
11)
Telegram: https://t.me/campusdrive
Sol:
void printFibonacci(int num)
int i;
long sum=0;
long num1=0;
long num2=1;
for(i=1;i<=num;++i)
printf("%ld ",num1);
sum=num1+num2;
num2=num1;
num1=sum;
reason:Not an appropriate logic for fibonacci series
12)
Telegram: https://t.me/campusdrive
Sol:
void reverseHalfArray(int size,int inputList)
int i,temp;
for(i=0;i<size/2;i++)
temp=inputList[size-i-1];
inputList[size-i-1]=inputList[(size/2)+i];
inputList[(size/2)+i]=temp;
Reason:The loop statements are not properly runned
13)
Sol:
Telegram: https://t.me/campusdrive
int countDigits(int num)
int p=num;
int count=0;
while(num!=0)
num=num/10;
count++;
return(p%count);
Reason:the num variable becomes 0 so it needed to be stored in another variable p
14)
Telegram: https://t.me/campusdrive
Sol:
void arrayReverse(int len,int arr)
int i,temp,originalLen=len;
for(i=0;i<originalLen/2;i++)
temp=arr[len-1-i];
arr[len-1-i]=arr[i];
arr[i]=temp;
reason:
The array can be reversed by swapping from first and last upto middle
15)d
Telegram: https://t.me/campusdrive
int sortArray(int len,int arr)
int i=0,j=0,temp=0;
for(i=0;i<len;i++)
for(j=i+1;j<len;j++)
if(arr[i]>arr[j])
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
return arr;
int findMaxElement(int len1,int arr1,int len2,int arr2)
int brr1=sortArray(len1,arr1);
int brr2=sortArray(len2,arr2);
int a=(brr1[len1-1]>brr2[len2-1]?brr1[len1-1]:brr2[len2-1]);
return a;
Reason:
Telegram: https://t.me/campusdrive
to find max among
two arrays sort the two arrays and take last elements of two arrays and return
maximum of those two elements
Telegram: https://t.me/campusdrive
1) Check for syntax error/ logical
error and correct the error to get the desired output.
Given n, print from n to 0
int main()
int n;
scanf("%d", &n);
unsigned int i = n;
while(i >= 0)
printf("%dn", i);
i--;
return 0;
Input: 4 Output: Infinite loop Answer:
Error - Logical error unsigned int i = n; unsigned integer ranges from 0 to 65535, which
will be taken in the cyclic order.So i-- will keep repeating in a cyclic way. The loop will
never be terminated. So it should be written as int i = n;
2) FACTORIAL OF GIVEN NUMBER
int main()
long int fact = 1, n, i;
scanf("%d", &n);
Telegram: https://t.me/campusdrive
for(i =1; i <= n; i++)
fact = fact i;
printf("%d", fact);
return 0;
}
Input: 20
Output: -2102132736
Answer:Error Logical error
The fact and n are declare d as long int, so in scanf and printf%ld should be used
in place of %d
3) Check whether the below program print the below pattern
1111
222
33
void main()
int i, j, n;
scanf("%d", &n);
for(i = 1; i<=n; i++)
for(j = 1; j<=n; j++)
{
Telegram: https://t.me/campusdrive
printf("%d", i);
printf("\n");
}
Input: 3
Output:
111
222
333
Answer:Error: Logical error
The inner for loop has to be written in this way: for(j = i-1; j<n+1; j++)
4) Find the greatest of three numbers.
int main()
int num1, num2, num3;
scanf("%d %d %d", &num1,&num2,&num3);
if (num1 > num2) && (num1 > num3)
printf("%d", num1);
elseif(num2>num3)
printf("%d", num2)
Telegram: https://t.me/campusdrive
}
else
printf("%d", num3);
return 0;
Answer:Error: Syntax error
if (num1 > num2) && (num1 > num3) it has to be written
5) Fix the error, recompile and match against the output provided.
int main(void)
printf("This is a "buggy" programn");
return 0;
Corrected program:
int main(void)
printf("This is a "buggy" program n");
return 0;
}
Telegram: https://t.me/campusdrive
6) Code
reuse:Convert Binary to Decimal by using the existing function.
voidbinarytodecimal(number)
// Type your code here
void main()
int num;
scanf("%d", &num);
printf(%d, binarytodecimal(num);
Answer
voidbinarytodecimal(number)
int dval=0, base=1, rem;
while(number > 0)
rem = number % 10;
dval = dval + rem * base;
num = number / 10;
base = base * 2;
Telegram: https://t.me/campusdrive
}
return dval;
7) Print the prime numbers from an array up to given value n by using existing function.
int isprime(int num)
// type your code here
int main()
int n, m, arr[100], size=0, i;
scanf("%d", &n);
for(m = 2; m <= n; m++)
if(isprime(m))
arr[size++]= m;
for(i = 0; i < size; i++)
printf("%dn", arr[i]);
return 0;
}
Telegram: https://t.me/campusdrive
Answer:
int isprime(int num)
int i;
int isprime = 1;
for(i = 2; i <= num / 2; i++)
if(num % i == 0)
isprime = 0;
break;
return isprime;
8) Find the syntax error in the below code without modifying the logic.
#include <stdio.h>
int main()
float x = 1.1;
switch (x)
case 1: printf("Choice is 1");
Telegram: https://t.me/campusdrive
break;
default: printf("Invalid choice");
break;
return 0;
Answer
#include <stdio.h>
int main()
int x = 1;
switch (x)
case 1: printf("Choice is 1");
break;
default: printf("Invalid choice");
break;
return 0;
The expression used in the switch must be an integral type (int, char, and enum). Any
other type of expression is not allowed.
9) Find the logical error in the below code.
void main () {
int i, j, n = 5;
Telegram: https://t.me/campusdrive
for(i=1; i<n; i++)
for(j=i;j<n;j++);
printf("%d", i);
printf("\n");
Solution:
void main () {
int i, j, n = 5;
for(i=1; i<n; i++)
for(j=i;j<n;j++)
printf("%d", i);
printf("\n");
we use a semicolon in C statement to tell the compiler where's the end of our statement.
Second for loop executes one time.
Telegram: https://t.me/campusdrive
10 )Complete the below code by
reusing the existing function.
Find the index of equilibrium element in the given array. In an array equilibrium element
is the one where the sum of all the elements to the left side is equal to the sum of all the
elements in the right side.
Return Value:
1) Return -1 if no equilibrium element is found
2) In case there is more than one equilibrium element, return the element with least index
value
You are required to complete the given code by reusing the existing function. You can
click on Compile & run anytime to check the compilation/execution status of the program
you can use printf to debug your code. The submitted code should be
logically/syntactically correct and pass all the test cases.
Code approach For the question:
You will need to correct the given implementation.
We do not expect you to modify the approach or incorporate any additional library
methods.
Test Case:
a[] = {1,2,3,4,3,3}. 4 is the equilibrium element since its left side sum (1+2+3) is equal to its
right side sum (3+3)
#include <stdio.h>
// Return the sum of elements from index 0 to (idx - 1)
int left_side_sum(int a[], int n, int idx)
int sum = 0, i;
for(i = 0; i < idx; i++)
sum += a[i];
Telegram: https://t.me/campusdrive
}
return sum;
// Return the sum of elements from index (idx + 1) to (n - 1)
int right_side_sum(int a[], int n, int idx)
int sum = 0, i;
for(i = idx + 1; i < n; i++)
sum += a[i];
return sum;
// returns -1 if no equilibrium index found
int findEquilibriumIndex(int a[], int n)
// Type your code here
int main() {
//code
int a[10], n, i;
// get the elements count
scanf("%d", &n);
// get the array elements
Telegram: https://t.me/campusdrive
for(i=0; i<n; i++)
scanf("%d", &a[i]);
int equiIndex = findEquilibriumIndex(a, n);
if(equiIndex != -1) {
printf("%d", a[equiIndex]);
return 0;
Solution:
// Return the sum of elements from index 0 to (idx - 1)
int left_side_sum(int a[], int n, int idx)
int sum = 0, i;
for(i = 0; i < idx; i++)
sum += a[i];
return sum;
// Return the sum of elements from index (idx + 1) to (n - 1)
int right_side_sum(int a[], int n, int idx)
int sum = 0, i;
Telegram: https://t.me/campusdrive
for(i = idx + 1; i < n; i++)
sum += a[i];
return sum;
// returns -1 if no equilibrium index found
int findEquilibriumIndex(int a[], int n)
// Type your code here
int i;
for(i = 0; i < n; i++)
if(left_side_sum(a, n, i) == right_side_sum(a, n, i))
return i;
return -1;
int main() {
//code
int a[10], n, i;
// get the elements count
scanf("%d", &n);
Telegram: https://t.me/campusdrive
// get the
array elements for(i=0;
i<n; i++)
scanf("%d", &a[i]);
int equiIndex = findEquilibriumIndex(a, n);
if(equiIndex != -1) {
printf("%d", a[equiIndex]);
return 0;
12) Check for syntax error/ logical error and correct the error to get the desired output.
The function sortString modifies the input list by sorting its elements depending upon
the length of the array, i.e; if the length of the array is even, then the elements are
arranged in the ascending order, and if the length of the array is odd, then the elements
are arranged in the descending order
The function sortString accepts two arguments – len representing the length of the string,
and arr a list of characters, representing the input list respectively.
The function sortString compiles successfully but fails to get the desired results for some
test cases due to logical errors. Your task is to fix the code, so that it passess all the test
cases
Incorrect Code
void sortArray(int len, int arr)
{
Telegram: https://t.me/campusdrive
int i, max,
location, temp, j,k; if(len/2 ==
0)//error in this line
for(i=0;i<len;i++)
max=arr[i];
location = i;
for(j=i;j<len;j++)
if(max<arr[j])//error in this line
max=arr[j];
location = j;
temp=arr[i];
arr[i]=arr[location];
arr[location]=temp;
else
for(i=0;i<len;i++)
max=arr[i];
location = i;
Telegram: https://t.me/campusdrive
for(j=i;j<len;j++)
if(max>arr[j])//error in this line
max=arr[j];
location = j;
temp=arr[i];
arr[i]=arr[location];
arr[location]=temp;
Correct Code
void sortArray(int len, int arr)
int i, max, location, temp, j,k;
if(len%2 == 0)
for(i=0;i<len;i++)
max=arr[i];
location = i;
for(j=i;j<len;j++)
if(max>arr[j])
max=arr[j];
Telegram: https://t.me/campusdrive
location = j;
temp=arr[i];
arr[i]=arr[location];
arr[location]=temp;
else
for(i=0;i<len;i++)
max=arr[i];
location = i;
for(j=i;j<len;j++)
if(max<arr[j])
max=arr[j];
location = j;
temp=arr[i];
arr[i]=arr[location];
arr[location]=temp;
}
Telegram: https://t.me/campusdrive
}
13) Check for syntax error/ logical error and correct the error to get the desired output.
The function sortString modifies the input list by sorting its elements depending upon
the length of the array, i.e; if the length of the array is even, then the elements are
arranged in the ascending order, and if the length of the array is odd, then the elements
are arranged in the descending order
The function sortString accepts two arguments – len representing the length of the string,
and arr a list of characters, representing the input list respectively
The function sortString compiles successfully but fails to get the desired results for some
test cases due to logical errors. Your task is to fix the code, so that it passess all the test
cases
Incorrect Code
void sortArray(int len, int arr)
int i, max, location, temp, j,k;
if(len/2 == 0)//error in this line
for(i=0;i<len;i++)
max=arr[i];
location = i;
for(j=i;j<len;j++)
if(max<arr[j])//error in this line
max=arr[j];
Telegram: https://t.me/campusdrive
location = j;
temp=arr[i];
arr[i]=arr[location];
arr[location]=temp;
else
for(i=0;i<len;i++)
max=arr[i];
location = i;
for(j=i;j<len;j++) if(max>arr[j])//error in this line
max=arr[j];
location = j;
temp=arr[i];
arr[i]=arr[location];
arr[location]=temp;
}
Telegram: https://t.me/campusdrive
}
Correct Code
void sortArray(int len, int arr)
int i, max, location, temp, j,k;
if(len%2 == 0)
for(i=0;i<len;i++)
max=arr[i];
location = i;
for(j=i;j<len;j++)
if(max>arr[j])
max=arr[j];
location = j;
temp=arr[i];
arr[i]=arr[location];
arr[location]=temp;
else
{
Telegram: https://t.me/campusdrive
for(i=0;i<len;i++)
max=arr[i];
location = i;
for(j=i;j<len;j++)
if(max<arr[j])
max=arr[j];
location = j;
temp=arr[i];
arr[i]=arr[location];
arr[location]=temp;
14) Check for syntax error/ logical error and correct the error to get the desired output.
The function replaceElements is modifying the input list in such a way – if the sum of all
the elements of the input list is odd, then all the elements of the input list are supposed to
be replaced by 1s, and in case if the sum of all the elements of the input list is even, then
the elements should be replaced by 0s.
For example, given the input list [1,2,3,4,5], the function will modify the input list like [1,
1, 1, 1, 1]
Telegram: https://t.me/campusdrive
The function
replaceElements accepts two arguments – size an integer representing the size of the
given input list and arr, a list of integers representing the input list.
The function replaceElements compiles successfully but fails to get the desired result for
some test cases due to incorrect implementation of the function. Your task is to fix the
code so that it passes all the test cases
void replaceElements(int size, int arr)
int i,j;
int sum=0;
for (i=0;i<size;i++)
sum+=arr[i];
if(size % 2 == 0)//error in this line
i=0;
while(i<size)
arr[i] = 0;
i += 1;
else
j=1;
Telegram: https://t.me/campusdrive
while(j<size)
arr[j]=1;
j+=1;
Corrected Code
void replaceElements(int size, int arr)
int i,j;
int sum=0;
for (i=0;i<size;i++)
sum+=arr[i];
if(sum % 2 == 0)
i=0;
while(i<size)
arr[i] = 0;
i += 1;
}
Telegram: https://t.me/campusdrive
else
j=1;
while(j<size)
arr[j]=1;
j+=1;
15) Check for syntax error/ logical error and correct the error to get the desired output.
Given n, print 0 to n by identifying whether the n is even or odd.
Test Case : 1
n : 10
Output
0 2 4 6 8 10
Test Case : 2
n:9
Output
1 3 5 79
Incorrect Code
#include <stdio.h>
int main()
int n, i;
Telegram: https://t.me/campusdrive
printf("n : ");
scanf("%d", n);
if(n%2=0)
for(i=0;i<n:i=i+2)
printf("%d ",i);
else
for(i=1;i<n;i=i+2)
printf("%d ",i);
return 0;
Corrected Code
#include <stdio.h>
int main()
int n, i;
printf("n : ");
scanf("%d",&n);
Telegram: https://t.me/campusdrive
if(n%2==0)
for(i=0;i<=n;i=i+2)
printf("%d ",i);
else
for(i=1;i<=n;i=i+2)
printf("%d ",i);
return 0;
17) Check whether the below program print the below pattern
Input
enter number of rows : 4
Output
Telegram: https://t.me/campusdrive
1
22
333
4444
4444
333
22
Incorrect Code
#include <stdio.h>
int main()
int i,j,n;
printf("Enter the number of rows: ");
scanf("%d",&n);
for(i=1;i<=n;i=i+1)
for(j=1;j<=i;j=j+1)
printf("%d",i);
printf("\n");
for(i=n;i>=1;i=i-1)
{
Telegram: https://t.me/campusdrive
for(j=1;j<=i;j=j-1)
printf("%d",i);
return 0;
Corrected Code
#include <stdio.h>
int main()
int i,j,n;
printf("Enter the number of rows: ");
scanf("%d",&n);
for(i=1;i<=n;i=i+1)
for(j=1;j<=i;j=j+1)
printf("%d",i);
printf("\n");
for(i=n;i>=1;i=i-1)
{
Telegram: https://t.me/campusdrive
for(j=1;j<=i;j=j+1)
printf("%d",i);
printf("\n");
return 0;
20) Check for syntax/logical error and correct the code for desired output.
In the code you need to find the greatest among three numbers.
Incorrect Code
#include <stdio.h>
int main()
int a, b, c, max_num;
printf("Enter the three numbers\n");
printf("First: ");
scanf("%d",&a);
printf("Second: ");
scanf("%d",&b);
printf("Third: ");
scanf("%d",&c);
max_num = (a > b) ? (a > c ? a : c) ? (b > c ? b : c);
Telegram: https://t.me/campusdrive
printf("Largest number among %d, %d and %d is %d.", a, b, c, max_num);
return 0;
Corrected Code
#include <stdio.h>
int main()
int a, b, c, max_num;
printf("Enter the three numbers\n");
printf("First: ");
scanf("%d",&a);
printf("Second: ");
scanf("%d",&b);
printf("Third: ");
scanf("%d",&c);
max_num = (a > b) ? (a > c ? a : c) : (b > c ? b : c);
printf("Largest number among %d, %d and %d is %d.", a, b, c, max_num);
return 0;
21) Print the prime numbers from an array up to given value n by using existing function.
int isprime(int num)
// type your code here
Telegram: https://t.me/campusdrive
}
int main()
int n, m, arr[100], size=0, i;
scanf(“%d”, &n);
for(m = 2; m <= n; m++)
if(isprime(m))
arr[size++]= m;
for(i = 0; i < size; i++)
printf(“%d\n”, arr[i]);
return 0;
Corrected Code
int isprime(int num)
int i;
int isprime = 1;
for(i = 2; i <= num / 2; i++)
{
Telegram: https://t.me/campusdrive
if(num % i == 0)
isprime = 0;
break;
return isprime;
22) Instructions: You are required to write the code. The code should be
logically/syntactically correct.
Problem: Write a program in C to display the table of a number and print the sum of all
the multiples in it.
Test Cases:
TestCase 1:
Input:
Expected Result Value:
5, 10, 15, 20, 25, 30, 35, 40, 45, 50
275
TestCase 2:
Input:
12
Expected Result Value:
Telegram: https://t.me/campusdrive
12, 24, 36, 48, 60, 72, 84, 96, 108, 120
660
#include <stdio.h>
int main()
// write your code here
Corrected Code
#include <stdio.h>
int main()
int n, i, value=0, sum=0;
printf("Enter number : ",n);
scanf("%d",&n);
for(i=1; i<=10; ++i)
value = n i;
printf("%d \t",value);
sum=sum+value;
printf("\nsum : %d",sum);
return 0;
}
Telegram: https://t.me/campusdrive
23)You have to find the security key from the data transmitted.
Input
The input consists of an integer data, representing the data to be transmitted.
Output
Print an integer representing the security key for the given data.
Example
Input
578378923
Output
783
Explanation
The repeated digits in the data are 7, 8 and 3. So, the security key is 783
#include <stdio.h>
#include <string.h>
int main()
char a[50];
int i, j, len, count=0;
scanf("%s",a);
strlen(a);
for(i=0;i<len;i++)
for(j=i+1;j<len;j++)
Telegram: https://t.me/campusdrive
{
if(a[i]=a[j])
printf("%c",a[i]);
break;
return 0;
Corrected Code
#include <stdio.h>
#include <string.h>
int main()
char a[50];
int i, j, len, count=0;
scanf("%s",a);
len = strlen(a);
for(i=0;i<len;i++)
for(j=i+1;j<len;j++)
if(a[i]==a[j])
{
Telegram: https://t.me/campusdrive
printf("%c",a[i]);
break;
return 0;
25) Question :
Function/method numberCount accepts three arguments-len, an integer representing the
length of input list. arr, a list of integers. and value, an integer value. It returns an integer
representing the sum of all the elements of arr that are equal to the given integer value
for example
len = 9, arr = [1,2,4,3,2,5,4,1,2], value = 2
function /method will return 6
function/method compiles successfully but fails to return the desired result for some/all
cases due to incorrect implementation. Your task is to correct the code so that it passes all
test cases.
int numberCount(int len, int arr, int value)
int count = 0;
for(int i =0 ; i < len -1 ; )
if(arr[i]==value)
count++;
}
Telegram: https://t.me/campusdrive
return sum;
Corrected Code
int numberCount(int len, int arr, int value)
int count = 0;
for(int i =0 ; i < len ; i++ )
if(arr[i]==value)
count++;
return count;
26) Question
Print the sum of fibonacci series nth term.
Series : 0, 1, 1, 2, 3, 5, 8, 13
Check syntax/logical error for the code
Example
Input
Output
33
Telegram: https://t.me/campusdrive
Explanation:
The sequence generated by the system will be 0, 1, 1, 2, 3, 5, 8, 13
The sum till 8th term will be 33.
Incorrect Code
#include <stdio.h>
int main()
int a=0,b=1,c,n,i=2,sum=0;
printf("n : ");
scanf("%d",&n);
while(i<=n)
c=a+b;
a=b;
b=c;
sum=sum+c;
i++;
printf("%d",sum);
return 0;
Corrected Code
#include <stdio.h>
int main()
{
Telegram: https://t.me/campusdrive
int
a=0,b=1,c,n,i=2,sum=1;
printf("n : ");
scanf("%d",&n);
while(i<n)
c=a+b;
a=b;
b=c;
sum=sum+c;
i++;
printf("%d",sum);
return 0;