Coding Round Question & Answers
Coding Round Question & Answers
Coding Round Question & Answers
1) Find the distinct elements in a given array. (Assume size of an array n<=20)
Sample Input:
9 = size of an array
2 3 4 5 6 1 2 3 4 = array elements
Sample Output:
234561
Program:
// C program to print all distinct elements in a given array
#include
void distict_elements(int a[], int n);
int main()
{
int size_array, i, arr[20];
// Get the array size
scanf(“%d”, &size_array)
// Get the array elements
for(i=0; i<size_array; i++)
{
scanf(“%d”, &arr[i]);
}
Input:
5
86927
Output:
26789
98762
Program:
// C program to sort the given array elements in ascending and descending order
#include
int main(void)
{
int arr[10], i=0, j=0, size, temp;
// Get the size of an array
scanf (“%d”, &size);
// Get the array elements as an input
for (i = 0; i <size; i++)
{
scanf (“%d”, &arr[i]);
}
// Sorting elements in ascending order
for (j=0 ; j<(size-1) ; j++)
{
for (i=0 ; i<(size-1) ; i++)
{
if (arr[i+1] < arr[i])
{
temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
}
}
}
// Print the elements from index value 0 to (size-1) –> ascending order
for (i=0 ; i {
printf (“%d “, arr[i]);
}
printf(“\n”);
// Print the elements from the index value (size-1) to 0 –> descending
order
for (i=size-1; i>=0 ; i–)
{
printf (“%d “, arr[i]);
}
return 0;
}
Example 1:
Input:
8
24793168
Output:
12349876
Example 2:
Input:
6
123456
Output:
123654
Algorithm:
Input:
3 4
Output:
3
44
555
6666
555
44
3
Input :
4 4
Output:
4
55
666
7777
666
55
4
Program:
#include
int main()
{
int i,j,s,N,count=0;
scanf(“%d%d”,&s,&N);
for(i=s;count<4;count++)
{
for(j=0;j<count+1;j++)
printf(“%d”,i);
printf(“\n”);
i=i+1;
}
for(i=s+N-2;count>0;count–)
{
for(j=0;j<count-1;j++)
printf(“%d”,i);
printf(“\n”);
i=i-1;
}
return 0;
}
Input :
3
Output:
1
2*2
3*3*3
3*3*3
2*2
1
Input :
4
Output:
1
2*2
3*3*3
4*4*4*4
4*4*4*4
3*3*3
2*2
1
Program:
#include
int main()
{
int i,j,k,N,count=0;
scanf(“%d”,&N);
for(i=1;i<=N;i++)
{
k=1;
for(j=0;j<i;j++)
{
printf(“%d”,i);
if(k<i)
{
printf(“*”);
k=k+1;
}
}
printf(“\n”);
}
for(i=N;i>0;i–)
{
k=1;
for(j=0;j<i;j++)
{
printf(“%d”,i);
if(k<i)
{
printf(“*”);
k=k+1;
}
}
printf(“\n”);
}
return 0;
}
Input:
4
Output:
1
2*3
4*5*6
7*8*9*10
7*8*9*10
4*5*6
2*3
1
Program:
#include
int main() {
int i,j,count=1,n;
printf(“Enter a number\n”);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
if(j<i)
printf(“%d*”,count++);
else
printf(“%d”,count++);
} printf(“\n”);
}
count=count-n;
for(i=n;i>=1;i–)
{ for(j=1;j<=i;j++)
{
if(j<i)
printf(“%d*”,count++);
else
printf(“%d”,count++);
}
count=(count+1)-2*i;
printf(“\n”);
}
return 0;
}
Input:
3 4
Output:
3
44
555
6666
6666
555
44
3
Program:
#include
int main()
{
int i,j,s,N,count=0;
scanf(“%d%d”,&s,&N);
for(i=s;count<4;count++)
{
for(j=0;j<count+1;j++)
printf(“%d”,i);
printf(“\n”);
i=i+1;
}
for(i=s+N-2;count>0;count–)
{
for(j=0;j<count-1;j++)
printf(“%d”,i);
printf(“\n”);
i=i-1;
}
return 0;
}
Input:
5
Output:
1
3*2
4*5*6
10*9*8*7
11*12*13*14*15
Program:
#include
int main()
{
int i,j,k,l=1,N,d,r,count=0;
scanf(“%d”,&N);
for(i=1;i<=N;i++)
{
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;
}
Program:
#include
void pattern(int);
int main()
{
int n;
scanf(“%d”, &n);
pattern(n);
return 0;
}
void pattern(int n)
{
int i, j, k, s, a = 1,b = n*n + 1;
for (i = n; i >= 1; i–) {
for (s = 0; s < n – i; s++)
printf(“–“);
for (j = 0; j < i; j++)
printf(“%d*”, a++);
for (k = 0; k < i – 1; k++)
printf(“%d*”, b++);
printf(“%d\n”, b); // last b should without *
b -= 2*(i – 1);
}
}
Input:
3
Output:
333
313
323
333
Program:
#include
int main()
{
int i, j, n, c=1;
scanf(“%d”, &n);
for(i=1; i<=n+1; i++)
{
for(j=1; j<=n; j++)
{
if(i!=1 && j==n-1)
{
printf(“%d “, c);
c++;
}
else
printf(“%d “, n);
}
printf(“\n”);
}
return 0;
}
11) Paranthesis checker: Check whether the given expression is valid or not(only
parenthesis symbol).
Test Case: 1
Input: “(( ))”
Output: Valid
Test Case: 2
Input: “()(“
Output: Invalid
Program:
#include
#include
#include
int top = -1; char stack[100];
void push(char);
void pop();
void find_top();
void main()
{
int i;
char a[100];
scanf(“%s”, &a);
for (i = 0; a[i] != ‘\0’; i++)
{
if (a[i] == ‘(‘)
push(a[i]);
else if (a[i] == ‘)’)
pop();
}
find_top();
}
// to push elements in stack
void push(char a)
{
top++;
stack[top] = a;
}
// to pop elements from stack
void pop()
{
if (top == -1)
{
printf(“Invalid”);
exit(0);
}
else
top–;
}
// to find top element of stack
void find_top()
{
if (top == -1)
printf(“Valid”);
else
printf(“Invalid”);
}
#include
int main()
{
int a[10][10], transpose[10][10], r, c, i, j;
printf(“Enter rows and columns of matrix: “);
scanf(“%d %d”, &r, &c);
Program:
#include
int main()
{
int r, c, a[100][100], b[100][100], sum[100][100], i, j;
printf(“Enter number of rows (between 1 and 100): “);
scanf(“%d”, &r);
printf(“Enter number of columns (between 1 and 100): “);
scanf(“%d”, &c);
printf(“\nEnter elements of 1st matrix:\n”);
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
printf(“Enter element a%d%d: “,i+1,j+1);
scanf(“%d”,&a[i][j]);
}
printf(“Enter elements of 2nd matrix:\n”);
for(i=0; i<r; ++i)
for(j=0; j<c; ++j)
{
printf(“Enter element a%d%d: “,i+1, j+1);
scanf(“%d”, &b[i][j]);
}
// Adding Two matrices
for(i=0;i<r;++i)
for(j=0;j<c;++j)
{
sum[i][j]=a[i][j]+b[i][j];
}
// Displaying the result
printf(“\nSum of two matrix is: \n\n”);
for(i=0;i<r;++i)
{ for(j=0;j<c;++j)
{
printf(“%d “,sum[i][j]);
if(j==c-1)
{
printf(“\n\n”);
}
}
return 0;
}
1) 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”);
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.
2) Find the logical error in the below code.
void main () {
int i, j, n = 5;
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.
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:
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.
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];
}
return sum;
}
int main() {
//code
int a[10], n, i;
Solution:
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;
}
return -1;
}
int main() {
//code
int a[10], n, i;
// get the elements count
scanf(“%d”, &n);
// get the array elements
for(i=0; i<n; i++)
{
scanf(“%d”, &a[i]);
}
return 0;
}
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(“%d\n”, i);
i–;
}
return 0;
}
Input: 4
Output: Infinite loop
Input: 20
Output: -2102132736
void main()
{
int i, j, n;
scanf(“%d”, &n);
for(i = 1; i<n; i++)
{
for(j = 1; j<n; j++)
{
printf(“%d”, i);
}
printf(“\n”);
}
}
Input: 3
Output:
111
222
333
5) Fix the error, recompile and match against the output provided.
int main(void)
{
printf(“This is a \”buggy” program\n”);
return 0;
}
Corrected program:
int main(void)
{
printf(“This is a \”buggy\” program\n”);
return 0;
}
Answer:
void binarytodecimal(number)
{
int dval=0, base=1, rem;
while(number > 0)
{
rem = number % 10;
dval = dval + rem * base;
num = number / 10;
base = base * 2;
}
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(“%d\n”, arr[i]);
}
return 0;
}
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;
}
Find the number of all possible triplets in the array that can form the triangle
(condition is a + b > c).
#include”stdio.h”
int arr[100], n, n1, n2, i, j, k;
int a,b,c;
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];
if( ((a + b)>c) && ((a + c)>b) && ((b + c)>a) )
{
printf(“%d %d %d “,a,b,c);
printf(“Yes\n”);
}
else
{
printf(“%d %d %d “,a,b,c);
printf(“No\n”);
}
}
}
}
return 0;
}
Print all the prime numbers which are below the given number separated by
comma
Input: 50
#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
#include”stdio.h”
int main()
{
int m, n, i, gcd;
scanf(“%d %d”, &m, &n);
for(i=1; i <=m && i <= n; ++i)
{
// Checks if i is factor of both integers
if(m%i==0 && n%i==0)
gcd = i;
}
printf(“ %d”, gcd);
return 0;
}
#include “stdio.h”
void main()
{
long n, t, digit;
int sum = 0;
scanf(“%ld”, &n);
t = n;
while (n > 0)
{
digit = n % 10;
sum = sum + digit;
n /= 10;
}
printf(“%d”, sum);
}
1
3*2
4*5*6
10*9*8*7
11*12*13*14*15
#include ”stdio.h”
int main()
{
int i, j, k, l=1, N, d, r, count=0;
scanf(“%d”, &N);
for(i=1; i<=N; i++)
{
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;
}
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.
EX: 8 by 8(8*8) matrix maze where Mooshak can get the cheese.
10111001
10001111
10000000
10109011
11101001
10101101
10000101
11111111
Test Cases:
Test Case 1:
Input: [[0,0,0],[9,1,1],[0,1,1]]
Expected return value: 0
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)
#include”stdlib.h”
#include”stdio.h”
int path(int maze[3][3]);
int main()
{
int i,j,maze[3][3], result=0;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
scanf (“%d”, &maze[i][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;
}
int path(int m[3][3])
{ for(static int i=0;i<3;i++)
{ 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;
}
}
}
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}
#include”stdio.h”
int Not_common (int *arr1, int *arr2, int l1, int l2)
{
int count =0, flag1, i, j;
for(i=0; i<l1; i++)
{
flag1=0;
for(j=0; j<l2; j++)
{
if(arr1[i] == arr2[j])
{
flag1=1; break;
}}
if(flag1 ==0)
{
count++;
printf(“%d,”, arr1[i]);
}
}
for(i=0; i<l2; i++)
{
flag1=0;
for(j=0; j<l1; j++)
{
if(arr2[i] == arr1[j])
{
flag1=1;
break;
}}
if(flag1 ==0)
{
count++;
printf(“%d,”, arr2[i]);
}}
return count;
}
int main()
{
int len1=3,len2=3, result, i, j;
int arr1[10],arr2[10];
scanf(“%d %d”, &len1, &len2);
for(i=0; i<len1; i++)
scanf(“%d”, &arr1[i]);
for(i=0; i<len2; i++)
scanf(“%d”, &arr2[i]);
result = Not_common (arr1,arr2,len1,len2);
printf(“\n %d”, result);
return 0;
}
Instructions
Points to note
1) While printing the output no leading or trailing spaces should be printed.
2) Other than the required output, no other text should be printed.
3) If the output is a number, no leading sign must be printed unless it is a negative
number.
4) No scientific notation (3.9265E + 2).
5) All floating point numbers must contain that many decimal places as mentioned in
the question.
This series is a mixture of 2 series – all the odd terms in this series form a Fibonacci
series and all the even terms are the prime numbers in ascending order.
The value N is a Positive integer that should be read from STDIN. The Nth term that
is calculated by the program should be written to STDOUT. Other than the value
of Nth term, no other characters/strings or message should be written to STDOUT.
For example, when N = 14, the 14th term in the series is 17. So only the value 17
should be printed to STDOUT.
Program:
#include
#define MAX 1000
void fibonacci(int n)
{
int i, t1 = 0, t2 = 1, nextTerm;
for (i = 1; i<=n; i++)
{
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
}
printf(“%d”, t1);
}
void prime(int n)
{
int i, j, flag, count =0;
for (i=2; i<=MAX; i++)
{
flag = 0;
for (j=2; j<i; j++)
{
if(i%j == 0)
{
flag = 1;
break;
}
}
if (flag == 0)
count++;
if(count == n)
{
printf(“%d”, i);
break;
}
}
}
int main( )
{
int n;
scanf(“%d”, &n);
if(n%2 == 1)
fibonacci (n/2 + 1);
else
prime(n/2);
return 0;
}
#include
int main(int a, char *b[]) //command line arguments
{
int x,y,f=1;
x=atoi(b[1]); //atoi function is to convert a character to integer
for(i=1;i<=x;i++)
{
f=f*i;
}
printf(“%d”,f);
return 0;
}
Write a c program, to find the area of a circle when the diameter is given, using
command line arguments. The input diameter is an integer and the output area
should be a floating point variable with 2 point precision.
Solution:
#include
#define PI 3.14
int main(int a, char *b[]) //command line arguments
{
int d; float area =0;
d= atoi(argv[1]);
area =(float) PI*(d/2)*(d/2);
printf(“%0.2f”, area); //%0.2f is to print the answer with 2 values after decimal point.
return 0;
}
Write a c program, to check whether the given year is a leap year or not using
command line arguments. A leap year is a calendar year containing one additional
day (Feb 29th) added to keep the calendar year synchronized with the astronomical
year.
Solution:
#include
int main(int a, char*b[])
{
int year; year=atoi(b[1]);
if(year%100==0)
{
if(year%400==0)
{
printf(“LEAP YEAR”);
}
else{
printf(“NOT LEAP YEAR”); } }
else if(year%4==0)
{
printf(“LEAP YEAR”);
}
else{
printf(“NOT LEAP YEAR”);
}
return 0; }
Write a c program, to find the GCD of the given 2 numbers, using command line
arguments. The input is 2 integer and the output GCD also should be an integer
value.
Solution:
#include
int main(int x, char *y[])
{
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;
}
C Program to check whether a given number is a prime number or not. The given
number N, a positive integer, will be passed to the program using the first command
line parameter. If it is a prime number the output should be the square root of the
number up to 2 decimal point precision, If it is not a prime number then print 0.00 to
stdout.
Solution:
#include
#include
#include int main(int a, char *b[])
{
int number,i,flag = 1;
number = atoi(b[1]);
for(i=2; i<number; i++)
{
if(number%i == 0)
{
flag = 0;
break;
}
}
if(flag == 1)
printf(“%.2f”,sqrt(number));
else
printf(“0.00”);
return 0;
}
C Program to check whether a given number is a strong number or not. The given
number N, a positive integer, will be passed to the program using the first command
line parameter. If it is a strong number, the output should be “YES”, If it is not a prime
number then output should be “NO” to stdout. Other than YES or NO, no other extra
information should be printed to stdout.
Solution:
#include
#include
int main(int a, char *b[])
{
int number, i, temp, sum = 0, factorial = 1;
number = atoi(b[1]);
temp = number;
while(number != 0)
{
int rem = number%10;
for(i=2; i<=rem; i++)
{
factorial = factorial * i;
}
sum = sum + factorial;
number = number/10;
factorial = 1;
}
if(temp == sum)
printf(“YES”);
else
printf(“NO”);
return 0;
}
Write a C program which will convert a given decimal integer number N to its binary
equivalent. The given number N, a positive integer, will be passed to the program
using the first command line parameter. Print the equivalent binary number to
stdout. Other than the binary number, no other extra information should be printed
to stdout Example: Given input “19”, here N=19, expected output 10011
Solution:
#include
#include
int main(int a, char *argv[])
{
int number, count, i;
int b[32];
number = atoi(argv[1]);
count = 0;
while(number != 0)
{
b[count]=number%2;
number = number/2;
count++;
}
for(i=(count-1); i>=0; i–)
printf(“%d”, b[i]);
return 0;
}
Write a c program that will find the sum of all prime numbers in a given range. The
range will be specified as command line parameters. The first command line
parameter, N1 which is a positive integer, will contain the lower bound of the range.
The second command line parameter N2, which is also a positive integer will contain
the upper bound of the range. The program should consider all the prime numbers
within the range, excluding the upper bound and lower bound. Print the output in
integer format to stdout. Other than the integer number, no other extra information
should be printed to stdout. Example Given inputs “7” and “24” here N1= 7 and
N2=24, expected output as 83.
Solution:
#include
int main(int argc, char *argv[])
{
int N1, N2, j, i, count, sum = 0;
N1 =atoi(argv[1]);
N2 =atoi(argv[2]);
for(i=N1+1; i<N2; ++i)
{
count = 0;
for(j=2; j<=(i/2); j++)
{
if(i%j==0)
{
count++;
break;
}
}
if(count==0)
sum = sum + i;
}
printf(“%d”,sum);
return 0;
}
Write a C program to check whether the given number is a perfect square or not
using command line arguments.
Solution:
#include
#include
int main(int a, char *b[])
{
int n, i;
n= atoi(b[1]);
for(i = 0; i <= n; i++)
{
if(n == i * i)
{
printf(“YES”);
return 0;
}
}
printf(“NO”);
return 0;
}
Write a C program to check whether the given number is Palindrome or not using
command line arguments.
Solution:
#include
#include
int main(int a,int *b[])
{
int number, rem, sum = 0;
number = atoi(b[1]);
int copy = number;
while(number != 0)
{
rem =number%10;
sum = sum * 10 + rem;
number = number/10;
}
if(copy == sum)
printf(“Palindrome”);
else
printf(“Not Palindrome”);
return 0;
}
Solution:
#include
int main(int argc, char *argv[])
{
char *str = argv[1];
int i;
for(i =0; str[i] !=’\0′; i++)
{
if(str[i] == ‘a’ || str[i] == ‘e’ || str[i] == ‘i’ || str[i] == ‘o’ || str[i] == ‘u’)
{
str[i] = str[i] – 32;
}
}
printf(“%s”, str);
return 0;
}
Solution:
Write a C program to find whether the given number is an Armstrong number or not
using command line arguments.
An Armstrong number of three digits is an integer such that the sum of the cubes of
its digits is equal to the number itself. For example, 371 is an Armstrong number
since 3**3 + 7**3 + 1**3 = 371.
Solution:
#include
#include
#include int main(int a, char*b[])
{
int n;
n= atoi(b[1]);
int sum=0;
int temp=n;
int cnt=0;
while(n!=0)
{
n=n/10;
cnt++;
}
n=temp;
while(n!=0)
{
int rem=n%10;
sum=sum+pow(rem,cnt);
n=n/10;
}
if(temp==sum)
{
printf(“yes”);
}
else
{
printf(“no”);
}
return 0;
}
Solution:
#include
#include
int main(int a, char *b[])
{
int i, n, t1 = 0, t2 = 1, nextTerm;
n=atoi(b[1]);
for (i = 1; i <= n; ++i)
{
printf(“%d “, t1);
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
}
return 0;
}
1) Count the number of co-prime pairs in an array. (Any two numbers whose GCD
is 1 are be called as co-prime)
Input:
The first line contains an integer T, total number of elements. Then follow T elements.
Output:
Count the number of co-prime pairs in an array.
Constraints
1 ≤ T ≤ 25
1 ≤ elements ≤ 100
Input:
4
4839
Output:
4
Here, Co-prime pairs are (4, 3), (8, 3), (4, 9 ), (8, 9 )
Program:
#include<stdio.h>
int coprime(int a, int b)
{
int gcd;
while ( a != 0 )
{
gcd = a; a = b%a; b = gcd;
}
if(gcd == 1)
return 1;
else
return 0;
}
int count_pairs(int arr[], int n)
{
int count = 0;
for (int i = 0; i < n – 1; i++)
{
for (int j = i + 1; j < n; j++)
{
if (coprime(arr[i], arr[j]))
count++;
}
}
return count;
}
int main()
{
int n;
scanf(“%d”, &n);
int a[25], i;
for(i=0; i<n; i++)
scanf(“%d”, &a[i]);
printf(“%d”, count_pairs(a, n));
return 0;
}
Given an array, number to search (say e1), and occurrence (say n), print the index of
the nth occurrence of e1 in the array. If e1 does not occur n times, then print the
index as -1.
Program:
#include<stdio.h>
int main()
{
int a[100],n,i,e1,size,count=0;
scanf(“%d”,&size);
for(i=0;i<size;i++)
scanf(“%d”,&a[i]);
scanf(“%d”,&e1);
scanf(“%d”,&n);
for(i=0;i<size;i++)
{
if(e1==a[i])
count++;
//If ‘n’th occurrence found then print it’s index and exit.
if(count==n)
{
printf(“%d”,i);
return 0;
}
}
//If ‘n’ occurrence not found then print ‘-1’.
printf(“%d”,-1);
return 0;
}
3) Search for an element in an array:
The input consists of n + 2 lines. The first line consists a single integer n, The next n
lines consists of 1 integer element part of the array. The last line consists of an
integer to be searched. Output found or missing based on whether the element is
present in the array or not. Note: max value of n is 100.
Program:
#include <stdio.h>
#define MAX_SIZE 20
int main()
{
int n, i, j, min_index, array[MAX_SIZE], x;
scanf(“%d”, &n);
for(i = 0; i < n; i++)
scanf(“%d”, &array[i]);
scanf(“%d”, &x);
for(i = 0; i < n; i++)
{
if(x == array[i])
{
printf(“Found\n”);
return 0;
}
}
printf(“Missing\n”);
return 0;
}
Input:
The first line contains an integer T, total number of elements. Then follow T integers.
Output:
Display the second largest among the given T integers.
Constraints
1 ≤ T ≤ 1000
1 ≤ integers ≤ 1000000
Program:
#include<stdio.h>
int main()
{
int a[50], size, i, j = 0, big, sec_big;
scanf(“%d”, &size);
for(i=0; i<size; i++)
scanf(“%d”, &a[i]);
big=a[0];
for(i=1;i<size;i++)
{
if(big<a[i])
{
big=a[i];
j = i;
}
}
sec_big =a[size-j-1];
for(i=1;i<size;i++)
{
if(sec_big <a[i] && j != i)
sec_big =a[i];
}
printf(“%d”, sec_big);
return 0;
}
Get a target element and return its coordinates. If the value didn’t exist, the program
had to return (-1,-1). First line of input is size of row and column,
followed rxc elements. The third line of input is the element to be searched in
the rxc matrix.
Program:
#include <stdio.h>
int main()
{
int i, j, count = 0;
int arr[10][10], search, r, c;
scanf(“%d %d”, &r, &c);
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
scanf(“%d”, &arr[i][j]);
}
scanf(“%d”, &search);
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
if (arr[i][j] == search)
{
printf(“(%d , %d)\n”, i, j);
count++;
}
}
}
if (count == 0)
printf(“(-1,-1)”);
return 0;
}
1) Count the number of co-prime pairs in an array. (Any two numbers whose GCD
is 1 are be called as co-prime)
Input:
The first line contains an integer T, total number of elements. Then follow T elements.
Output:
Count the number of co-prime pairs in an array.
Constraints
1 ≤ T ≤ 25
1 ≤ elements ≤ 100
Program:
#include<stdio.h>
int coprime(int a, int b)
{
int gcd;
while ( a != 0 )
{
gcd = a; a = b%a; b = gcd;
}
if(gcd == 1)
return 1;
else
return 0;
}
int count_pairs(int arr[], int n)
{
int count = 0;
for (int i = 0; i < n – 1; i++)
{
for (int j = i + 1; j < n; j++)
{
if (coprime(arr[i], arr[j]))
count++;
}
}
return count;
}
int main()
{
int n;
scanf(“%d”, &n);
int a[25], i;
for(i=0; i<n; i++)
scanf(“%d”, &a[i]);
printf(“%d”, count_pairs(a, n));
return 0;
}
Given an array, number to search (say e1), and occurrence (say n), print the index of
the nth occurrence of e1 in the array. If e1 does not occur n times, then print the
index as -1.
Get the size of an array and get elements one by one. Input the number to be
searched and occurrence. For example, 7 => Size of an array 1 4 6 7 6 3 6 => array
elements 6 => number to be searched 3 => 3rd occurrence of number 6 Output: 6
Explanation: Number 6, 3rd occurrence position is 6
Program:
#include<stdio.h>
int main()
{
int a[100],n,i,e1,size,count=0;
scanf(“%d”,&size);
for(i=0;i<size;i++)
scanf(“%d”,&a[i]);
scanf(“%d”,&e1);
scanf(“%d”,&n);
for(i=0;i<size;i++)
{
if(e1==a[i])
count++;
//If ‘n’th occurrence found then print it’s index and exit.
if(count==n)
{
printf(“%d”,i);
return 0;
}
}
//If ‘n’ occurrence not found then print ‘-1’.
printf(“%d”,-1);
return 0;
}
The input consists of n + 2 lines. The first line consists a single integer n, The next n
lines consists of 1 integer element part of the array. The last line consists of an
integer to be searched. Output found or missing based on whether the element is
present in the array or not. Note: max value of n is 100.
Program:
#include <stdio.h>
#define MAX_SIZE 20
int main()
{
int n, i, j, min_index, array[MAX_SIZE], x;
scanf(“%d”, &n);
for(i = 0; i < n; i++)
scanf(“%d”, &array[i]);
scanf(“%d”, &x);
for(i = 0; i < n; i++)
{
if(x == array[i])
{
printf(“Found\n”);
return 0;
}
}
printf(“Missing\n”);
return 0;
}
Input:
The first line contains an integer T, total number of elements. Then follow T integers.
Output:
Display the second largest among the given T integers.
Constraints
1 ≤ T ≤ 1000
1 ≤ integers ≤ 1000000
Program:
#include<stdio.h>
int main()
{
int a[50], size, i, j = 0, big, sec_big;
scanf(“%d”, &size);
for(i=0; i<size; i++)
scanf(“%d”, &a[i]);
big=a[0];
for(i=1;i<size;i++)
{
if(big<a[i])
{
big=a[i];
j = i;
}
}
sec_big =a[size-j-1];
for(i=1;i<size;i++)
{
if(sec_big <a[i] && j != i)
sec_big =a[i];
}
printf(“%d”, sec_big);
return 0;
}
Get a target element and return its coordinates. If the value didn’t exist, the program
had to return (-1,-1). First line of input is size of row and column,
followed rxc elements. The third line of input is the element to be searched in
the rxc matrix.
Program:
#include <stdio.h>
int main()
{
int i, j, count = 0;
int arr[10][10], search, r, c;
scanf(“%d %d”, &r, &c);
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
scanf(“%d”, &arr[i][j]);
}
scanf(“%d”, &search);
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
if (arr[i][j] == search)
{
printf(“(%d , %d)\n”, i, j);
count++;
}
}
}
if (count == 0)
printf(“(-1,-1)”);
return 0;
}