Amcat Automata Questions PDF
Amcat Automata Questions PDF
Easy:-
Program:
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a,b,gcd;
printf(“\nEnter two numbers : “);
scanf(“%d %d”,&a,&b);
int i;
for(i = 1; i <= a && i <= b; i++)
{
if((a % i == 0) && (b % i == 0))
{
gcd = i;
}
}
printf(“\nGCD of %d and %d is %d “,a,b,gcd);
printf(“\n”);
return 0;
}
#include <stdio.h>
int main()
{
int a, b;
printf(“Enter two numbers: “);
scanf(“%d %d”, &a, &b);
Q.3. Program to find GCD of two numbers using Command Line Arguments.
Program:
#include<stdio.h>
inta,b,small,i;
a=atoi(y[1]);
b=atoi(y[2]);
small=a>b?b:a;
for(i=small;i>=1;i–)
{
if((a%i==0)&&(b%i==0))
printf(“%d”,i);
break;
}}
return 0;
Program:
#include <stdio.h>
int main()
{
int a, b, lcm;
printf(“\nEnter two numbers: “);
scanf(“%d %d”, &a, &b);
lcm = (a > b) ? a : b;
while(1)
{
if( lcm % a == 0 && lcm % b == 0 )
{
printf(“\nLCM of %d and %d is %d\n”, a, b,lcm);
break;
}
++lcm;
}
return 0;
}
if (a > b)
return GCD(a-b, b);
return GCD(a, b-a);
}
int main()
{
int a,b;
printf(“\nEnter two numbers : “);
scanf(“%d %d”,&a,&b);
printf(“\nLCM of %d and %d is %d \n”, a, b, LCM(a, b));
return 0;
}
Program:
// C program to check if the given number is prime or not using for loop
#include<stdio.h>
int main()
{
int n,i;
printf(“\nEnter the number : “);
scanf(“%d”,&n);
Output:
19 is a prime number.
(A number is an Armstrong number when the sum of nth power of each digit
is equal to the number itself.)
Example:
#include
#include
int main()
{
int number, temp, remainder, result = 0, n = 0 ;
temp = number;
while (temp != 0)
{
temp /= 10;
++n;
}
temp = number;
while (temp != 0)
{
remainder = temp%10;
result += pow(remainder, n);
temp /= 10;
}
if(result == number)
printf(“%d is an Armstrong number\n”, number);
else
printf(“%d is not an Armstrong number\n”, number);
return 0;
}
( A strong number is a number in which the sum of the factorial of the digits
is equal to the number itself.)
Example:
Program:
#include<stdio.h>
int main()
{
int n,i;
int fact,rem;
printf(“\nEnter a number : “);
scanf(“%d”,&n);
printf(“\n”);
int sum = 0;
int temp = n;
while(n)
{
i = 1,fact = 1;
rem = n % 10;
return 0;
}
(An automorphic number is a number whose square ends with the number
itself.)
Program:
#include<stdio.h>
bool isAutomorphic(int N)
{
int sq = N * N;
while (N > 0)
{
if (N % 10 != sq % 10)
return false;
return true;
}
int main()
{
//Fill the code
int N;
scanf(“%d”,&N);
isAutomorphic(N) ? printf(“Automorphic”) : printf(“Not Automorphic”);
return 0;
}
Output:
(An abundant number is a number for which the sum of its proper divisors is
greater than the number itself.)
Example:
Program:
#include<stdio.h>
int main()
{
//fill the code
int num;
int temp;
scanf(“%d”,&num);
int sum = 0;
for(int i = 1; i < num; i++)
{
if(num % i == 0)
{
sum = sum + i;
}
}
if(num < sum)
printf(“Abundant Number”);
else
printf(“Not Abundant Number”);
return 0;
}
Program:
#include <stdio.h>
int main()
{
int a, b, i, flag;
printf(“\nEnter start value : “);
scanf(“%d”,&a);
printf(“\nEnter end value : “);
scanf(“%d”,&b);
printf(“\nPrime Numbers between %d and %d : “, a, b);
while (a < b)
{
flag = 0;
for(i = 2; i <= a/2; ++i)
{
if(a % i == 0)
{
flag = 1;
break;
}
}
if (flag == 0)
printf(“%d “, a);
++a;
}
printf(“\n”);
return 0;
}
(Fibonacci series is a series in which each number is the sum of the last two
preceding numbers. The first two terms of a Fibonacci series are 0 and 1.)
Example:
0+1=1
1+1=2
1+2=3
2+3=5
3+5=8
5 + 8 = 13
8 + 13 = 21
13 + 21 = 34
21 + 34 = 55
34 + 55 = 89
55 + 89 = 144
144 + 89 = 233
Program:
#include<stdio.h>
int main()
int sum = 0, n;
int a = 0;
int b = 1;
scanf("%d", &n);
while(sum <= n)
a = b; // swap elements
b = sum;
return 0;
Program:
#include
int main()
{
int i, j,n;
scanf(“%d”,&n);
for(i = 0; i <n; i++)
{
for(j = 0; j < n; j++)
{
printf(“*”);
}
printf(“\n”);
}
return 0;
}
Output:
*******
*******
*******
*******
*******
*******
Program:
*******
* *
* *
* *
* *
* *
*******
Medium:-
Q.15. Program to print all the Armstrong numbers between the two intervals.
Program:
#include <stdio.h>
#include <math.h>
int main()
{
int start, end, i, temp1, temp2, remainder, n = 0, result = 0;
printf(“Enter start value and end value : “);
scanf(“%d %d”, &start, &end);
printf(“\nArmstrong numbers between %d an %d are: “, start, end);
for(i = start + 1; i < end; ++i)
{
temp2 = i;
temp1 = i;
while (temp1 != 0)
{
temp1 /= 10;
++n;
}
while (temp2 != 0)
{
remainder = temp2 % 10;
result += pow(remainder, n);
temp2 /= 10;
}
if (result == i) {
printf(“%d “, i);
}n = 0;
result = 0;
}
printf(“\n”);
return 0;
}
Output:
Q. 16. Program to convert the given binary number into decimal number.
Example:
1 * 20 = 1
1 * 21 = 2
1 * 22 = 4
1 * 23 = 6
Decimal number = 1 + 2 + 4 + 6 = 15
#include
#include
int main()
{
long int n;
printf(“Enter a binary number: “);
scanf(“%ld”, &n);
printf(“\nDecimal number : %d\n “, binary_to_decimal(n));
return 0;
}
Output:
7 / 2 = 3, rem = 1
3 / 2 = 1, rem = 1
1 / 2 = 0, rem = 1
Program:
#include <stdio.h>
for(i = 1; n != 0; i = i * 10)
remainder = n % 2;
n /= 2;
binary += remainder * i;
return binary;
int main()
int n;
printf("Enter a decimal number: ");
scanf("%d", &n);
return 0;
Output:
25 / 8 = 3, rem = 1
3 / 8 = 0, rem = 3
Program:
#include <stdio.h>
#include <math.h>
return 0;
}
while (decimal != 0)
{
octal += (decimal % 8) * i;
decimal /= 8;
i *= 10;
}
return octal;
}
Output:
7 * 80 = 7
0 * 81 = 0
9 * 82 = 576
1 * 83 = 512
512 + 576 + 0 + 7 = 1095. The decimal equivalent of octal number 1907 is 1095.
Program:
#include
#include
while(octal != 0)
{
decimal += (octal%10) * pow(8,i); // multiplying with powers of 8
++i;
octal/=10; // Divide by 10 to make it as decimal
}
i = 1;
return decimal;
}
int main()
{
int octal;
return 0;
}
Output:
Output: 97531
Program:
#include <stdio.h>
int main()
{
int n, rev = 0, rem;
printf(“\nEnter a number : “);
scanf(“%d”, &n);
printf(“\nReversed Number : “);
while(n != 0)
{
rem = n%10;
rev = rev*10 + rem;
n /= 10;
}
printf(“%d\n”, rev);
return 0;
}
Program:
/* C program for Palindrome pyramid pattern printing using numbers */
#include<stdio.h>
#include<stdlib.h>
int main()
{
int i,j,k,l,n;
printf(“Enter the number of rows : “);
scanf(“%d”,&n);
for(i = 1; i <= n; i++)
{
for(k = 1; k <= i; k++)
{
printf(“%d “,k);
}
for(l = i-1; l >= 1; l–)
{
printf(“%d “,l);
}
printf(“\n”);
}
return 0;
}
Output:
1
121
12321
1234321
123454321
Program:
Output:
A
ABA
ABCBA
ABCDCBA
ABCDEDCBA
Average:-
Q.23. Remove vowels from a string and return the string with consonants.
Q.24. Program to find all the triplets with the given sum.
Example: Consider the array: arr [] = {0, -1, 2, -3, 1}. The given sum is -2. In the
given array, the triplets with sum = -2 are {0, -3, 1} and {-1, 2, -3}.
Program:
// C program to find all the triplets with the given sum
#include
void find_all_triplets(int arr[], int n, int sum)
{
for (int i = 0; i < n – 2; i++)
{
for (int j = i + 1; j < n – 1; j++)
{
for (int k = j + 1; k < n; k++)
{
if (arr[i] + arr[j] + arr[k] == sum)
{
printf(“%d,%d,%d\n”,arr*i+,arr*j+,arr*k+);
}}}}}
int main()
{
int n, sum;
printf(“\nEnter the number of elements : “);
scanf(“%d”,&n);
int arr[n];
printf(“\nInput the array elements : “);
for(int i = 0; i < n; i++)
{
scanf(“%d”,&arr*i+);
}
printf(“\nEnter the sum value : “);
scanf(“%d”,&sum);
printf(“\nThe triplets are \n “);
find_all_triplets(arr, n, sum);
return 0;
}
Output:
Q.25. Program to find all the triplets with the given sum using sorting
technique.
Program:
// C++ program to find all the triplets with the given sum
#include <bits/stdc++.h>
using namespace std;
Q.26. Program to find all the triplets with the given sum using hashing
technique.
Program:
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n, sum;
cout << “\nEnter the number of elements : “;
cin >> n;
int arr[n];
cout << “\nInput the array elements : “;
for(int i = 0; i < n; i++)
{
cin >> arr[i];
}
cout << “\nEnter the sum value : “;
cin >> sum;
cout << “\nThe triplets are \n “;
find_all_triplets(arr, n, sum);
return 0;
}
#include <stdio.h>
#include <string.h>
#define MAX 50
int main()
{
char str[MAX];
int i, len;
int frequency[26];
len = strlen(str);
return 0;
}
Output:
Frequency of characters:
d 1
e 1
h 1
l 1
o 1
r 1
w 1
Program:
int main()
{
double a, b, c, discriminant, root1, root2, realPart, imaginaryPart;
discriminant = b*b-4*a*c;
Output:
Program:
#include
int check_palindrome(int n)
{
int div = 1;
while (n / div >= 10)
div *= 10;
while (n != 0)
{
int first = n / div;
int last = n % 10;
// If first and last digits are not same then return false
if (first != last)
return -1;
11111
4545
687
95
Program:
printf(“\n”);
}
return 0;
}
Output:
**
***
****
*****
*****
****
***
**
*
Q.31. Program for Solid Half Diamond pattern printing using stars.
Program:
#include
int main()
{
int i, j, space, k = 0, n;
printf(“\nEnter the number of rows : “);
scanf(“%d”,&n);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n-i;j++)
{
printf(” “);
}
for(int j=1;j<=i;j++)
{
printf(“*”);
}
printf(“\n”);
}
for(int i=n-1;i>0;i–)
{
for(int j=1;j<=n-i;j++)
{
printf(” “);
}
for(int j=1;j<=i;j++)
{
printf(“*”);
}
printf(“\n”);
}
}
Output:
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
Program:
Output:
1
121
12321
1234321
123454321
Program:
Output:
A
ABA
ABCBA
ABCDCBA
ABCDEDCBA
Program:
#include <stdio.h>
int main()
{
int i, j, k, space, n;
scanf(“%d”, &n);
printf(” “);
for (i=1; i<=n; i++)
{
printf(” “);
}
return 0;
}
Output:
1 2 1
12321
1234321
123454321
Q.35. Program to print palindrome pyramid pattern using numbers & stars.
Program:
#include<stdio.h>
int main()
{
int n, i, j, space, count = 1, num = 0, star = 8;
scanf(“%d”, &n);
space = n;
for (i = 1; i <= n; i++)
{
for (j = 1; j <= star; j++)
if(i + j <= star + 1)
printf(“*”);
num++;
for (j = 1; j <= i; j++)
{
printf(“%d”, num);
if (i > 1 && count < i)
{
printf(“*”);
count++;
}
}
for (j = 1; j <= star; j++)
if(i + n <= j + n)
printf(“*”);
printf(“\n”);
space–;
count = 1;
}
return 0;
}
Output:
********1********
*******2*2*******
******3*3*3******
*****4*4*4*4*****
****5*5*5*5*5****
***6*6*6*6*6*6***
Q.36. Find the number of all possible triplets in the array that can form the
triangle (condition is a + b > c).
Program:
#include<stdio.h>
int main()
scanf(“%d”,&n);
for(i=0;i<n;i++)
scanf(“%d”,&arr*i+);
for(i=0;i<n-2;i++)
a=arr[i];
for(j=i+1;j<n-1;j++)
b=arr[j];
for(k=j+1;k<n;k++)
c=arr[k];
printf(“%d %d %d “,a,b,c);
printf(“Yes\n”);
else
{
printf(“%d %d %d “,a,b,c);
printf(“No\n”);
return 0;
Q.37. Print all the prime numbers which are below the given number
separated by comma.
Input: 50
Program:
#include"stdio.h"
int main()
int n,i,j,ct=0;
scanf(“%d”,&n);
for(i=2;i<=n;i++)
ct=0;
for(j=2;j<i;j++)
if(i%j==0)
{ ct=1; break; }
if(ct==0)
if(i>2)
printf(“, “);
printf(“%d”,i);
return 0;
Output: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47
Program:
#include”stdio.h”
int main()
int m, n, i, gcd;
{
// Checks if i is factor of both integers
gcd = i;
return 0;
Program:
#include “stdio.h”
void main()
long n, t, digit;
int sum = 0;
scanf(“%ld”, &n);
t = n;
while (n > 0)
digit = n % 10;
n /= 10;
}
printf(“%d”, sum);
Program:
#include<stdio.h>
int main()
{
int i, j, k, l=1, N, d, r, count=0;
scanf(“%d”, &N);
k=1;
d=i%2;
r=l+i-1;
for(j=0;j<i;j++)
if(d==0)
printf(“%d”,r);
r–;
if(k<i)
printf(“*”);
k=k+1;
l++;
continue;
printf(“%d”,l);
l++;
if(k<i)
printf(“*”);
k=k+1;
printf(“\n”);
}
return 0;
Output:
3*2
4*5*6
10*9*8*7
11*12*13*14*15
Q.41. Mooshak the mouse has been placed in a maze. There is a huge chunk
of cheese somewhere in the maze. The maze is represented as a two-
dimensional array of integers, where 0 represents walls.1 represents paths
where Mooshak can move and 9 represents the huge chunk of cheese.
Mooshak starts in the top left corner at 0.
Write a method is Path of class Maze Path to determine if Mooshak can
reach the huge chunk of cheese. The input to is Path consists of a two-
dimensional array and for the maze matrix. the method should return 1 if
there is a path from Mooshak to the cheese and 0 if not Mooshak is not
allowed to leave the maze or climb on walls.
Example: 8 by 8(8*8) matrix maze where Mooshak can get the cheese.
Test Case 1:
Input: [[0,0,0],[9,1,1],[0,1,1]]
Explanation: The piece of cheese is placed at(1,0) on the grid Mooshak can
move from (0,0) to (1,0) to reach it or can move from (0,0) to (0,1) to (1,1) to
(1,0)
Program:
#include”stdlib.h”
#include”stdio.h”
int main()
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
printf(“input\n”);
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
printf(“%d “,maze*i+*j+);
printf(“\n”);
printf(“%d”, path(maze));
return 0;
{ for(int j=0;j<3;j++)
{ if((m[i][j]==1))
int path(m[3][3]);
if(m[i][j]==0)
return 0;
if(m[i][j]==9)
return 1;
}
Output:
10111001
10001111
10000000
10109011
11101001
10101101
10000101
11111111
Q.42. Program to print all distinct elements of given input arrays. Also print
the total of the distinct elements.
Input:
Arr1 = {1,2,3,4,5}
Arr 2 = {2,6,8,10}
Program:
#include”stdio.h”
int Not_common (int *arr1, int *arr2, int l1, int l2)
flag1=0;
if(arr1[i] == arr2[j])
flag1=1; break;
}}
if(flag1 ==0)
count++;
printf(“%d,”, arr1[i]);
flag1=0;
if(arr2[i] == arr1[j])
{
flag1=1;
break;
}}
if(flag1 ==0)
count++;
printf(“%d,”, arr2*i+);
}}
return count;
int main()
int arr1[10],arr2[10];
scanf(“%d”, &arr1*i+);
scanf(“%d”, &arr2*i+);
return 0;
}
Program:
#include
int t;
t = *p1;
*p1 = *p2;
*p2 = t;
int *t;
return arr;
}
int main(){
int n, *p;
cin >> n;
int arr[n];
p = swapArr(arr, n);
return 0;
}
Q.44. Print the below pattern
11
121
1331
14641
Program:
#include
void printPascal(int n)
int x = n;
x--;
int C = 1;
C = C * (line - i) / i;
}
int main()
int x;
cin >> x;
printPascal(x);
return 0;
Output:
11
121
1331
14641
Q.45. Print the below pattern where Input n=6.
1111112
3222222
3333334
5444444
5555556
7666666
Program:
#include
int main()
int num;
cin>>num;
patternprint(num);
return 0;
}
void patternprint(int num)
int x = 1;
{
if(i % 2 == 0)
cout << x;
cout << x + 1;
else
cout << x + 1;
cout << x;
x++;
}}
Output:
1111112
3222222
3333334
5444444
5555556
7666666
Q.46. Programming Pattern to Print 2*N Number of rows for input Pattern?
44
555
6666
555
44
Program:
#include <iostream>
int main()
int n=4,num=n-1;
for(int i=1;i<=n;i++)
for(int j=1;j<=i;j++)
cout<<num;
num++;
cout<<endl;
num--;
for(int i=n;i>=1;i--)
for(int j=1;j<=i;j++)
cout<<num;
num--;
cout<<endl;
return 0;
Program:
#include<iostream>
int main(){
int n=4,num=1,i=1,space=0,k=1,number=n;
for(i=0;i<n;i++)
for(int j=1;j<=space;j++)
cout<<"-";
}
for(int m=1;m<2*n-space;m++)
if(m%2==0)
cout<<"*";
else
cout<<num++;
cout<<"*";
for(int l=1;l<2*n-space;l++)
if(l%2==0)
cout<<"*";
else
cout<<k+number*number;
k++;
number--;
space=space+2;
cout<<endl;
}
return 0;
Output:
1*2*3*4*17*18*19*20
- -5*6*7*14*15*16
- - - -8*9*12*13
- - - - - -10*11
Q.48. Find the number of occurrences of input num2 in input num1 and
such that 0 <= num1 <= 99999999 and 0 <= num2 <= 9
Example:
Output: 3
Test Case 1:
Input:
1222212
Expected Output:
5
Test Case 2:
Input:
1001010
Expected Output:
Q.49. Print all prime no which are below then given input no separated by
comma .
Input:
20
Output:
2,3,5,7,11,13,17,19
Program:
<iostream>
int main() {
int n=20,count,k=0,arr[10];
for(int i=2;i<n;i++)
{count=0;
for(int j=2;j<i;j++)
{
if(i%j==0)
count++;
break;
}}
if(count==0)
arr[k]=i;
k++;
}}
for(int i=0;i<k-1;i++)
if(i!=k-2)
cout<<arr[i]<<",";
else
cout<<arr[i];
return 0;
Q.50. You have 3 dice , you need to take input from the user what’s the sum
of three dice he wants .Program to find all the possible outcome of input.
Example:
Input- 5
Output- 6
Explanation- (1,2,2) (2,2,1 ) (2,1,2) (1,1,3) (1,3,1) (3,1,1)
Input- 2
Output- 0
Explanation- Minimum sum value on the dice is 3 , so it not possible to come
sum equal to 2
Input- 3
Output- 1
Explanation- (1,1,1)
Program:
#include<iostream.h>
int main()
int x,count=0;
cin>>x;
for(int i=1;i<=6;i++)
for(int j=1;j<=6;j++)
for(int k=1;k<=6;k++)
if(x==(i+j+k))
count++;
}
}
cout<<count;
return 0;
Program:
#include <stdio.h>
int main()
temp=i;
num=0;
while(temp!=0)
rem=(temp%10);
num+=rem*rem*rem;
temp/=10;
if(i==num)
printf("%d ",i);
return 0;
Program:
<stdio.h>
int main()
{
flag=0;
if(i%j==0)
flag=1;
break;
if(flag==0)
printf("%d ",i);
return 0;
Algorithm
Program:
main()
{
int num,i=1,j,k;
printf("\nEnter a number:");
scanf("%d",&num);
while(i<=num)
k=0;
j=1;
if(i%j==0)
k++;
j++;
if(k==2)
i++;
}
Q.54. Write a function that accepts a sentence as a parameter, and returns
the same with each of its words reversed. The returned sentence should have
1 blank space between each pair of words. Demonstrate the usage of this
function from a main program.
Example:
Program:
#include <iostream>
#include<string.h>
cout<< arr[i];
cout<<" ";
int main() {
int p;
p = strlen(st);
int startIndex = 0;
//int wordCount = 0;
for(int j=0;j<=p;j++)
if(j == p) {
// wordCount++;
wordCount++;
startIndex = j + 1;
return 0;
Q.55. Given an array of integers, write a function that returns true if there is
a triplet (a, b, c) that satisfies a2 + b2 = c2.
Example:
Output: True
Program:
#include <iostream>
if (x == y + z || y == x + z || z == x + y)
return true;
int main()
return 0;