Vous êtes sur la page 1sur 18

Question 1 (Second Smallest Number)

Given a set of elements, design an Algorithm and write the subsequent C program to determine the second
smallest number in that set.
Input Format
Number of elements in n
element-1
element-2

element-n
Output Format
Second smallest element in the set

Solution
#include< stdio.h >
void main()
{
int n,i,num,small=999,sec=999;
scanf("%d",&n);
for(i=0;i < n;i++)
{
scanf("%d",&num);
if(num < =small)
{
sec=small;
small=num;
}
else if(num < =sec)
sec=num;
}
printf("%d",sec);
}

Question 2 (Recursive Fibonacci)

Given the value of n, write a recursive routine in C to print the first n elements of the Fibonacci series.
Input Format
Value of n
Output Format
Fibonacci series of n terms, each term separated by a space
Solution
#include< stdio.h >

void main()
{
int i,n,f=0,sum=0,num=1;
scanf("%d",&n);
printf("0 1");
for(i=0;i < n-2;i++)
{
sum=f+num;
f=num;
num=sum;
printf(" %d",sum);
}
}

Question 3 (Cyclic Right shift of Elements)

Given a set of elements stored in an array and a number m, design an Algorithm and write the subsequent
C program to perform cyclic right shift of the array by m places. For example, if the elements are 12, 13,
16, 7, 10 and m =2 then the resultant set will be 7, 10, 12, 13, 16.
Input Format
Number of elements in the set: n
element-1
element-2

element-n
value of m
Output Format
Elements in the set after right shift by m places
Solution
#include< stdio.h >
void main()
{
int a[20],n,i,m,t,j;
scanf("%d",&n);
for(i=0;i < n;i++)
scanf("%d",&a[i]);
scanf("%d",&m);
for(j=0;j < m;j++)
{
t=a[n-1];
for(i=n-1;i > 0;i--)
a[i]=a[i-1];
a[0]=t;
}
for(i=0;i < n;i++)
printf("%d\n",a[i]);
}

Qestion 4 (Leaders of Elements)

Given a set of n elements in an order, identify all the leaders and print them. An element is said to be a
leader if all the elements to its right are smaller than it. For example, if the elements are 12, 13, 16, 7, 10
then there is only one leader element 16. If there are no leaders in the given set of elements then print No
leaders.
Input Format
Number of elements in the given set: n
element-1
element-2

element-n
Output Format
Elements that are leaders. Else, print No leaders when there is no leader.
Solution
#include< stdio.h >
void main()
{
int a[20],f=1,flag=0,i,j,n;
scanf("%d",&n);
for(i=0;i < n;i++)
scanf("%d",&a[i]);
for(i=0;i < n-1;i++)
{
for(j=i+1;j < n;j++)
if(a[i] > a[j])
f=1;
else
{
f=0;
break;
}
if(f==1)
{
printf("%d\n",a[i]);
flag=1;
}
f=0;
}
if(flag==0)
printf("No leaders");
}

Question 5 (Recursive reverse)

Given a string, write a recursive routine to reverse it. For example, given the string and, the reversal of the
string is dna.

Input Format
A string
Output Format
Reverse of the string
Solution
#include< stdio.h >
#include< string.h >
void main()
{
char s[20],ch;
int i,len;
scanf("%s",s);
len=strlen(s);
for(i=0;i < (len/2);i++)
{
ch=s[i];
s[i]=s[len-1-i];
s[len-1-i]=ch;
}
puts(s);
}

Question 1 (Row Maximum of a Matrix)

Given an nXn matrix with entries as numbers, print the maximum value in each row of the matrix.
Input Format
Value of n
Element in first row first column
Element in first row second column
..
Element in the first row nth column
Element in second row first column
Element in second row second column
..
Element in the second row nth column

Element in nth row first column

Element in nth row second column


..
Element in nth row nth column
Output Format
Maximum value in the first row
Maximum value in the second row

Maimum value in the nth row


Solution
#include< stdio.h >
void main()
{
int i,j,n,max,num;
scanf("%d",&n);
for(i=0;i < n;i++)
{
max=-1;
for(j=0;j < n;j++)
{
scanf("%d",&num);
if(num > max)
max=num;
}
printf("%d\n",max);
}
}

Question 2 (Salary of Employees)

A company stores the following details of employees such as name, employee id, basic pay, % of DA and
HRA. Given details of n employees of an organization. Write a C code to
i. get the details of each employee.
ii. print their employee id and
iii. Total salary.
Total salary = Basic Pay + % of DA * basic pay + HRA.
Input Format
value of n
Employee name of employee1

Employee id of employee1
Basic pay of employee1
Percentage of DA of employee1
HRA of employee1

Employee name of employee n


Employee id of employee n
Basic pay of employee n
Percentage of DA of employee n
HRA of employee n
Output Format
Employee id of employee1
Total salary of employee1
Employee id of employee2
Total salary of employee2

Employee id of employee n
Total salary of employee n
Solution
#include< stdio.h >
void main()
{
char name[20];
int i,n,id,bp,da,hra;
scanf("%d",&n);
for(i=0;i < n;i++)
{
scanf("%s",name);
scanf("%d%d%d%d",&id,&bp,&da,&hra);
printf("%d\n%d\n",id,(bp+((bp*da)/100)+hra));
}
}

Question 3 (Verification of Circular Prime Number)

A circular prime number is a prime number p with a property that all the numbers got by cyclically
permuting the digits of p, are also a prime number.
A number is said to be a prime if it has no factors other than the number 1 and itself. 19937 is a circular
prime number, as all the numbers obtained by cyclically permuting the number 19937 : 99371,
93719,37199,71993,19937 are all prime.
Develop an algorithm and write a C program to check if the given number is circular prime or not.
Input Format
A number
Output Format
Print Circular prime or Not circular prime
Solution
#include< stdio.h >
#include< string.h >
int prime(int n)
{
int i,flag=1;
for(i=2;i < =n/2;i++)
if(n%i==0)
{
flag=0;
break;
}
return(flag);
}
int nnum(char s[])
{
int n=0,i;
char temp=s[0];
for(i=0;i < strlen(s);i++)
{
n*=10;
n+=s[i]-'0';
s[i]=s[i+1];
}
s[strlen(s)]=temp;
return(n);
}
void main()
{
int i,j,n,num,flag=1;
char s[20];
scanf("%s",s);
n=nnum(s);
while(num!=n)
{
num=nnum(s);
if(prime(num)==0)
{
flag=0;
break;
}
}
if(flag==0)

printf("Not circular prime");


else
printf("Circular prime");

Question 4 (Identify machines in same local network)

Numeric addresses for computers on the international network, Internet has four parts, separated by
periods, of the form xxx.yyy.zzz.mmm where xxx , yyy , zzz , and mmm are positive integers.
Locally, computers are usually known by a nickname as well.
Sample Data
IP address

Name

111.22.3.44

platte

555.66.7.88

wabash

111.22.5.66

green

0.0.0.0

none

A pair of computers are said to be in same locality when the first two components of the addresses are same.
Given the details of some computers, design an algorithm and write a C program to display a list of
messages identifying each pair of computers from the same locality. In the messages, the computers should
be identified by their nicknames. In this example, the message to be displayed will be Machines platte and
green are on the same local network. For example, given IP address and nick name of machines as follows:
101.33.2.1
101.33.56.80
101.43.45.74

Atlas
Horizon
Pluto

Print Machines Atlas and Horizon are on the same local network.
Input Format
Number of computers n
IP address of the computer1 as a String
Nick names of the computer1 as a String
IP address of the computer2 as a String
Nick names of the computer2 as a String
.
IP address of the computer n as a String
Nick names of the computer n as a String

Output Format
For each pair of machines in the same local network, print:
Machines A and B are on the same local network (A and B are names of the respective machines)
Solution
#include< stdio.h >
#include< string.h >
void main()
{
int n,p,i,j;
char s[20][20],na[10][10];
scanf("%d",&n);
for(i=0;i < n;i++)
{
scanf("%s%s",s[i],na[i]);
p=0;
j=0;
while(p!=2)
{
j++;
if(s[i][j]=='.')
p++;
}
s[i][j]='\0';
}
for(i=0;i < n-1;i++)
for(j=i+1;j < n;j++)
if(strcmp(s[i],s[j])==0)
printf("Machines %s and %s are on the same local network",na[i],na[j]);
}

Question 5 (Verification of L shaped arrangement of coins on game board)

Consider an nxn board game with four types of coins red, green, blue and yellow. Given the state of the
board with coins in all cells, develop an algorithm and write a C program to check if the same coins are
placed in the shape of L on the board. The number of cells in the vertical and horizontal line of L shape,
is same. Red coins are represented by r, blue coins are represented by b, green coins are represented by
g and yellow coins are represented by y.
For example, given the configuration of a 4 X 4 board with coins as shown below, the program must print
Yes since the coin r is placed in the positions (3,2), (4,2),(4,3),(4,4) form an L shape.
bryr
rryb
yrbb
brrr
Input Format
Number of rows
Number of columns

Elements in the matrix(board), given row by row


Output Format
Print Yes or No
Solution
#include< stdio.h >
void main()
{
char m[10][10];
int r,c,i,j,f=0;
scanf("%d%d",&r,&c);
for(i=0;i < r;i++)
for(j=0;j < c;j++)
{
m[i][j]=getchar();
while(!((m[i][j] > ='a')&&(m[i][j] < ='z')))
m[i][j]=getchar();
}
if(r > 2 && c > 2)
{
for(i=0;i < r-1;i++)
for(j=0;j < c-2;j++)
{
if((m[i][j]==m[i+1][j])&&(m[i][j]==m[i+1][j+1])&&(m[i][j]==m[i+1][j+2]))
{
f=1;
printf("Yes");
break;
}
}
if(f==0)
printf("No");
}
else
printf("No");
}

Question (Nature of Digit in Position)

Given a number n and a position p, write an algorithm and the subsequent C program to check if the pth digit from the leftmost position of n is odd or even. For example, if n is 3145782 and p is 4 then you
have to check if 5 is odd or even. Since it is odd print Odd. Make your code accept numbers of larger size.
Input Format:
The first line contains the number, n
The second line contains the position, p
Output Format:
Print either Odd or Even

Solution

#include< stdio.h >


void main()
{
int p,i=0,a[10];
long int n;
scanf("%d%d",&n,&p);
while(n > 0)
{
a[i++]=n%10;
n/=10;
}
if(a[i-p]%2==0)
printf("Even");
else
printf("Odd");
}

Question (Complex Number)

A computer scientist working in image processing is working on discrete Fourier transform. He needs an
implementation of complex number to use in his program. Develop an algorithm and write a C program to
implement addition, subtraction and multiplication in complex numbers. Implement each operation as a
function and call it in your main. The function call sequence is addition, subtraction and multiplication. For
example when the complex numbers are 3+2i, 1+7i the output should be
4+9i
2-5i
-11+23i
Input Format
Real part of complex number1
Imaginary part of complex number1
Real part of complex number2
Imaginary part of complex number2
Output Format
Resultant complex number represented as
real part +/- imaginary part followed by an i

Solution
#include< stdio.h >
void add(int r1,int i1,int r2,int i2)

if(i1+i2 > 0)
printf("%d+%di\n",r1+r2,i1+i2);
else
printf("%d%di\n",r1+r2,i1+i2);

}
void multi(int r1,int i1,int r2,int i2)
{
int d=(r1*i2)+(r2*i1);
if(d > 0)
printf("%d+%di",(r1*r2)-(i1*i2),d);
else if(d==-1)
printf("%d-i",(r1*r2)-(i1*i2));
else
printf("%d%di",(r1*r2)-(i1*i2),d);
}
void main()
{
int r1,r2,i1,i2;
scanf("%d%d%d%d",&r1,&i1,&r2,&i2);
add(r1,i1,r2,i2);
add(r1,i1,-r2,-i2);
multi(r1,i1,r2,i2);
}

// program for the addition of two matrices

#include <stdio.h>
main()
{
int m,i,j, n, c, d, first[10][10], second[10][10], sum[10][10];

printf("Enter the number of rows and columns of matrix\n");


scanf("%d%d", &m, &n);
printf("Enter the elements of first matrix\n");
for ( i = 0 ; i < m ; i++ )
for ( j = 0 ; j < n ; j++ )
scanf("%d", &first[i][j]);

printf("Enter the elements of second matrix\n");

for ( c = 0 ; c < m ; c++ )

for ( d = 0 ; d < n ; d++ )


scanf("%d", &second[c][d]);

for ( c = 0 ; c < m ; c++ )


for ( d = 0 ; d < n ; d++ )
sum[c][d] = first[c][d] + second[c][d];
printf("the first matrix is \n");
for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < n ; d++ )
printf("%d\t", first[c][d]);

printf("\n");
}

printf("the second matrix is \n");


for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < n ; d++ )
printf("%d\t", second[c][d]);

printf("\n");
}

printf("Sum of entered matrices:-\n");

for ( c = 0 ; c < m ; c++ )


{
for ( d = 0 ; d < n ; d++ )
printf("%d\t", sum[c][d]);

printf("\n");
}

// Program using matrix multiplication

#include <stdio.h>
main()
{
int m, n, p, q, c, d, k, sum = 0;
int first[10][10], second[10][10], multiply[10][10];

printf("Enter the number of rows and columns of first matrix\n");


scanf("%d%d", &m, &n);
printf("Enter the elements of first matrix\n");

for ( c = 0 ; c < m ; c++ )


for ( d = 0 ; d < n ; d++ )
scanf("%d", &first[c][d]);

printf("Enter the number of rows and columns of second matrix\n");


scanf("%d%d", &p, &q);

if ( n != p )// very important for the matrix multiplication to takes place


printf("Matrices with entered orders can't be multiplied with each other.\n");
else
{
printf("Enter the elements of second matrix\n");

for ( c = 0 ; c < p ; c++ )


for ( d = 0 ; d < q ; d++ )
scanf("%d", &second[c][d]);

for ( c = 0 ; c < m ; c++ )

{
for ( d = 0 ; d < q ; d++ )
{
for ( k = 0 ; k < p ; k++ )
{
multiply[c][d]+= first[c][k]*second[k][d];
}

//multiply[c][d] = sum;
// sum = 0;
}
}

printf("Product of entered matrices:-\n");

for ( c = 0 ; c < m ; c++ )


{
for ( d = 0 ; d < q ; d++ )
printf("%d\t", multiply[c][d]);

printf("\n");
}
}

// program using structure

#include<stdio.h>
struct information
{
char name[20];

char add[100];
int mobile,age;
float salary;
};
void main()
{
struct information info;
printf("enter the values");
scanf(" %s %s %d %d %f",
info.name,info.add,&info.mobile,&info.age,&info.salary);

printf("the Entered values are \n");


printf(" %s\n %s\n %d\n %d\n %f\n ",
info.name,info.add,info.mobile,info.age,info.salary);

// Program using structure array

#include<stdio.h>
struct webinfo
{
float login,logout;
int time;
char sites[100];
};

int main()
{
int n;
struct webinfo w[10];
printf("enter the value of n");
scanf("%d",&n);

printf("enter the details");


for(int i = 0;i<n;i++)
{
scanf("%f %f %d %s",&w[i].login,&w[i].logout,&w[i].time,w[i].sites);
}

printf("the details are \n");


for(int i = 0;i<n;i++)
{
printf("%f %f %d %s",w[i].login,w[i].logout,w[i].time,w[i].sites);
}

/*C program to check whether a character is VOWEL or CONSONANT using switch.*/


#include <stdio.h>
int main()
{
char ch;
printf("Enter a character: ");
scanf("%c",&ch);
//condition to check character is alphabet or not
if((ch>='A' && ch<='Z') || (ch>='a' && ch<='z'))
{
//check for VOWEL or CONSONANT
switch(ch)
{
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
printf("%c is a VOWEL.\n",ch);
break;
default:
printf("%c is a CONSONANT.\n",ch);
}
}
else
{

printf("%c is not an alphabet.\n",ch);

return 0;

struct book
{
char name ;
float price ;
int pages ;
};
struct book b[100] ;
for ( i = 0 ; i <= 99 ; i++ )
{
printf ( "\n Enter name, price and pages " ) ;
scanf ( "%c %f %d", &b[i].name, &b[i].price, &b[i].pages ) ;
}

Vous aimerez peut-être aussi