Automata Fi Questions
Automata Fi Questions
Automata Fi Questions
#include<stdio.h>
int main()
{
int n;
scanf("%d", &n);
int i = n;
while(i >= 0)
{
printf("%d ", i);
i--;
}
return 0;
}
• Output: Infinite loop
• Answer: Error - Logical error
• unsigned int i = n; unsigned integer ranges
from 0 to 4294967295 , which will be taken in
the cyclic order.So it will keep repeating in a
cyclic way. The loop will never be terminated.
So it should be written as int i = n;
•
2) Find the output
#include<stdio.h>
int main()
{
unsigned int x=5;
if(x>-1)
printf("Hello");
else
printf("Ok");
}
3) 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
8
Output
33
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;
}
Correct Code:
#include <stdio.h>
int main()
{
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;
}
Q4. 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
Incorrect Code:
#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++)
{
if(a[i]=a[j])
{
printf("%c",a[i]);
break;
}
}
}
return 0;
}
Correct 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])
{
printf("%c",a[i]);
break;
}
}
}
return 0;
}
5) Fix the error, recompile and match against the
output provided.
Output : Welcome to “KIIT”
Incorrect Code
#include<stdio.h>
int main()
{
printf("Welcome to \“KIIT");
return 0;
}
Corrected Code
#include <stdio.h>
int main(void)
{
printf("Welcome to \“KIIT\"");
return 0;
}
6) 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);
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);