0% found this document useful (0 votes)
26 views21 pages

Automata Pro - Set 2

The document provides a collection of sample AMCAT Automata programming questions along with their solutions in C and C++. It covers various topics such as matrix comparison, spiral printing of arrays, string manipulation, stack operations, and basic algorithms like checking for prime numbers and Fibonacci series generation. Each question includes code snippets and expected outputs to illustrate the solutions.

Uploaded by

sannjaysir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views21 pages

Automata Pro - Set 2

The document provides a collection of sample AMCAT Automata programming questions along with their solutions in C and C++. It covers various topics such as matrix comparison, spiral printing of arrays, string manipulation, stack operations, and basic algorithms like checking for prime numbers and Fibonacci series generation. Each question includes code snippets and expected outputs to illustrate the solutions.

Uploaded by

sannjaysir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Sample for AMCAT Automata PRO Questions

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:

Question 1: Program to check if two given matrices are identical

#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;
}

Question 2: Program to print the given 2D Array or Matrix in spiral form

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

Question 3: Write a program to calculate the sum of array elements equal


to a given integer.

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.

Question 4:Write a function printPattern(int num) that prints even or odd


numbers from an array based on whether num is even or odd.

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.

Question 5:Write a C program that checks whether a string of parentheses


is balanced or not using stack.

An opening symbol that has a corresponding closing symbol is considered


balanced parentheses, where the parentheses are correctly nested and the
opening and closing symbols are the same.

Valid balanced parentheses:

●​ [ [ { { ( ( ) ) } } ] ]
●​ [ ] [ ] [ ] ( ) { }
Invalid balanced parentheses:

●​ ( [ ) ]

●​ ( ( ( ) ] ) )

●​ [ { ( ) ]

In C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_SIZE 100

// Global variables for stack and top


char stack[MAX_SIZE];
int top = -1;

// Function to push a character onto the stack


void push(char data) {
if (top == MAX_SIZE - 1) {
printf("Overflow stack!\n");
return;
}
top++;
stack[top] = data;
}

// Function to pop a character from the stack


char pop() {
if (top == -1) {
printf("Empty stack!\n");
return ' ';
}
char data = stack[top];
top--;
return data;
}

// Function to check if two characters form a matching pair of parentheses


int is_matching_pair(char char1, char char2) {
if (char1 == '(' && char2 == ')') {
return 1;
} else if (char1 == '[' && char2 == ']') {
return 1;
} else if (char1 == '{' && char2 == '}') {
return 1;
} else {
return 0;
}
}

// Function to check if the expression is balanced


int isBalanced(char* text) {
int i;
for (i = 0; i < strlen(text); i++) {
if (text[i] == '(' || text[i] == '[' || text[i] == '{') {
push(text[i]);
} else if (text[i] == ')' || text[i] == ']' || text[i] == '}') {
if (top == -1) {
return 0; // If no opening bracket is present
} else if (!is_matching_pair(pop(), text[i])) {
return 0; // If closing bracket doesn't match the last opening bracket
}
}
}
if (top == -1) {
return 1; // If the stack is empty, the expression is balanced
} else {
return 0; // If the stack is not empty, the expression is not balanced
}
}

// Main function
int main() {
char text[MAX_SIZE];
printf("INPUT an expression in parentheses: ");
scanf("%s", text);

// Check if the expression is balanced or not


if (isBalanced(text)) {
printf("The expression is balanced.\n");
} else {
printf("The expression is not balanced.\n");
}
return 0;
}

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.

Question 6:Write a C program to sort a given stack using another stack.

#include <stdio.h>
#include <stdlib.h>

#define MAX_SIZE 100

// Global stack arrays and top variable


int stack[MAX_SIZE]; // Original stack
int sorted_Stack[MAX_SIZE]; // Stack used for sorting
int top = -1; // Top of the original stack

// Function to push an element onto the original stack


void push(int data) {
if (top == MAX_SIZE - 1) {
printf("Overflow stack!\n");
return;
}
top++;
stack[top] = data;
}

// Function to pop an element from the original stack


int pop() {
if (top == -1) {
printf("Empty Stack!\n");
return -1;
}
int data = stack[top];
top--;
return data;
}

// Function to sort the original stack


void sort_stack() {
int temp;
int sortedTop = -1; // Top of the stack used for sorting

// Sorting logic
while (top != -1) {
temp = pop();
while (sortedTop != -1 && sorted_Stack[sortedTop] < temp) {
push(sorted_Stack[sortedTop]);
sortedTop--;
}
sortedTop++;
sorted_Stack[sortedTop] = temp;
}

// Pushing sorted elements back to the original stack


while (sortedTop != -1) {
push(sorted_Stack[sortedTop]);
sortedTop--;
}
}

// Main function
int main() {
// Push elements onto the original stack
push(1);
push(5);
push(5);
push(2);
push(3);
push(8);

// Print original stack elements


printf("Original stack: ");
for (int i = 0; i <= top; i++) {
printf("%d ", stack[i]);
}
printf("\n");

// Sort the original stack


sort_stack();

// Print sorted stack elements


printf("Sorted stack: ");
for (int i = 0; i <= top; i++) {
printf("%d ", stack[i]);
}
printf("\n");
return 0;
}
Output:
Original stack: 1 5 5 2 3 8
Sorted stack: 1 2 3 5 5 8

Question 7:Write a C++ program to check if the INPUT string is a


palindrome. The string will contain only a and b.

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.

Question 8: Write a C++ program to check if the INPUT string contains an


equal number of a’s and b’s.
INPUT:-

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.

Question 9:Write a C++ program to reverse the INPUT string.

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

Question 10 :Write a C++ program to find the longest word in a sentence.

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

Question 11 :Write a C++ program to count the number of vowels and


consonants in the INPUT string. Consider both uppercase and lowercase
letters.
INPUT:-
Hello World
CODE:-
#include <iostream>
#include <string>
using namespace std;
void countVowelsConsonants(string s) {
int vowels = 0, consonants = 0;
for (char c : s) {
if (isalpha(c)) {
char lowerChar = tolower(c);
if (lowerChar == 'a' || lowerChar == 'e' || lowerChar == 'i' || lowerChar == 'o' ||
lowerChar == 'u') {
vowels++;
} else {
consonants++;
}
}
}
cout << "Vowels: " << vowels << "\n";
cout << "Consonants: " << consonants << "\n";
}
int main() {
string s;
cout << "Enter the string: ";
getline(cin, s);
countVowelsConsonants(s);
return 0;
}
OUTPUT
Vowels: 3
Consonants: 7

Question 12 :Write a C++ program to check if the first string is a substring


of the second string.

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

Yes , the first string is a substring of the second string.

Question 13: Print the First N Fibonacci Numbers.

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

Question 14 Convert a Number to Binary

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

Question 15:Write a C program to determine if a given number is prime.

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;

for (int i = 1; i <= n; i++) {


for (int j = 1; j <= i; j++) {
cout << j;
}
cout << endl;
}

return 0;
}
OUTPUT
1
12
123
1234
12345

Question 16 .Write a C program to check if a number is an Armstrong


number (sum of its digits raised to the power of its length equals the
number itself).

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

Question 18 :Write a C program to convert a given temperature from


Celsius to Fahrenheit.

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

Question 19 :Write a C++ program to compute the factorial of a given


number.
Formula:
n!=n×(n−1)×(n−2)×...×1
INPUT:-
5
CODE:-
#include <iostream>
using namespace std;
int main() {
int n, fact = 1;
cin >> n;
for (int i = 1; i <= n; i++) {
fact *= i;
}
cout << fact;
return 0;
}
OUTPUT
120

Question 20: Write a C program to reverse a given integer.

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

Question 21 Write a C program to print a right-angled triangle pattern of


stars for a given N.
INPUT:-
5
CODE:-
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
printf("*");
}
printf("\n");
}
return 0;
}
OUTPUT
*
**
***
****
*****

Question 22 :Write a C program to generate and print all substrings of a


given string.

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;

bool isPrime(int num) {


if (num < 2) return false;
for (int i = 2; i * i <= num; i++) {
if (num % i == 0) return false;
}
return true;
}

int main() {
int n;
cin >> n;

while (!isPrime(++n));

cout << n;
return 0;
}

OUTPUT
11

Question 24 Write a C++ program to check if the INPUT string contains


balanced parentheses. A string is balanced if:
●​ Every opening parenthesis ( has a corresponding closing parenthesis ).

●​ The parentheses are properly nested.

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

Question 25 :Write a C program to find the digital root of a number. The


digital root is obtained by repeatedly summing the digits until a single-digit
number is obtained.

INPUT:-
9875
CODE:-
#include <stdio.h>

int main() {
int n, sum;
scanf("%d", &n);

while (n >= 10) {


sum = 0;
while (n > 0) {
sum += n % 10;
n /= 10;
}
n = sum;
}

printf("%d", n);
return 0;
}
OUTPUT
2

You might also like