# Question 1 : Write a program to calculate the simple interest. Accept the values of principal, rate of interest and time from the user through standard input device (Keyboard).

Internet και Εφαρμογές Web

4 Δεκ 2013 (πριν από 4 χρόνια και 5 μήνες)

143 εμφανίσεις

I

Question 1 : Write a program to calculate the simple interest. Accept
the values of principal, rate of interest and time from the user through
standard input device (Keyboard).

Solution :

// CALCULATE SIMPLE INTEREST

#include<stdio.h>

void main()

{

fl
oat p,r,t,i;

clrscr();

printf("Enter principal amount : ");

scanf("%f",&p);

printf("
\
nEnter Rate of Interest : ");

scanf("%f",&r);

printf("
\
nEnter time period : ");

scanf("%f",&t);

i=(p*r*t)/100;

printf("
\
nInterest calculated is %f",i);

getch();

}

Output :

Enter principal amount : 5000

Enter Rate of Interest : 10.5

Enter time period : 7

Interest calculated is 3675.000000

Question 2 : Write a program to find the sum of digits of a give four
digit number.

Solution :

// SUM OF FOUR DIGIT NUMB
ER

#include<stdio.h>

void main()

{

int r,f,s,t,fo,sum;

II

clrscr();

printf("Enter a four digit number : ");

scanf("%d",&r);

f=r%10;

r=r/10;

s=r%10;

r=r/10;

t=r%10;

fo=r/10;

sum=f+s+t+fo;

printf("
\
nsum is %d",sum);

getch();

}

Output :

Enter a four digit number : 5487

sum is 24

Question 3 : Write a program to reverse the given four digit number.

Solution :

// REVERSE OF A FOUR DIGIT NUMBER

#include<std
io.h>

void main()

{

int r,f,s,t,fo,sum;

clrscr();

printf("Enter a four digit number : ");

scanf("%d",&r);

f=r%10;

r=r/10;

s=r%10;

r=r/10;

t=r%10;

fo=r/10;

r=(f*1000)+(s
*100)+(t*10)+fo;

printf("
\
nREVERSED NUMBER IS %d",r);

getch();

}

Output :

III

Enter a four digit number : 8509

REVERSED NUMBER IS 9058

Question 4 : Write a program to the compound interest for the 5 years
duration.

Solution :

// CALCULATE

COMPOUND INTEREST

#include<stdio.h>

#include<math.h>

void main()

{

float p,r,t,i;

clrscr();

printf("Enter Principle amount : ");

scanf("%f",&p);

printf("
\
nEnter rate of interest : ");

scanf("%f",&r);

i=p*(pow((1+(r/100)),5));

printf("
\
nCompound Interest is %f",i);

getch();

}

Output :

Enter Principle amount : 4200

Enter rate of interest : 7

Compound Interest is 5890.717285

Question 5 : Write a program to calculate the simple inter
est. If time is
>=10 years then rate of interest is 10% otherwise rate of interest is
5%(Use ternary operator).

Solution :

// CALCULATE SIMPLE INTEREST USING TERNARY OPERATOR

void main()

{

float p,t,a;

IV

clrscr();

printf("Enter the

principal : ");

scanf("%f",&p);

printf("Enter the time duration : ");

scanf("%f",&t);

t>=10?(a=(p*10.0*t)/100):(a=(p*5.0*t)/100);

printf("
\
nInterest calculated is %f",a);

getch();

}

Output :

Enter the prin
cipal : 1200

Enter the time duration : 8

Interest calculated is 480.000000

Question 6 : A 4 digit number WXYZ is called an ordered number if the
difference between the first two digits, WX and the last two digits YZ is
eqal to 1(WX
-
YZ=1 or YZ
-
WX=1) for e
xample 1213 and 4645 are
ordered numbers, while 2345 and 7685 are not ordered numbers. Write
a program that will only accept a 4
-
digit number and determines
whether it is ordered number or not.

Solution :

// ORDERED NUMBER OR NOT

#include<stdio.h>

void
main()

{

int n,a;

clrscr();

printf("Enter number : ");

scanf("%d",&n);

if(n>=1000&&n<=9999)

{

a=n%100;

n=n/100;

if(a
-
n==1||n
-
a==1)

{

printf("Number is ordered pair");

}

else

printf("Number is not ordered");

V

}

else

printf("
\
nInva
lid number entered");

getch();

}

Output :

Enter number : 5455

Number is ordered pair

Question 7 : Check a number for Armstrong number e.g. 153 is
Armstrong number (1*1*1+5*5*5+3*3*3=153)

Solution :

// ARMSTRONG NUMBER

#include<stdio.h>

#include<ma
th.h>

void main()

{

int n,i=0,k,j,f=0,a[5];

clrscr();

printf("Enter a number : ");

scanf("%d",&n);

j=n;

do

{

a[i]=n%10;

n=n/10;

i++;

}

while(n>0);

for(k=0;k<i;k++)

{

f=f+pow(a[k],i);

}

if(f==j)

{

printf("
\
n
\
nArmstrong number");

}

else

printf
("
\
n
\
nNot an armstrong number"
);

getch();

}

VI

Output :

Enter a number : 407

Armstrong number

Question 8 : Write a program to check a number for perfect e.g. 6 is a
perfect number.

Solution :

//PERFECT NUMBER

#include<stdio.h>

#include<m
ath.h>

void main()

{

int a,l,k,n,m=0;

clrscr();

printf("Enter the number : ");

scanf("%d",&n);

a=n;

while(n!=0)

{

n=n/2;

m++;

}

k=m/2;

l=m
-
k;

n=(pow(2,k))*(pow(2,l)
-
1);

if(a==n)

printf("
\
nPerfect number!!");

else

printf("
\
nNot a perfe
ct number!!");

getch();

}

Output :

Enter the number : 496

Perfect number!!

VII

Question 9 : A whole number is said to be circular if when you multiply
the number by its units decimal digits the result is the number with its
decimal digit rotated to the th
e firth with the using digit becoming it is
high order digit. For example 102564 is a circular number because in
the multiplication

102564

X 4

-------------

410256

-------------

taking 4 at the right of 102564 and moving it to the left to get 4102
56
can form the resulting number. Write a program to test whether a given
number is a circular.

Solution :

// CIRCULAR OR NOT

#include<stdio.h>

void main()

{

long int n,a,b;

clrscr();

printf("Enter number a six digit number: ");

scanf("%ld",&n);

if
(n>=100000&&n<=999999)

{

a=n%10;

b=n*a;

n=n/10;

n=(a*100000)+n;

if(b==n)

{

printf("Number is circular");

}

else

printf("Number is not circular");

}

else

printf("
\
nInvalid number entered");

getch();

}

VIII

Output :

Enter number a si
x digit number: 102564

Number is circular

Question 10 : A department store places an order with a company for n
pieces of miners, m pieces of toasters and p number of fans the cost of
these items is as follows:
-

Miners

:

Rs. 1500 per piece

Toaster

:

R
s. 200 per piece

Fan

:

Rs. 450 per piece

The discount allowed for various items are 5% for miners, 15%
for fan and

10%

for toaster. The company charge 10% as sales tax on
all items on net

value after

deducting the discount. Write a program
that
reads m, n and p

and computes the

amount to be paid by
the store.

Solution :

//STORE

#include<stdio.h>

void main()

{

int n,m,p;

float cost=0,st;

clrscr();

printf("Enter number of Miners: ");

scanf("%d",&n);

printf("Enter number of Toasters: ");

scanf("%d",&m);

printf("Enter number of Fan: ");

scanf("%d",&p);

cost=1425*n;

cost=cost+(180*m);

cost=cost+(382.5*p);

st=cost/10;

cost=cost+st;

printf("Amount to be paid is : %f",cost);

getch();

}

IX

Output :

Enter number of Miners: 6

Enter numbe
r of Toasters: 7

Enter number of Fan: 10

Amount to be paid is : 14998.500000

Question 11 : Write a program to calculate LCM and GCD of two
numbers.

Solution :

// LCM OF TWO NUMBERS

#include<stdio.h>

void main()

{

int n,m,i=2,lcm=1,max,maxi;

clrscr();

printf("Enter the first number : ");

scanf("%d",&n);

printf("Enter the second number : ");

scanf("%d",&m);

maxi=n*m;

if(m>n)

max=m;

else

max=n;

do

{

if(n%i==0&&m%i==0)

{

n=n/i;

m=m/i;

lcm=lcm*i;

}

else

{

i++;

}

}while
(i<=max);

printf("LCM is %d",lcm*n*m);

printf("
\
n
GCD is %d",maxi/(lcm*n*m));

getch();

}

X

Output :

Enter the first number : 54

Enter the second number : 81

LCM is 162

GCD is 27

Question 12 : Write a program to generate the Fibonacci series upto a
given

number.

Solution :

// FIBONACCI SERIES

#include<stdio.h>

void main()

{

int i,n,a=0,b=0,c=1;

clrscr();

printf("Enter the elements required in the series : ");

scanf("%d",&n);

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

{

a=b;

b=c;

c=a+b;

printf("%d ",a);

}

getch(
);

}

Output :

Enter the elements required in the series : 10

0 1 1 2 3 5 8 13 21 34

Question 13 : . Write a program to display the table of a given number
output is like 5*1=5.

Solution :

// TABLES

#include<stdio.h>

XI

void main()

{

int n,i,k;

char c;

clrscr();

do

{

printf("Enter the number you want table of: ");

scanf("%d",&n);

for(i=1;i<=10;i++)

{

k=n*i;

printf("%d * %d = %d
\
n",n,i,k);

}

printf("do u want to continue <y/n>: ");

fflush(stdin);

scanf("%c",&c);

}

while(c=='y'|
|c=='Y');

getch();

}

Output :

Enter the number you want table of: 19

19 * 1 = 19

19 * 2 = 38

19 * 3 = 57

19 * 4 = 76

19 * 5 = 95

19 * 6 = 114

19 * 7 = 133

19 * 8 = 152

19 * 9 = 171

19 * 10 = 190

do u want to continue <y/n>:

Question 14 : . Income tax f
or individual is computed on slab rates as
follows.

Income

Tax payable

Upto Rs. 50,000

Nil

From Rs. 50,001 to Rs. 60,000

10% of the excess over Rs. 50,000

From Rs. 60,001 to Rs. 1,00,000

20% of the excess over Rs. 60,000

Above Rs. 1,00,000

30% of the

excess over Rs. 1,00,000

XII

Write a program that reads the income and print the income tax due.

Solution :

//INCOMETAX SLAB

#include<stdio.h>

void main()

{

long int n;

float tax,temp;

clrscr();

printf("Enter the annual income : ");

scanf("%ld",&n);

if(n<=50000)

{

printf("No Tax To Be Paid !");

}

else if(n>=50001&&n<=60000)

{

temp=n
-
50000;

tax=temp/10;

printf("Tax To Be Paid Is %f",tax);

}

else if(n>=60001&&n<=100000)

{

tax=1000;//for slab 50001
-
60000.

temp=n
-
60000;

tax=tax+(tem
p/5);

printf("Tax To Be Paid Is %f",tax);

}

else if(n>100000)

{

tax=9000;//for slab 50001
-
60000 & 60001
-
100000

temp=n
-
100000;

tax=tax+((temp*3)/10);

printf("Tax To Be Paid Is %f",tax);

}

getch();

}

Output :

Enter the annual income : 125000

Tax To Be Paid Is 16500.000000

XIII

Question 15 : The monthly telephone bill is to be computed as follows:

Minimum Rs. 200 for upto 100 calls

Plus Rs. 0.60 per call for next 50 calls

Plus Rs. 0.50 per call for next 50 calls

Plus Rs. 0.40 per call for any call

beyond 200 calls

Solution :

//Telephone bill

#include<stdio.h>

void main()

{

int n;

float k=200,temp,l,j;

clrscr();

printf("Enter the number of calls : ");

scanf("%d",&n);

if(n<=100)

{

printf("Amount to be paid is RS.%f",k);

}

else if(n>100&
&n<=150)

{

temp=0.6*50;

k=k+temp;

printf("Amount To Be Paid Is %f",k);

}

else if(n>150&&n<=200)

{

j=0.6*50;

n=n
-
150;

temp=0.5*n;

k=k+temp+j;

printf("Amount to be paid is %f",k);

}

else if(n>200)

{

j=0.6*50;

l=0.5*50;

n=n
-
200;

temp=0.4*n;

k=k+j+l+temp;

printf("Amount To Be Paid Is %f",k);

XIV

}

getch();

}

Output :

Enter the number of calls : 700

Amount To Be Paid Is 455.000000

Question 16 : Write a program to find factorial of a given number.

Solution :

// FACTORIAL OF
A NUMBER

#include<stdio.h>

void main()

{

float n,i,a=1;

clrscr();

printf("Enter a number : ");

scanf("%f",&n);

for(i=n;i>0;i
--
)

{

a=a*i;

}

printf("Factorial is %f",a);

getch();

}

Output :

Enter a number : 8

Factorial is 40320.000000

Question 17 :
Write a program to print factorial of five numbers.

Solution :

// FACTORIAL OF FIVE NUMBER

#include<stdio.h>

void main()

{

int i,j;

XV

float n[5],a=1;

clrscr();

for(i=0
;i<5;i++)

{

printf("Enter the %d number : ",i+1);

scanf("%f",&n[i]);

}

for(j=0;j<5;j++)

{

for(i=n[j];i>0;i
--
)

{

a=a*i;

}

printf("
\
nFactorial of %f is %f",n[j],a);

a=1;

}

getch();

}

Output :

Enter the 1 number : 2

Enter the 2 number

: 6

Enter the 3 number : 8

Enter the 4 number : 3

Enter the 5 number : 12

Factorial of 2.000000 is 2.000000

Factorial of 6.000000 is 720.000000

Factorial of 8.000000 is 40320.000000

Factorial of 3.000000 is 6.000000

Factorial of 12.000000 is 479001600.00
0000

Question 18 : . Write a program to print Menu driven program:
-

1. Add

2. Multiply

3. Subtract

4. Divide

5. Exit

Enter choice:

Solution :

// MENU DRIVEN PROGRAM TO APPLY ARITHMATIC OPERATORS

XVI

#include<stdio.h>

void main()

{

int a;

float b,d;

char c;

clrscr();

do

{

printf("ENTER THE NUMBER FOR THE DESIRED OPERATION
\
n");

printf("1. ADDITION
\
n2. SUBTRACTION
\
n3. MULTIPLICATION
\
n4.
DIVISION
\
n5. TO EXIT
\
n");

printf("ENTER CHOICE ");

scanf("%d",&a);

if(a>0&&a<5)

{

printf("
\
nENTER THE FIRST

NUMBER: ");

scanf("%f",&b);

printf("
\
nENTER THE SECOND NUMBER: ");

scanf("%f",&d);

}

else

printf("
\
n
\
nInvalid choice Entered !");

switch(a)

{

case 1:

printf("
\
nSUM IS %10.2f",b+d);

break;

case 2:

printf("
\
nDIFFERENCE

IS %10.2f",b
-
d);

break;

case 3:

printf("
\
nPRODUCT IS %10.2f",b*d);

break;

case 4:

printf("
\
nQUOTIENT IS %10.2f",b/d);

break;

case 5:

exit();

}

printf("
\
n
\
nDO U WANT TO CONTINUE <Y/N>??
\
n");

XVII

fflush(stdin);

scanf("%c",&c);

}

while(c=='y'||c=='Y');

getch();

}

Output :

ENTER THE NUMBER FOR THE DESIRED OPERATION

1. ADDITION

2. SUBTRACTION

3. MULTIPLICATION

4. DIVISION

5. TO EXIT

ENTER CHOICE 3

ENTER THE FIRST NUMBER: 21

ENTER THE SECOND NUMBER: 84

PRODUCT IS 1764.00

DO

U WANT TO CONTINUE <Y/N>??

Question 19 : Write a program to read five values in an array and print
them in reverse order.

Solution :

// DISPLAY CONTENTS OF AN ARRAY IN REVERSE..

#include<stdio.h>

void main()

{

int a[5],i;

clrscr();

for(i=0;i<5;i++)

{

printf("
\
nENTER %d value : ",i+1);

scanf("%d",&a[i]);

}

printf("
\
n
\
nREVERSE ARRAY IS: ");

for(i=4;i>=0;i
--
)

{

printf("%d ",a[i]);

XVIII

}

getch();

}

Output :

ENTER 1 value : 54

ENTER 2 value : 27

ENTER 3 value : 84

ENTER 4 value : 66

ENTER
5 value : 30

REVERSE ARRAY IS: 30 66 84 27 54

Question 20 :

Write a program to read five values in any array and
print the greatest and lowest value.

Solution :

// MAX AND MIN FROM AN ARRAY..

#include<stdio.h>

void main()

{

int max,min,a[5],i;

clr
scr();

for(i=0;i<5;i++)

{

printf("
\
nENTER %d value : ",i+1);

scanf("%d",&a[i]);

}

max=a[0];

min=a[0];

for(i=0;i<5;i++)

{

if(min>a[i])

{

min=a[i];

}

if(max<a[i])

{

XIX

max=a[i];

}

}

printf("MAX VALUE IS %d
\
nMIN VALUE IS %d",max,m
in);

getch();

}

Output :

ENTER 1 value : 12

ENTER 2 value : 65

ENTER 3 value : 87

ENTER 4 value : 41

ENTER 5 value : 190

MAX VALUE IS 190

MIN VALUE IS 12

Question 21 : . Write a program to sort a list of n elements of an array
using Bubble sort.

Solution :

// BUBBLE SORT

#include<stdio.h>

void main()

{

int n,a[10],i,j,t;

clrscr();

printf("Enter the values u wish to enter [<10] : ");

scanf("%d",&n);

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

{

printf("Enter the %d value : ",i+1);

scanf("%d",&a[i]);

}

for(j=
0;j<n
-
1;j++)

{

XX

for(i=0;i<n
-
1;i++)

{

if(a[i]>a[i+1])

{

t=a[i+1];

a[i+1]=a[i];

a[i]=t;

}

}

}

printf("
\
n
\
nSorted array is
\
n");

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

{

printf("%d ",a[i]);

}

getch();

}

Output :

Enter the values u wish to enter

[<10] : 6

Enter the 1 value : 54

Enter the 2 value : 12

Enter the 3 value : 94

Enter the 4 value : 38

Enter the 5 value : 66

Enter the 6 value : 05

Sorted array is

5 12 38 54 66 94

Question 22 : Write a program to sort a list of n elements of an array
using Selection sort.

Solution :

// SELECTION SORT

#include<stdio.h>

void main()

{

int n,i,a[10],min,j,val,pos;

clrscr();

printf("Enter the values u want to enter [<10] : ");

scanf("%d",&n);

XXI

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

{

printf("Enter the %d value : ",i+1
);

scanf("%d",&a[i]);

}

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

{

min=a[i];

val=a[i];

pos=i;

for(j=i;j<n;j++)

{

if(a[j]<min)

{

min=a[j];

pos=j;

}

}

a[i]=min;

a[pos]=val;

}

printf("
\
n
\
nSorted array is ");

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

{

printf("%d "
,a[i]);

}

getch();

}

Output :

Enter the values u want to enter [<10] : 5

Enter the 1 value : 54

Enter the 2 value : 68

Enter the 3 value : 12

Enter the 4 value : 41

Enter the 5 value : 08

Sorted array is 8 12 41 54 68

Question 23 : Write a program t
o sort a list of n elements of an array
using Insertion sort.

Solution :

XXII

// INSERTION SORT..

#include<stdio.h>

void main()

{

int a[10],i,j,k,temp,n;

clrscr();

printf("ENTER THE NUMBER OF VALUES U WISH TO ENTER(<10) :");

scanf("%d",&n);

printf("Enter

the numbers ");

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

{

scanf("%d",&a[i]);

}

for(j=1;j<n;j++)

{

while(a[j]<a[j
-
1]&&j>0)

{

temp=a[j
-
1];

a[j
-
1]=a[j];

a[j]=temp;

j
--
;

}

}

printf("
\
nSORTED ARRAY IS :");

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

{

print
f("%d
\
n",a[i]);

}

getch();

}

Output :

ENTER THE NUMBER OF VALUES U WISH TO ENTER(<10) :7

Enter the numbers 12

84

54

27

89

118

22

SORTED ARRAY IS :12

22

27

54

XXIII

84

89

118

Question 24 : Print the following:

1 2 3 4 3 2 1

1 2 3 3 2 1

1 2 2 1

1 1

Solution :

Output :

Question 25 : Write a program to find:

a) Row wise sum

b) Column wise sum

c) Diagonal wise sum(both)

Solution :

// sum of rows, columns..

#include<stdio.h>

void main()

{

static int a[4][4];

int c=5,r=5,i,j,s
um=0;

clrscr();

printf("Enter the elements of 3*3 matrix :");

for(i=0;i<3;i++)

{

for(j=0;j<3;j++)

{

gotoxy(c,r);

scanf("%d",&a[i][j]);

c=c+5;

}

r=r+5;

c=5;

}

for(i=0;i<3;i++)

{

for(j=0;j<3;j++)

XXIV

{

a[i][3]=a[i][3]+a[i][j];

a[3][i]=a[3][i]+a[j][i];

if(i==j)

{

a[3][3]=a[3][3]+a[i][j];

}

if(i+j==2)

{

sum=sum+a[i][j];

}

}

}

printf("
\
nOutput is :");

c=10;

r=20;

for(i=0;i<4;i++)

{

for(j=0;j<4;j++)

{

gotoxy(c,r);

printf("%d",a[i][j]);

c=c+5;

}

r=r+5;

c=10;

}

gotoxy(5,35);

printf("%d",sum);

getch();

}

Output :

Enter the elements of 3*3 matrix :

4 5 8

95 4 1

XXV

65 77 1

Output is :

4 5 8 17

95 4 1 10
0

65 77 1 143

77 164 86 10 9

Question 26 :
Write a program to find(using library and without library
functions)

a)

Length of a string

b)

Combine two strings

Solution :

// LENGTH OF STRING USING LIBRARY FUNCTION

#include<std
io.h>

void main()

{

int n;

char s[10];

clrscr();

printf("Enter a string : ");

gets(s);

n=strlen(s);

printf("
\
nLength of string is : %d",n);

XXVI

getch();

}

Output :

Enter a string : hello

Length of string is : 5

// LENGTH OF STRING WITHOUT USING LI
BRARY FUNCTION

#include<stdio.h>

void main()

{

int n=0,i=0;

char s[10];

clrscr();

printf("Enter a string : ");

gets(s);

while(s[i]!='
\
0')

{

n++;

i++;

}

printf("
\
nLength of string is : %d",n);

getch();

}

Output :

Enter a string : myworld

Length of string is : 7

// COMBINATION OF STRINGS USING LIBRARY FUNCTION

#include<stdio.h>

void main()

{

int n;

char a[20],b[10];

clrscr();

printf("Enter the first string : ");

gets(a);

XXVII

fflush(stdin);

printf("Enter the second string : ");

gets(b
);

strcat(a,b);

printf("
\
nConcatinated string is : %s",a);

getch();

}

Output :

Enter the first string : hello

Enter the second string : world

Concatinated string is : helloworld

// COMBINATION OF STRINGS WITHOUT USING LIBRARY FUNCTION

#include<st
dio.h>

void main()

{

int n,m,i,j=0;

char a[20],b[10];

clrscr();

printf("Enter the first string : ");

gets(a);

fflush(stdin);

printf("Enter the second string : ");

gets(b);

n=strlen(a);

m=strlen(b);

for(i=n;i<n+m;i++)

{

a[i]=b[j];

j++;

}

a[i]='
\
0';

printf("
\
nConcatinated string is : %s",a);

getch();

}

Output :

Enter the first string : good

Enter the second string : morning

XXVIII

Concatinated string is : goodmorning

Question 27 : Write a program to convert a given string in the
following:

a.

Title Case

b.

tOGGLE cASE

Solution :

//CONVERT STRING

#include<stdio.h>

void main()

{

int n,i;

char c[10];

clrscr();

printf("ENTER THE STRING:
\
n");

gets(c);

printf("PRESS 1 TO CONVERT STRING TO
TITLE CASE
");

printf("
\
nPRESS 2 TO CONVERT STRING TO

TOGGLE CASE
");

printf("
\
n
\
nENTER THE CHOICE : ");

scanf("%d",&n);

switch(n)

{

case 1:

for(i=0;i<10;i++)

{

c[i]=toupper(c[i]);

}

break;

case 2
:

for(i=0;i<10;i++)

{

if(islower(c[i]))

c[i]=toupper(c[i]);

else

c[i]=tolower(c[i]);

}

break;

}

printf("%s",c);

getch();

}

XXIX

Output :

ENTER THE STRING:

HelLo u

PRESS 1 TO CONVERT STRING TO
TITLE CASE

PRESS 2 TO CONVERT STRING TO
TOGGLE CASE

ENTER THE CHOICE :
2

hELlO U

Question 28 : Write a program to change multiple occurrences of each
character inot a single occurrence.

Solution :

// replace duplicate characters

#include<stdio.h>

void main()

{

static char a[20],b[20];

int i,n,y=0;

clrscr();

printf("E
nter the string ");

gets(a);

n=strlen(a);

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

{

if(a[i]!=a[i+1]&&a[i]!='
\
0')

{

b[y]=a[i];

y++;

}

if(a[i]=='
\
0')

{

b[y]=a[i];

}

}

puts(b);

getch();

}

Output :

Enter the string aapplleyy

XXX

apley

Question 29 : Write a

program to accept a string and two words. Search
first word into the string and if it exists replace it with second.

Solution :

Output :

Question 30 : Write a function say int prime (int n), that returns 1 if
number n is prime else return 0.

Solution

:

// Prime number

#include<stdio.h>

int prime(int);

void main()

{

int x;

clrscr();

printf("Enter the number : ");

scanf("%d",&x);

x=prime(x);

printf("
\
n
\
nnumber is %d",x);

printf("
\
n1 represents prime
\
n0 represents non
-
prime");

getch();

}

int pr
ime(int x)

{

int i,flag=1;

for(i=2;i<x;i++)

{

if(x%i==0)

flag=0;

}

if(flag==1)

return(flag);

else

return(flag);

}

Output :

XXXI

Enter the number : 53

number is 1

1 represents prime

0 represents non
-
prime

Question
31 : A Positive integer num
ber IJK is said to be well ordered
if I<J<K. for example number 138 is called well ordered because the
digits in the number (1,3,8) increase from left to right i.e. 1<3<8.
Number 365 is not well ordered because 6 is larger than 5. Write a
function, sa
y int is_wellorder (unsigned int k), that returns value 1 if the
k is well ordered number else return 0.

Solution :

// wellordered..

#include<stdio.h>

int well_ordered(int);

void main()

{

int n;

clrscr();

printf("Enter the number : ");

scanf("%d",&n
);

n=well_ordered(n);

printf("
\
n
\
noutput %d",n);

printf("
\
n
\
n1 represents well ordered
\
n0 if not");

getch();

}

int well_ordered(int x)

{

static int y[5];

int i=0,flag,j;

while(x!=0)

{

y[i]=x%10;

x=x/10;

i++;

}

for(j=0;j<i
-
1;j++)

{

if(y[
j+1]<y[j])

XXXII

flag=1;

else

flag=0;

}

return(flag);

}

Output :

Enter the number : 249

output 1

1 represents well ordered

0 if not

Question 32 :
Write a function say in is_fib_prime(int n), that returns
value 1 if the nth Fibonacci number is prime
. Your function should call
two other functions one say int Fibonacci(int n), that returns nth
Fibonacci number the second say int is_prime (int k) that returns 1 if
number k is prime else returns 0.

Solution :

// CHECK PRIME NUMBER IN FIBONACCI SERIES U
SING FUNCTIONS

#include<stdio.h>

#include<math.h>

int fibonacci(int);

int prime(int);

void main()

{

int n;

clrscr();

printf("Enter the number of elements in series : ");

scanf("%d",&n);

n=fibonacci(n);

if(prime(n)==0)

printf("
\
nNumber is prime !!
");

else

printf("
\
nNumber is not prime !!");

getch();

}

XXXIII

int fibonacci(int a)

{

int i,m=0,n=0,o=1;

for(i=0;i<a;i++)

{

m=n;

n=o;

o=m+n;

printf("%d ",m);

}

return(m);

}

int prime(int a)

{

int flag=0,i;

for(i=2;i<=sqrt(a);i++)

{

if(a%i=
=0)

flag=1;

}

return(flag);

}

Output :

Enter the number of elements in series : 18

0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597

Number is prime !!

Question 33 : Write a recursive function say int fact(int n), that returns
the factorial of
a given number.

Solution :

// FACTORIAL USING FUNCTIONS

#include<stdio.h>

int fact(int);

void main()

{

int n;

clrscr();

printf("Enter the number : ");

XXXIV

scanf("%d",&n);

n=fact(n);

printf("
\
nFactorial of number is : %d",n);

getch();

}

int fact(int

a)

{

int f=1,i;

for(i=1;i<=a;i++)

{

f=f*i;

}

return(f);

}

Output :

Enter the number : 7

Factorial of number is : 5040

Question 34 : A car manufacturing company records the following
information about their cars.

Data item

type

Length

Eng
ine_no

char

6

Chasy_no

char

8

Year_of_manufacturing

int

---

Horse_power

float

---

Assume that there are not more than 5 cars. Design a structure to store
date about a car and using this structure write a program that accepts
the dat
a about cars from the user and retrieves the data about a car
whose engine _no is given.

Solution :

// Car records

#include<stdio.h>

struct cars

{

char eno[6];

XXXV

char chasis[8];

int year;

float horsep;

}c[10];

void linkfloat()

{

float *a,*b;

b= &
a;

a= &b;

}

void main()

{

int n,i,j,m;

char d[10];

clrscr();

printf("Enter the number of entries : ");

scanf("%d",&n);

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

{

fflush(stdin);

printf("Enter Engine number : ");

gets(c[i].eno);

fflush(stdin);

printf("
\
nEnter chas
is number : ");

gets(c[i].chasis);

fflush(stdin);

printf("
\
nEnter year of manufacture : ");

scanf("%d",&c[i].year);

printf("
\
nEnter horse power : ");

scanf("%f",&c[i].horsep);

}

fflush(stdin);

printf("
\
n
\
nEnter the Engine number to be search
ed : ");

gets(d);

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

{

if(strcmp(d,c[i].eno)==0)

break;

}

printf("
\
nEngine number %s",c[i].eno);

printf("
\
nChasis number %s",c[i].chasis);

printf("
\
nYear of manufacture %d",c[i].year);

printf("
\
nHorse power %f",c[i].horsep);

printf("
\
n
\
n");

getch();

XXXVI

}

Output :

Enter the number of entries : 3

Enter Engine number : 126

Enter chasis number : 547

Enter year of manufacture : 2008

Enter horse power : 1200

Enter Engine number : 547

Enter chasis number : 448621

Enter year o
f manufacture : 2010

Enter horse power : 5000

Enter Engine number : 300

Enter chasis number : 841265

Enter year of manufacture : 2008

Enter horse power : 506

Enter the Engine number to be searched : 547

Engine number 547

Chasis number 448621

Year o
f manufacture 2010

Horse power 5000.000000

Question 35 :
Write a program to accept a range(n) and enter the
records of the employees upto number n and then display them using
pointers.

Solution :

Output :

XXXVII

Question 36 : Write a program on salary sheet

of employees to accept
the elements of the structures(I) Emp No. and (ii) Basic pay (iii) Name
(iv) Department and display the same structure along with the DA,
HRA,CCA and Gross salary, The DA and CCA are calculated as:

DA

=

51% of Basic pay

CCA

=

Rs. 1
00/
-

HRA

=

15% of Basic pay & max Rs. 800/
-

Solution :

// SALARY OF EMPLOYEE

#include<stdio.h>

struct employee

{

char name[20];

float basic;

int eno;

char dept[10];

float da;

float cca;

float hra;

float total;

}e[10];

void linkfloat()

{

float

*a,*b;

a=&b;

b=&a;

}

void main()

{

int i,j,n;

clrscr();

printf("Enter the number of number of entries : ");

scanf("%d",&n);

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

XXXVIII

{

printf("
\
nEnter name : ");

fflush(stdin);

gets(e[i].name);

printf("Enter employee number : ");

scanf("%d",&e[i].eno);

printf("Enter department name : ");

fflush(stdin);

gets(e[i].dept);

printf("Enter basic salary : ");

scanf("%f",&e[i].basic);

}

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

{

e[i].da=0.51*e[i].basic;

e[i].cca=100;

e[i].hra=(0.15*e[i].basic>
800)?800:0.15*e[i].basic;

e[i].total=e[i].da+e[i].cca+e[i].hra+e[i].basic;

printf("
\
n
\
nNAME:%s
\
nEMPLOYEE
NUMBER:%d
\
nDEPARTMENT:%s
\
nBASIC:%f
\
nDA:%f
\
nCCA:%f
\
nHRA:%f
\
nTOTAL:%f",e[i].name,e[i].eno,e[i].dept,e[i].basic,e[i].da,e[i].cca,e[i].hra,e[i].tota
l);

}

getch();

}

Output:

Enter the number of number of entries : 2

Enter name : jack

Enter employee number : 5489

Enter department name : sales

Enter basic salary : 9000

Enter name : william

Enter employee number : 235

Enter department name : distrib
ution

Enter basic salary : 15000

NAME:jack

EMPLOYEE NUMBER:5489

DEPARTMENT:sales

BASIC:9000.000000

DA:4590.000000

CCA:100.000000

XXXIX

HRA:800.000000

TOTAL:14490.000000

NAME:william

EMPLOYEE NUMBER:235

DEPARTMENT:distributi

BASIC:15000.000000

DA:7650.000000

C
CA:100.000000

HRA:800.000000

TOTAL:23550.000000

Question 37 : Write a program which will calculate income tax and total
tax for ground of 150 persons at following rates.

i.

Taxable income upto 40000

No tax

ii.

Taxable income from 40000
-
60000

Income tax=3000+
30%

iii.

Taxable income from 60000
-
120000

iv.

Taxable income over 120000

Income tax =21000+40%

Surcharge on tax=0.2*income tax

If income above 1,50,000

Write a program to calculate the tax to be payable by the person using
structure the program should accept incom
e tax, no., name , address,
the gross income and tax payable.

Solution :

// INCOMETAX

#include<stdio.h>

double caltax(double);

struct income

{

double tax;

int eno;

char name[20];

char addr[30];

double inc;

}m[10];

XL

void linkfloat()

{

float *a,*b;

a=&b;

b=&a;

}

void main()

{

int i,n;

clrscr();

printf("Enter the number of entries : ");

scanf("%d",&n);

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

{

fflush(stdin);

printf("Enter name : ");

gets(m[i].name);

fflush(stdin);

printf("Enter income tax number : ");

s
canf("%d",&m[i].eno);

fflush(stdin);

printf("Enter address : ");

gets(m[i].addr);

printf("Enter income : ");

scanf("%lf",&m[i].inc);

printf("
\
n");

m[i].tax=caltax(m[i].inc);

}

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

{

puts(m[i].name);

printf("
\
t%lf
\
n",m[i].t
ax);

}

getch();

}

double caltax(double a)

{

double b,c;

if(a<40000)

{

b=0;

}

if(a>40000&&a<=120000)

{

XLI

b=3000+((30*a)/100);

}

if(a>120000&a<=150000)

{

b=21000+((40*a)/100);

}

if(a>150000)

{

c=21000+((40*a)/100);

b=0.2*c;

}

return(
b);

}

Output :

Enter the number of entries : 2

Enter name : alex

Enter income tax number : ab123

Enter address : philadelhia

Enter income : 500000

Enter name : kylr

Enter income tax number : 12x3

Enter address : cuba

Enter income : 350000

alex

44200.000000

kylr

32200.000000

Question 38 : Write a program to find(using library and without library
functions)

a.

Copy one string into another

b.

Compare two string

c.

Reverse a string

Solution :

// COPY ONE STRING TO ANOTHER USING LIBRARY FUNCTION

#include<stdio.h>

void main()

XLII

{

char a[10],b[10];

clrscr();

printf("Enter the first string : ");

gets(a);

fflush(stdin);

printf("Enter the second string : ");

gets(b);

strcpy(a,b);

printf("
\
nCopied string is : %s",a);

getch();

}

Output :

Ente
r the first string : pizza

Enter the second string : hut

Copied string is : hut

// COPY ONE STRING TO ANOTHER WITHOUT USING LIBRARY FUNCTION

#include<stdio.h>

void main()

{

char a[10],b[10];

int n,i,j=0;

clrscr();

printf("Enter the first string :
");

gets(a);

fflush(stdin);

printf("Enter the second string : ");

gets(b);

n=strlen(b);

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

{

a[i]=b[j];

j++;

}

a[i]='
\
0';

printf("
\
nCopied string is : %s",a);

getch();

}

Output :

XLIII

Enter the first string : hello

Enter the seco
nd string : world

Copied string is : world

// COMPARE TWO STRINGS USING LIBRARY FUNCTION

#include<stdio.h>

void main()

{

int n;

char a[10],b[10];

clrscr();

printf("Enter the first string : ");

gets(a);

fflush(stdin);

printf("Enter the second st
ring : ");

gets(b);

n=strcmp(a,b);

if(n==0)

{

printf("
\
nString are equal !!");

}

else

{

printf("
\
nStrings are unequal !!");

}

getch();

}

Output :

Enter the first string : aMouNT

Enter the second string : aMouNT

String are equal !!

// COMPA
RE TWO STRINGS WITHOUT USING LIBRARY FUNCTION

#include<stdio.h>

void main()

{

int n,m,i,flag=0;

XLIV

char a[10],b[10];

clrscr();

printf("Enter the first string : ");

gets(a);

fflush(stdin);

printf("Enter the second string : ");

gets(b);

n=strlen(a);

m=strlen(b);

if(m==n)

{

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

{

if(a[i]!=b[i])

flag=1;

}

if(flag==0)

printf("
\
nString are equal !!");

else

printf("
\
nStrings are unequal !!");

}

else

printf("
\
nStrings are unequal !!");

getch();

}

Output :

Enter
the first string : pROGRAMMINg

Enter the second string : ProgramminG

Strings are unequal !!

// REVERSE A STRING USING LIBRARY FUNCTION

#include<stdio.h>

void main()

{

char a[10];

clrscr();

printf("Enter the string : ");

gets(a);

strrev(a);

printf
("
\
nReversed String is : %s",a);

getch();

XLV

}

Output :

Enter the string : sarkar

Reversed String is : rakras

// REVERSE A STRING WITHOUT USING LIBRARY FUNCTION

#include<stdio.h>

void main()

{

int n,k,i;

char a[10],b[10];

clrscr();

printf("Enter th
e string : ");

gets(a);

n=strlen(a);

k=n
-
1;

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

{

b[i]=a[k];

k
--
;

}

b[i]='
\
0';

printf("
\
nReversed String is : %s",b);

getch();

}

Output :

Enter the string : PRESIDENT

Reversed String is : TNEDISERP

Question 39 :
Write a progra
m to convert a given string in the
following:

a.

UPPPER CASE

b.

Lowercase

c.

Sentence Case

Solution :

XLVI

//CONVERT STRING

#include<stdio.h>

void main()

{

int n,
i,m
;

char c[10];

clrscr();

printf("ENTER THE STRING:
\
n");

gets(c);

printf("PRESS 1 TO CONVERT STRI
NG TO UPPER CASE");

printf("
\
nPRESS 2 TO CONVERT STRING TO LOWER CASE");

printf("
\
nPRESS 3 TO CONVERT STRING TO
SENTENCE CASE
");

printf("
\
n
\
nENTER THE CHOICE : ");

scanf("%d",&n);

m=strlen(c);

switch(n)

{

case 1:

for(i=0;i<m
-
1
;i++)

{

c[i]=toupper(c[i]);

}

break;

case 2:

for(i=0;i<m
-
1
;i++)

{

c[i]=tolower(c[i]);

}

break;

case 3
:

c[0]=toupper(c[0]);

for(i=1;i<m
-
1
;i++)

{

c[i]=tolower(c[i]
);

}

break;

default:

break;

}

printf("%s",c);

getch();

}

Output :

XLVII

ENTER THE STRING:

KING SIZE

PRESS 1 TO CONVERT STRING TO UPPER CASE

PRESS 2 TO CONVERT STRING TO LOWER CASE

PRESS 3 TO CONVERT STRING TO
SENTENCE CASE

ENTER TH
E CHOICE : 3

King size

Question 40 : Write a program to input DATA file with n numbers.
Copy even numbers in EVEN file and odd numbers in ODD file from
DATA file.

Solution :

//COPY DATA FROM FILE.. EVEN ODD

#include<stdio.h>

void main()

{

int n,i,j,k
;

FILE *a,*b,*c;

clrscr();

a=fopen("data","w");

printf("Enter the values u want to enter : ");

scanf("%d",&n);

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

{

scanf("%d",&j);

putw(j,a);

}

fclose(a);

a=fopen("data","r");

b=fopen("even","w");

c=fopen("odd","w");

while(
(k=getw(a))!=EOF)

{

if(k%2==0)

putw(k,b);

else

putw(k,c);

}

fclose(a);

fclose(b);

fclose(c);

XLVIII

b=fopen("even","r");

c=fopen("odd","r");

printf("
\
nCONTENTS OF EVEN FILE ARE: ");

while((k=getw(b))!=EOF)

{

printf("%d ",k);

}

printf("
\
nCO
NTENTS OF ODD FILE ARE: ");

while((k=getw(c))!=EOF)

{

printf("%d ",k);

}

fclose(b);

fclose(c);

getch();

}

Output :

Enter the values u want to enter : 10

43

45

11

84

27

63

94

109

2

27

CONTENTS OF EVEN FILE ARE: 84 94 2

CONTENTS OF ODD FILE ARE: 4
3 45 11 27 63 109 27

Question 41 : Write a program to create a file that could store details
about five products. Product details include pro_code, cost, no_of_items
and compute and print the total value of all five products.

Solution :

// PRODUCT DETA
ILS IN FILE

#include<stdio.h>

struct product

XLIX

{

int pcode;

int cost;

int quan;

}p[5];

void main()

{

int n,i,l,k,s;

float total=0;

FILE *a;

clrscr();

a=fopen("product","w");

printf("Enter the number of entries : ");

scanf("%d",&n);

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

{

printf("
\
nEnter product code : ");

scanf("%d",&p[i].pcode);

putw(p[i].pcode,a);

printf("
\
nEnter product cost : ");

scanf("%d",&p[i].cost);

putw(p[i].cost,a);

printf("
\
nEnter product quantity : ");

scanf("%d",&p[i].quan);

putw(p[i]
.quan,a);

}

fclose(a);

a=fopen("product","r");

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

{

s=getw(a);

k=getw(a);

l=getw(a);

total=total+(k*l);

}

fclose(a);

printf("
\
n
\
nTotal cost is %f",total);

getch();

}

Output :

Enter the number of entries : 3

Enter product c
ode : 123

L

Enter product cost : 500

Enter product quantity : 7

Enter product code : 874

Enter product cost : 175

Enter product quantity : 37

Enter product code : 888

Enter product cost : 199

Enter product quantity : 12

Total cost is 12363.000000

Question 42 : Write a program to merge two sorted arrays into third
array ( in sorted order) using pointers.

Solution :

Output :

Question 43 :
Write a function to count number of characters, spaces and words in a
string using pointers.

Solution :

Outp
ut :

Question 44 :
Write a program to accept five strings. Print each string in reverse order
using pointers.

Solution :

Output :

Question 45 :
Demonstrate ‘pointers to functions’ and ‘structures and pointers’.

Solution :

Output :