Automata Pro - Set 2
Automata Pro - Set 2
The most commonly asked AMCAT Automata questions with their answers are discussed
here. These questions of easy and hard difficulty levels. The commonly asked AMCAT
Automata Questions are discussed below:
#include <stdio.h>
#define N 4
// This function returns 1 if A[][] and B[][] are identical. Otherwise, it returns 0
int areSame(int A[][N], int B[][N])
{
int i , j;
for (i = 0; i < N; i++)
for (j = 0; j < N; j++)
if (A[i][j] != B[i][j])
return 0;
return 1;
}
int main()
{
int A[N][N] = { {1, 1, 1, 1},
{2, 2, 2, 2},
{3, 3, 3, 3},
{4, 4, 4, 4}};
int B[N][N] = { {1, 1, 1, 1},
{2, 2, 2, 2},
{3, 3, 3, 3},
{4, 4, 4, 4}};
if (areSame(A, B))
printf("Matrices are identical");
else
printf("Matrices are not identical");
return 0;
}
INPUT:
123456
7 8 9 10 11 12
13 14 15 16 17 18
In C
#include <stdio.h>
#define R 3
#define C 6
void spiralPrint(int m, int n, int a[R][C])
{
int i, k = 0, l = 0;
/* k - starting row index
m - ending row index
l - starting column index
n - ending column index
i - iterator */
while (k < m && l < n)
{
/* Print the first row from the remaining rows */
for (i = l; i < n; ++i)
{
printf("%d ", a[k][i]);
}
k++;
/* Print the last column from the remaining columns */
for (i = k; i < m; ++i)
{
printf("%d ", a[i][n-1]);
}
n--;
/* Print the last row from the remaining rows */
if ( k < m)
{
for (i = n-1; i >= l; --i)
{
printf("%d ", a[m-1][i]);
}
m--;
}
/* Print the first column from the remaining columns */
if (l < n)
{
for (i = m-1; i >= k; --i)
{
printf("%d ", a[i][l]);
}
l++;
}
}
}
/* Driver program for the above functions */
int main()
{
int a[R][C] = { {1, 2, 3, 4, 5, 6},
{7, 8, 9, 10, 11, 12},
{13, 14, 15, 16, 17, 18}
};
spiralPrint(R, C, a);
return 0;
}
Output:
1 2 3 4 5 6 12 18 17 16 15 14 13 7 8 9 10 11
INPUT:
Length of the array.
Array elements.
Integer to match.
In C
#include <stdio.h>
int main() {
int length, match;
scanf("%d", &length);
int arr[length];
for(int i = 0; i < length; i++) {
scanf("%d", &arr[i]);
}
scanf("%d", &match);
int sum = 0;
for(int i = 0; i < length; i++) {
if(arr[i] == match) {
sum += arr[i];
}
}
printf("%d\n", sum);
return 0;
}
Output:
Sum of matching elements.
INPUT:
An integer num and an array.
#include <stdio.h>
void printPattern(int num, int arr[], int size)
{
for(int i = 0; i < size; i++)
{
if(num % 2 == 0 && arr[i] % 2 == 0)
{
printf("%d ", arr[i]);
}
else if(num % 2 != 0 && arr[i] % 2 != 0)
{
printf("%d ", arr[i]);
}
}
}
int main()
{
int num; scanf("%d", &num);
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int size = sizeof(arr) / sizeof(arr[0]);
printPattern(num, arr, size);
return 0;
}
Output:
Relevant numbers from the array.
● [ [ { { ( ( ) ) } } ] ]
● [ ] [ ] [ ] ( ) { }
Invalid balanced parentheses:
● ( [ ) ]
● ( ( ( ) ] ) )
● [ { ( ) ]
In C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Main function
int main() {
char text[MAX_SIZE];
printf("INPUT an expression in parentheses: ");
scanf("%s", text);
Output:
INPUT an expression in parentheses: {[])
The expression is not balanced.
-----------------------------------------
INPUT an expression in parentheses: ((()))
The expression is balanced.
-----------------------------------------
INPUT an expression in parentheses: ())
The expression is not balanced.
-----------------------------------------
INPUT an text of parentheses: ([]){}[[(){}]{}]
The expression is balanced.
-----------------------------------------
INPUT an expression in parentheses: [(]))
The expression is not balanced.
#include <stdio.h>
#include <stdlib.h>
// Sorting logic
while (top != -1) {
temp = pop();
while (sortedTop != -1 && sorted_Stack[sortedTop] < temp) {
push(sorted_Stack[sortedTop]);
sortedTop--;
}
sortedTop++;
sorted_Stack[sortedTop] = temp;
}
// Main function
int main() {
// Push elements onto the original stack
push(1);
push(5);
push(5);
push(2);
push(3);
push(8);
INPUT:-
ababa
CODE:-
#include <iostream>
#include <string>
using namespace std;
bool isPalindrome(string s) {
int n = s.length();
for (int i = 0; i < n / 2; i++) {
if (s[i] != s[n - 1 - i]) {
return false;
}
}
return true;
}
int main() {
string s;
cout << "Enter the string: ";
cin >> s;
if (isPalindrome(s)) {
cout << "Palindrome\n";
} else {
cout << "Not a palindrome\n";
}
return 0;
}
OUTPUT
Palindrome.
abbba
CODE:-
#include <iostream>
#include <string>
using namespace std;
bool isEqual(string s) {
int countA = 0, countB = 0;
for (char c : s) {
if (c == 'a') countA++;
if (c == 'b') countB++;
}
return countA == countB;
}
int main() {
string s;
cout << "Enter the string: ";
cin >> s;
if (isEqual(s)) {
cout << "Equal\n";
} else {
cout << "Not Equal\n";
}
return 0;
}
OUTPUT
Not Equal.
INPUT:-
Hello
CODE:-
#include <iostream>
#include <string>
using namespace std;
string reverseString(string s) {
int n = s.length();
for (int i = 0; i < n / 2; i++) {
swap(s[i], s[n - 1 - i]);
}
return s;
}
int main() {
string s;
cout << "Enter the string: ";
cin >> s;
cout << "Reversed string: " << reverseString(s) << endl;
return 0;
}
OUTPUT
Olleh
INPUT:-
I love programming in C++
CODE:-
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
string findLongestWord(string s) {
stringstream ss(s);
string word, longestWord;
while (ss >> word) {
if (word.length() > longestWord.length()) {
longestWord = word;
}
}
return longestWord;
}
int main() {
string s;
cout << "Enter the sentence: ";
getline(cin, s);
cout << "Longest word: " << findLongestWord(s) << endl;
return 0;
}
OUTPUT
Programming
INPUT:-
abc defabcghi
CODE:-
#include <iostream>
#include <string>
using namespace std;
int main() {
string s1, s2;
cout << "Enter the first string: ";
cin >> s1;
cout << "Enter the second string: ";
cin >> s2;
if (s2.find(s1) != string::npos) {
cout << "Yes,the first string is a substring of the second string." << endl;
} else {
cout << "No,the first string is not a substring of the second string." << endl;
}
return 0;
}
OUTPUT
INPUT:-
5
CODE:-
#include <stdio.h>
int main() {
int n, a = 0, b = 1, temp;
scanf("%d", &n);
for(int i = 0; i < n; i++) {
printf("%d ", a);
temp = a + b;
a = b;
b = temp;
}
return 0;
}
OUTPUT
01123
INPUT:-
10
CODE:-
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
char binary[32];
int index = 0;
while(n > 0) {
binary[index++] = (n % 2) + '0';
n /= 2;
}
for(int i = index - 1; i >= 0; i--) {
printf("%c", binary[i]);
}
return 0;
}
OUTPUT
1010
INPUT:-
7
CODE:-
#include <stdio.h>
int main() {
int n, isPrime = 1;
scanf("%d", &n);
if (n < 2) isPrime = 0;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) {
isPrime = 0;
break;
}
}
printf(isPrime ? "YES" : "NO");
return 0;
}
OUTPUT
YES IT IS A PRIME NUMBER
QUESTION 10
Write a C++ program to print a triangle pattern with numbers for a given N.
INPUT:-
5
CODE:-
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
return 0;
}
OUTPUT
1
12
123
1234
12345
INPUT:-
153
CODE:-
#include <stdio.h>
#include <math.h>
int main() {
int n, sum = 0, temp, len = 0;
scanf("%d", &n);
temp = n;
while (temp > 0) {
len++;
temp /= 10;
}
temp = n;
while (temp > 0) {
sum += pow(temp % 10, len);
temp /= 10;
}
printf(sum == n ? "YES" : "NO");
return 0;
}
OUTPUT
YES , It’s a Armstrong number .
Question 17: Write a C++ program to find the majority element in an array
(an element that appears more than N/2 times). If no such element exists,
print -1
INPUT:-
5
33423
CODE:-
#include <iostream>
using namespace std;
int main() {
int n, count = 0, candidate = -1;
cin >> n;
int arr[n];
for (int i = 0; i < n; i++) {
cin >> arr[i];
if (count == 0) candidate = arr[i];
count += (arr[i] == candidate) ? 1 : -1;
}
count = 0;
for (int i = 0; i < n; i++)
if (arr[i] == candidate) count++;
cout << (count > n / 2 ? candidate : -1);
return 0;
}
OUTPUT
3
Formula:
F=(C×9/5)+32
INPUT:-
25
CODE:-
#include <stdio.h>
int main() {
float c;
scanf("%f", &c);
float f = (c * 9/5) + 32;
printf("%.2f", f);
return 0;
}
OUTPUT
77.00 Fahrenheit
INPUT:-
1234
CODE:-
#include <stdio.h>
int main() {
int n, rev = 0;
scanf("%d", &n);
while (n > 0) {
rev = rev * 10 + (n % 10);
n /= 10;
}
printf("%d", rev);
return 0;
}
OUTPUT
4321
INPUT:-
abc
CODE:-
#include <stdio.h>
#include <string.h>
int main() {
char s[100];
scanf("%s", s);
int len = strlen(s);
for (int i = 0; i < len; i++) {
for (int j = i; j < len; j++) {
for (int k = i; k <= j; k++) {
printf("%c", s[k]);
}
printf("\n");
}
}
return 0;
}
OUTPUT
a
ab
abc
b
bc
c
Question 23 Write a C++ program to find the smallest prime number that
is greater than a given number N.
INPUT:-
10
CODE:-
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
while (!isPrime(++n));
cout << n;
return 0;
}
OUTPUT
11
INPUT:-
(()(()
CODE:-
#include <iostream>
#include <stack>
using namespace std;
bool isBalanced(string s) {
stack<char> stk;
for (char c : s) {
if (c == '(') {
stk.push(c);
} else if (c == ')') {
if (stk.empty()) {
return false;
}
stk.pop();
}
}
return stk.empty();
}
int main() {
string s;
cout << "Enter the string: ";
cin >> s;
if (isBalanced(s)) {
cout << "Balanced\n";
} else {
cout << "Not Balanced\n";
}
return 0;
}
OUTPUT
Not Balanced
INPUT:-
9875
CODE:-
#include <stdio.h>
int main() {
int n, sum;
scanf("%d", &n);
printf("%d", n);
return 0;
}
OUTPUT
2