14 views

Uploaded by ASHUTOSH RAY 15BME0081

c codes

- Matrix Algebra
- SCHEME AND SYLLABUS FOR MBA
- Matrices
- FE Sem -I.pdf
- Qi George Chen - Presentation Paper- Spring 17
- Notes on MATLAB
- Matrix Algebra
- Programming in C 1
- c_gtu_prct
- 77.docx
- MINDTREE Version1 2018 Programs
- Simplest C Program: What's Happening? Let's Walk
- Introduction to OpenGL - Matrices
- Compiler Lab Manual
- Program
- C++ - The Complete Reference 4e
- An Introductio to C Programming
- l_5
- jawapan khawarizmi
- pract16b

You are on page 1of 18

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);

}

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);

}

}

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]);

}

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");

}

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);

}

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 nth column

Output Format

Maximum value in the first row

Maximum value in the second 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);

}

}

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 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));

}

}

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)

else

printf("Circular prime");

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]);

}

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

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");

}

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

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");

}

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);

}

#include <stdio.h>

main()

{

int m,i,j, n, c, d, first[10][10], second[10][10], sum[10][10];

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]);

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

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");

}

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

{

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

printf("%d\t", second[c][d]);

printf("\n");

}

{

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

printf("%d\t", sum[c][d]);

printf("\n");

}

#include <stdio.h>

main()

{

int m, n, p, q, c, d, k, sum = 0;

int first[10][10], second[10][10], multiply[10][10];

scanf("%d%d", &m, &n);

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

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

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

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

printf("Matrices with entered orders can't be multiplied with each other.\n");

else

{

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

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

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

{

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;

}

}

{

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

printf("%d\t", multiply[c][d]);

printf("\n");

}

}

#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(" %s\n %s\n %d\n %d\n %f\n ",

info.name,info.add,info.mobile,info.age,info.salary);

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

for(int i = 0;i<n;i++)

{

scanf("%f %f %d %s",&w[i].login,&w[i].logout,&w[i].time,w[i].sites);

}

for(int i = 0;i<n;i++)

{

printf("%f %f %d %s",w[i].login,w[i].logout,w[i].time,w[i].sites);

}

#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

{

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 ) ;

}

- Matrix AlgebraUploaded bySylvia Cheung
- SCHEME AND SYLLABUS FOR MBAUploaded bypadmakumar_1976
- MatricesUploaded byMhai Mabanta
- FE Sem -I.pdfUploaded byUjalaJha
- Qi George Chen - Presentation Paper- Spring 17Uploaded bycrazyfrog1991
- Notes on MATLABUploaded bySVR07
- Matrix AlgebraUploaded byrks_rmrct
- Programming in C 1Uploaded byS Regurathi Pandian
- c_gtu_prctUploaded byMahavir Jadeja
- 77.docxUploaded byGuru Dutt Perichetla
- MINDTREE Version1 2018 ProgramsUploaded bymd aju
- Simplest C Program: What's Happening? Let's WalkUploaded bymacklinsp3
- Introduction to OpenGL - MatricesUploaded bykorisnikkk
- Compiler Lab ManualUploaded byMuhammad Uzzam Afroz
- ProgramUploaded byDheeraj Gupta
- C++ - The Complete Reference 4eUploaded byAyushNil
- An Introductio to C ProgrammingUploaded byFazal Mcpherson
- l_5Uploaded byEntropyPrinciple
- jawapan khawarizmiUploaded byNorliyana Idris
- pract16bUploaded byIulian Decuseara
- c ProgramsUploaded byBalaji Loyisetti
- New Text Document.txtA Calculator Program in C Using graphicsUploaded byajaykaundal
- Chapter4.pdfUploaded byGrace Altea Heidi Cesar
- Maths Chapater BArtsUploaded byhsWS
- UnspecifidfddUploaded bydearbhupi
- 10.1.1.1.2485Uploaded byRocky Barus
- fe sem -iUploaded byapi-200685371
- fe sem -iUploaded byapi-200685371
- Vectors and MatricesUploaded bydorathi
- Yichao Chen, Jonathan L. Gross and Toufik Mansour- Genus Distributions of Mobius Ladders: A Website SupplementUploaded byNimsma

- ProjectMekonnenUploaded byAhmed Hassan
- 06484358Uploaded byranjith120198
- The Dynamic Synchronization MatrixUploaded byNeda Kahn
- day three madeline hunter lesson planUploaded byapi-269729950
- cdc_10668_DS1Uploaded byNguyễn Thanh Tùng
- ASCE710SUploaded byNEO
- Onis 2.3 - Dicom ClientUploaded byjand_20
- COMP1611 WEEK1 Introduction to PMUploaded bySaynuwara AyeNu
- 1. Ecodial Plugin IntroductionUploaded byDardak Bin Ahmad
- ASCE-7-10 changes.pdfUploaded byManoj Jaiswal
- TOEFL_SPEAKING_SECTION_2_Idea.docUploaded bygreen1458
- Boeckh - Philological HermeneuticsUploaded byMondlT
- Majumdar 2012Uploaded byI C
- 819-4208-10-v240 admin guideUploaded byAsoka Hapu
- Turbine Gas Meter HandbookUploaded byfarshidian
- vagogyuruUploaded bypsnmy
- 2017 the History of Philosophical and Formal Logic From Aristotle to TarskiUploaded byfoued
- Analysis of Readiness RateUploaded byMuzafar Shah Mosam Shah
- 2.EIA STEPSUploaded bypushpraj
- Huawei Performance Optimization Management DatasheetUploaded bykikirn
- 978-1-63057-094-1-3Uploaded bygouravbhatia200189
- ARC 311 Project Handout Fall2016 House_of_OneUploaded byHenna Younas
- Hume's Theory of Time and Trans-Temporal IdentityUploaded bymackus28397
- Network AnalysisUploaded byJayasankar Narayanan
- Fire Escape's 314 Area Code BBS Directory - June 1999Uploaded byFire Escape
- Chem162 FractCryst Report Gradescope 021919 PCUploaded byAmalawa Aiwekhoe
- PSA Maintenace in SAP BIUploaded bymath_mallikarjun_sap
- 08 CET Power - Module & Controller Replacement Procedure V1.2Uploaded byleonardomarin
- Akticon CM 2045 PDS - I-ChemUploaded byChanBoonChew
- E-104 Structure of Modern EnglishUploaded byheera