Solution of C Programming by E Balaguruswamy
Solution of C Programming by E Balaguruswamy
Output:
Output:
a>>------------------>b
#include<conio.h>
#define pi 3.14159
void main()
{
float r,A;
clrscr();
printf("\n\tENTER THE RADIUS OF A CIRCLE=");
scanf("%f",&r);
A=pi*r*r;
printf("\n\n\tArea=%f sqr unit",A);
getch();
}
Output:
ENTER THE RADIUS OF A CIRCLE=2
Area=12.566360 sqr unit
void sub()
{
printf("\n\t%d-%d=%d",20,10,10);
}
Output :
20+10=30
20-10=10
Output:
a)
Enter values of a,b&c
250
85
25
result=4
b)NO OUTPUT
Output :
ENTER TEMPERATURE IN FARENHITE
10
In Celsius scale=-12.222222
Coding :
#include<stdio.h>
#include<conio.h>
void main()
{
float a,F,C;
clrscr();
printf("ENTER TEMPERATURE IN CELSIUS\n");
scanf("%f",&C);
a=(9*C)/5;
F=a+32;
printf("\nIn farenhite scale=%f",F);
getch();
}
Output:
ENTER TEMPERATURE IN CELSIUS
10
In frenhite scale=50.00000
Sample output:
ENTER THE THREE SIDES OF A TRIANGLE=10
12
14
Area of the triangle=58.787754
Output :
ENTER CO-ORDINATES x1,x2,y1,y2=
2
4
8
5
Result=3.605551
Output :
Result=3.14159
A=pi*r*r;
printf("Result=%f",A);
getch();
}
Output :
Result=0.785398
Output :
5x+8y=18
Output :
ENTER TWO NUMBERS=
10
5
Sum=15.000000
Difference=5.000000
Product=50.000000
Division=2.000000
solution of C programming by
E.Balagurusamy
CHAPTER-2
CHAPTER
2
Constants,
Variables,
and Data Types
REVIEW QUESTIONS
STATE WHETHER THE FOLLOWING STATEMENTS
ARE TRUE OR FALSE:
(a) Any valid printable ANSII character can be used in an identifier. ( False )
(b) All variables must be given a type when they are declared. ( True )
(c) Declarations can appear anywhere in a program. ( False )
(d) ANSI C treats the variable name and Name to be same. ( False )
(e) The underscore can be used anywhere in an identifier. ( True )
(f) The keyword void is a data type in C. ( True )
(g) Floating point data constants, by default, denote float type values. ( False )
(h) Like variables, constants have a type. ( True )
(i) Character constants are coded using double quotes. (False )
(j) Initialization is the process of assigning a value to a variable at the time of
declaration. ( true )
(k) All static variables are automatically initialized to zero. ( True )
(l) The scanf function can be used to read only one value at a time. ( False )
(d) A variable can be made constant by declaring it with the qualifier . At the
time of initialization.
Answer: constant
Purpose:
(1) In a program we used it for new line.
(2)In a program we used it for horizontal tab.
=x;
When we are dealing with a very short number we can improve the accurancy of
calculation by using a keyword short before the keyword. Example short int.
When we are dealing with a very large number we can improve the accurancy of
calculation by using a keyword long before the keyword. Example long int.
+100
Answer: ( valid)
75.45E-2
Answer: ( Valid )
-45.6
Answer: ( Valid )
15.75
Answer: ( Invalid )
Reason: sign is not permitted.
-1.79e+4
Answer: (valid)
0.00001234
Answer: ( Valid )
Row Total
Answer: ( Invalid )
Reason: White space is not permitted.
Column total
Answer: ( Invalid )
Reason: White space is not permitted.
#include<stdio.h>
#include<conio.h>
Void main()
{
int n;
float I, sum, t;
clrscr();
printf(1+1/2+1/3++1/n\n);
printf(Enter the value of n\n);
scanf(%d,&n);
sum=0;
for(i=1;i<=n;i++)
{
t=1/i;
sum=sum+t;
}
printf(%f,sum);
getch();
}
Output:
1+1/2+1/3+.+1/n
Enter the value of n
4
2.083333
Solve:
#include<stdio.h>
#include<conio.h>
void main()
{
int b;
float a;
a=15.95;
clrscr();
b=100*a;
printf("%d",b);
getch();
}
Output:
1595
Output
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50
52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 9
8 100
Output:
Enter the value of number1 and number2
15.5
6.6
15.5/6,6=2.348484
***LIST OF ITEMS***
Item
Price
Rice
Rs 16.75
Sugar
Rs 15.00
Solve:
#include<stdio.h>
#include<conio.h>
void main ()
{
float Rice,Sugar;
Rice=16.75;
Sugar=15.00;
clrscr();
printf("***LIST OF ITEMS***\n");
printf("Item \tPrice\n");
printf("Rice\tRs%.2f\n",Rice);
printf("Sugar\tRs%.2f\n",Sugar);
getch();
}
Output:
***LIST OF ITEMS***
Item
Price
Rice
Rs16.75
{
int R,C;
float perimeter;
float area;
C=PI;
R=5;
perimeter=2.0*C*R;
Area = C*R*R;
printf("%f", "%d",&perimeter,&area)
}
Errors:
Cpp 10: Undefined symbol Area.
Cpp 12:statement missing,in function{}
Cpp12: compound statement missing.
Solve:
#include<stdio.h>
#include<conio.h>
#define PI 3.14159
void main()
{
int R,C;
float perimeter,Area;
C=PI;
R=5;
perimeter=2.0*C*R;
Area = C*R*R;
printf("%f %f",perimeter,Area);
getch();
CHAPTER-3
EXERCISE NO.3.1: Given the values of the variables X,Y and Z write a program to rotate
their values such that X has the value of Y,Y has the value of Z and Z has the value of X.
SOLUTION:
#include<stdio.h>
#include<conio.h>
void main()
{
int x,y,z,temp;
clrscr();
printf("Enter the value of x,y,z\n");
scanf("%d %d %d",&x,&y,&z);
temp=x;
x=y;
y=z;
z=temp;
printf("%d %d %d",x,y,z);
getch();
}
EXERCISE NO.3.2: write a program that reads a floating-point number and then displays
right-most digit of the integral part of the number.
SOLUTION:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,e;
float p;
clrscr();
printf("Enter the value of p\n");
scanf("%f",&p);
a=int(p);
printf("%d\n",a);
e=a%10;
if(a>10)
printf("%d\n",e);
getch();
}
EXERCISE NO.3.3. Modify the above program to display to right-most digits of the integral
part of the number.
SOLUTION:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,e;
clrscr();
printf("Enter the value of a\n");
scanf("%d",&a);
e=a%100;
if(a>100)
printf("%d\n%d\n",a,e);
getch();
}
3.4: Write a program that will obtain the length and width of a rectangle from the user and
compute its area and perimeter.
SOLUTION:
#include<stdio.h>
#include<conio.h>
void main()
{
int length,width;
clrscr();
float area,perimeter;
printf("Enter the value of length,width\n");
scanf("%d %d",&length,&width);
area=(length*width);
perimeter=2*(length+width);
printf("%f %f",area,perimeter);
getch();
}
EXERCISE NO.3.5: Given an integer number, write a program that displays the number as
follows:
First line: all digits
Second line: all except first digit
Third line: all except first two digits
Last line: The last digit
For example the number 5678 will be displayed as:
5678
678
8
SOLUTION:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,e,x;
float p;
clrscr();
printf("Enter the value of p\n");
scanf("%f",&p);
a=int(p);
printf("%d\n",a);
e=a%10000;
b=e%1000;
c=b%100;
x=c%10;
if(a>10000)
printf("%d\n%d\n%d\n%d\n",a,e,b,c,x);
else if(a>1000)
printf("%d\n%d\n%d\n",a,b,c,x);
else if(a>100)
printf("%d\n%d\n",a,c,x);
else if(a>10)
printf("%d\n%d\n",a,x);
getch();
}
EXERCISE NO.3.6: The straight-line method of computing the yearly depreciation of the
value of an item is given by
Depreciation=
Write a program to determine the salvage value of an item when the purchase price , years of
service, and the annual depreciation are given.
SOLUTION:
#include<stdio.h>
#include<conio.h>
void main()
{
int years;
float s, d,p;
clrscr();
printf("Enter the value of years,d,p\n");
scanf("%d %f %f",&years,&d,&p);
s=p-(years*d);
printf("%f",s);
getch();
}
EXERCISE NO.3.7: Write the program that will read a real number from the keyboard and
print the following output in one line:
Smallest integer
The given
number
the number
Largest integer
not greater than
the number
SOLUTION:
#include<stdio.h>
#include<conio.h>
void main()
{
float m;
int n,p;
clrscr();
printf("Enter the value of m\n");
scanf("%f",&m);
n=(m/1)+1;
p=m;
printf("%d %f %d",n,m,p);
getch();
}
SOLUTION:
#include<conio.h>
void main()
{
int a,u,t;
float distance;
clrscr();
printf("Enter the value of a,u,t\n");
scanf("%d %d %d",&a,&u,&t);
distance=u*t+(a*t*t)/2;
printf("%f",distance);
getch();
}
EXERCISE NO.3.9: In inventory management ,the Economic Order Quantity for a single
item is given by
EOQ=sqrt { ( 2*demand rate*setup rate ) / ( holding cost per item per unit time ) }
TBO =sqrt { ( 2* setup cost ) / (demand rate * holding cost per item per unit time ) }
SOLUTION 1:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{ float EOQ,d,s,h,x;
Clrscr();
printf("Enter the value of d,s,h\n");
scanf("%f %f %f",&d,&s,&h);
x=(2*d*s)/h;
EOQ=sqrt(x);
printf("%f",EOQ);
getch();
}
SOLUTION 2:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float x,s,d,h,TOB;
clrscr();
printf("Enter the value of s,d,h\n");
scanf("%f%f%f",&s,&d,&h);
x=(2*s)/(d*h);
TOB=sqrt(x);
printf("%f",TOB);
getch();
}
EXERCISE NO.3.10: For a certain electrical circuit with an inductance L and resistance R,the
damped natural frequency is given by
SOLUTION:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float L,R,C,x,a,b,F;
clrscr();
printf("Enter the value of L,R,C\n");
scanf("%f %f %f",&L,&R,&C);
a={ (1/L*C) (R*R/4*C*C) };
F=sqrt(a);
Printf(%f,F);
getch();
}
EXERCISE NO.3.11: Write program to read a four digit integer and print the sum of its digit.
Hints: Use / and % operators.
SOLUTION:
#include<stdio.h>
#include<conio.h>
void main()
{
int num,a,b,c,d,x,y,result;
clrscr();
printf("Enter a number");
scanf("%d",&num);
a=num%10;
x=num/10;
b=x%10;
y=x/10;
c=y%10;
d=y/10;
result=a+b+c+d;
printf("%d",result);
getch();
}
EXERCISE NO. 3.12: Write a program to print the size of various data types in C.
SOLUTION:
#include<stdio.h>
#include<conio.h>
void main()
{
int m;
clrscr();
m=sizeof(10);
printf("Size=%d",m);
getch();
}
EXERCISE NO.3.13: Given three values, write a program to read three values from keyboard
and print out the largest of them without using if statement.
SOLUTION:
#include<stdio.h>
#include<conio.h>
void main()
{
int x,y,z,a,b;
printf("Enter the value of x,y,z\n");
scanf("%d%d%d",&x,&y,&z);
printf("largest\n");
a=(x>y)?x:y
b=(a>z)?a:z
printf("%d",b);
}
EXERCISE NO.3.14: Write a program to read two integer values m and n and to decide and
print whether m is multiple of n.
SOLUTION:
#include<stdio.h>
#include<conio.h>
void main()
{
int m,n;
printf("Enter m & n,m>=n:");
scanf("%d %d",&m,&n);
if(m%n==0)
printf("m is a multiple of n");
else
printf("m is not a multiple of n");
getch();
}
EXERCISE N0.3.15: Write a program to read three values using scanf statement and print
the following results:
(a)Sum of the values
(b) Average of the three values
(c) Largest of the three
(d) Smallest of the three.
SOLUTION:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,x,y;
float sum, average;
clrscr();
printf("Enter the value of a,b,c\n");
scanf("%d%d%d",&a,&b,&c);
sum=(a+b+c);
printf("sum=%f\n",sum);
{
average=sum/3;
printf("average=%f\n",average);
}
{
printf("Largest\n");
x=(a>b)?a:b;
y=(x>c)?x:c;
printf("%d\n",y);
}
{
printf("Smallest\n");
x=(a<b)?a:b;
y=(x<c)?x:c;
printf("%d\n",y);
}
getch();
EXERCISE NO.3.16: The cost of one type of mobile service is Rs.250 plus Rs.1.25 for each
call made over and above 100 calls. Write a program to read customer codes and calls made
and print the bill for each customer.
SOLUTION:
#include<stdio.h>
#include<conio.h>
void main()
{
int code,call;
float bill;
clrscr();
printf("Enter customer code and number of calls made:");
scanf("%d %d",&code,&call);
bill=250+(call*1.25);
printf("Bill=%f",bill);
getch();
}
EXERCISE NO.3.17: Write a program to print a table of sin and cos functions for the interval
0 180 degrees in increments of 15 as shown below.
-----------------------------------------------------------------------------------------------x(degees)
sin(x)
cos(x)
O
15
..
SOLUTION:
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define p1 3.1416
#define MAX 180
void main()
{
int i;
float x,y,z;
clrscr();
i=0;
printf("x(degree) sin(x) cos(x)\n");
while(i<=MAX)
{
x=(p1/MAX)*i;
y=sin(x);
z=cos(x);
printf("%d\n %f\n %f\n",i,y,z);
i=i+15;
}
getch();
}
EXERCISE NO.3.18: Write a program to compute the values of square-roots and squares of
the number 0 to 100 in steps 10 print the output in a tabular form as shown below.
-----------------------------------------------------------------------------------------------number
Square-root
square
0
100
10
10000
SOLUTION:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
/*......square root and square of numbers 0 to 100.....*/
int i,y;
float x;
clrscr();
printf("Number\tSquare root\tSquare\n\n");
for(i=0;i<=100;i++)
{
x=sqrt(i);
y=i*i;
printf("%d\t%f\t%d\n",i,x,y);
}
getch();
}
EXERCISE NO.3.19: Write a program that determines whether a given integer is odd or even
and displays the number and description on the same line.
SOLUTION:
#include<stdio.h>
#include<conio.h>
void main()
{
int x;
clrscr();
printf("Enter the integer number:");
scanf("%d",&x);
if(x%2==0)
printf("THe number %d is even",x);
else
printf("The number %d is odd",x);
getch();
}
EXERCISE NO.3.20: Write a program to illustrate the use of cast operator in a real life
situation.
SOLUTION:
include<stdio.h>
#include<conio.h>
void main()
{
float sum;
int n;
clrscr();
sum=0;
for(n=1;n<=10;++n)
{
sum=sum+1/(float)n;
printf("%2d %6.4f\n",n,sum);
}
getch();
}
4 comments:
1.
Niteesh BihadeJuly 30, 2012 at 2:30 AM
Thank you for your tutorial and lectures. I am just posting a more simple code to
your
question
3.2
EXERCISE NO.3.2: Write a program that reads a floating-point number and then
displays right-most digit of the integral part of the number.
#include
main()
{
float
int
a;
num;
printf("Enter
scanf("%f",
num
printf("Rightmost
}
real
=
integer\t
number:\t");
&a);
(int)a%10;
num);
%d",
Reply
2.
NiteeshAugust 1, 2012 at 2:31 AM
Code edit for solution 3.10. Please edit the given values appropriately to suit your
solution needs. The code written below is edited and is more in accordance to the
question
asked.
#include
#include
main()
{
float
float
float
l
step
while(c<=limit)
{
=
=
0.01,
frequency
1000.0,
limit
r
0.1,
=
=
c
500.0;
0.01;
0;
frequency
printf("Step
c
}
}
=
-
sqrt(
(1/l*c)
%f\tFrequency
=
%f\n",
((r*r)/(4*c*c))
);
step,
frequency);
c+step;
Reply
3.
NiteeshAugust 1, 2012 at 2:50 AM
3.11
more
elegant
and
easy
to
understand
code
for
beginners...
#include
main()
{
int
printf("Enter
scanf("%d",
a;
four
digit
integer:
\t");
&a);
//Remember always division operator gives quotient wheras the modulo operator
gives
remainder
int
a1
=
a/1000;
//
a
=
2356,
a1
=
2
int
a2
=
a%1000;
//
a2
=
356
int
a3
=
a2/100;
//
a3
=
3
int
a4
=
a2%100;
//a4
=
56
int
a5
=
a4/10;
//a5
=
5;
int
a6
=
a4%10;
//a6
=
6;
printf("\n\n%d",
a1);
printf("\n%d",
a3);
printf("\n%d",
a5);
printf("\n%d",
a6);
printf("\n-----------------------------------------------------------------------------------\
n\n");
int
printf("Sum
}
sum
of
=
Digits
a1+a3+a5+a6;
%d",
sum);
i
f
d
c
of
of
of
of
=
=
=
=
data
data
data
data
type
integer
type
float
type
double
type
char
-
sizeof(int);
sizeof(float);
sizeof(double);
sizeof(char);
%d",
%d",
%d",
%d\n\n\n",
E.Balagurusamy C PROGRAMMING
:CHAPTER-4
4.6 STATES ERRORS,IF ANY,IN THE
FOLLOWING STATEMENTS.
[A] :scanf(%c %f %d,city,&price,&year);
=NO ERROR.
[B] :scanf(%s %d,city,amount);
i);
f);
d);
c);
Solution:
#include<stdio.h>
#include<conio.h>
void main()
{
char s[10],d[11];
clrscr();
printf("Enter the string: ");
scanf("%4s%10s",s,d);
printf("(a)%s %s\n",s,d);
printf("(b)%s\n%s\n",s,d);
printf("(c)%.1s.%.1s",s,d);
getch();
}
Output:
(b)(x+y)/2
Solution:
#include<stdio.h>
#include<conio.h>
void main()
{
float x,y,a,b,c;
clrscr();
printf("Enter the value of x & y: ");
scanf("%f%f",&x,&y);
if(x-y==0)
printf("(a)=imagine");
else
(c)(x+y)*(x-y)
{
a=(x+y)/(x-y);
printf("(a)=%.2f",a);
}
b=(x+y)/2;
c=(x+y)*(x-y);
printf(" (b)=%.2f (c)=%.2f",b,c);
getch();
}
Output:
Enter the value of x & y: 4 3
(a)=7.00
(b)=3.50
(c)=12.00
Enter the value of x & y: 7 7
(a)= imagine
(b)=7.00
(c)=0.00
50.21
-23.73
-46.45
Solution:
#include<stdio.h>
#include<conio.h>
void main()
{
int p,i;
float a;
clrscr();
printf("ENTER REAL NUMBER FOR GET NEAREST INTEGER NUMBER\n");
for(i=1;i<=4;i++)
{
scanf("%f",&a);
if(a>=0)
p=a+0.5;
else
p=a-0.5;
printf("\nNEAREST INTEGER NUMBER OF %f IS= %d\n",a,(int)p);
}
getch();
}
Output:
ENTER REAL NUMBER FOR GET NEAREST INTEGER NUMBER 35.7
NEAREST INTEGER NUMBER OF 35.7 IS= 36
ENTER REAL NUMBER FOR GET NEAREST INTEGER NUMBER 50.21
NEAREST INTEGER NUMBER OF 50.21 IS=50
void main()
{
float a1,a2,a3,a4;
int x,y,z,t,i;
clrscr();
4.36
Output:
Enter four float number: 4.85 4.36 3.12 5.47
The horizontal bar chard is:
* * *
* * 4.85
* * *
* * *
* * *
4.36
3.12
*
5.47
user to enter two two digit integer and print the product
of integers as shown bellow.
45
X
37
7x45 is
3x45is
Add them
315
135
1665
Solution:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,p;
clrscr();
printf("Enter 2 two digits number:");
scanf("%d%d",&a,&b);
printf(" \t%4d\n\tx%3d\n",a,b);
printf("\t------\n");
p=b/10;
c=b%10;
printf("%dx%dis%6d\n",c,a,c*a);
printf("%dx%dis%5d\n",p,a,p*a);
printf("\t-------\n");
printf("Add them %d\n",a*b);
printf("\t-------");
getch();
}
Output:
45
X
7x45 is
3x45is
Add them
37
315
135
1665
Output:
Enter three integer value of x,y,&z: 45 27 89
(a) X=45, Y=27, Z=89
(b) X=45, Y=27, Z=89
(c) X=45, Y=27, Z=89
Output:
1.04e+01
1.0456e+01
1.04567804e+01
Output:
The two decimal place is: 345.67
The five decimal place is: 345.67889
The two decimal place is: 345
GUPTA A. K.
Solution:
#include<stdio.h>
#include<conio.h>
void main()
{
char s[6],d[6],c[6];
clrscr();
printf("Enter the string:");
scanf("%5s%5s%5s",s,d,c);
printf("(a) %s %.1s. %s\n",s,d,c);
printf("(b) %.1s.%.1s.%s\n",s,d,c);
printf("(c) %.1s.%.1s.\n",c,s,d);
getch();
Output:
Enter the string: ANIL KUMAR GUPTA
(a)
ANIL K. GUPTA
( b) A. K. GUPTA
(d) GUPTA A. K.
Code
Price
Fan
67831
1234.50
Motor
450
5786.70
getch();
}
Output:
Enter first name ,code and price :
Fan
67831
1234.50
Motor
450
Name
5786.70
Code
Price
Fan
67831
1234.50
Motor
450
5786.70
Chapter 5
DECISION MAKING AND BRANCHING
REVIEW QUESTION:
RQ-5.1:State whether the following are true or false :
(a)When if statements are nested , the last else gets associated with the
nearest
if without an else.
Ans: False.
(b)One if can have more than one else clause.
Ans: False.
printf(" ");
(b) if (code >1)
a= b+c
else
a=0
Ans: Error.
Correct ans: if (code >1)
a= b+c;
else
a=0;
(c) if(p>0) || (q <0)
printf("Sign is negative);
Ans: Error.
Correct ans:if((p>0) || (q <0))
printf("Sign is negative);
Solution:
(a)The value of x is 2 & y is 0.
(b)The value of x & y is imaginary.
(b) if ( number>100||number<0)
printf(Out of range);
else
sum=sum+number;
Solution:
if ( number>100)
printf(Out of range);
else if(number<0)
printf(Out of range);
else
sum=sum+number;
(c) if (M1>60&&M2>60||T>200)
printf(Admitted\n);
else
printf (Not admitted);
Solution:
if (M1>60)
printf (Admitted\n);
if (M2>60)
printf (Admitted\n);
else if(T>200)
printf (Admitted\n);
else
printf (Not admitted);
Ans: Error.
Correct ans: case 10:
(c)switch(x+y)
Ans:No error.
(d)switch(x) {Case 2: y= x+y; break};
Ans: Error.
Correct ans: switch(x) {Case 2: y= x+y; break;}
(b)!(x==10)||!((y==5)||(z<0))
Ans: (x>0)
(d)!((x<=5)&&(y==10)&&(z<5))
Ans: (x>5)
z=0;
else
y=1;
Output:
0
1
Output:
8
if(m==2)
printf("Chennai");
else
printf("Banglore");
}
else
Printf("END");
getch();
}
Output:
1
Delhi
2
Chennai
3
Banglore
Output:
1 4 3 8
print:
printf("%d %d %d",m,n,p);
getch();
}
Output:
0 0 2
Solution:
The value of x after execution is :-25.
Output:
0
Number is positive
1
Number is negative
RQ-5.18: What will be the output when the following segment is executed?
Program:
char ch = a
switch(ch)
{
case a:
printf(A);
case b:
printf(B);
case c:
printf(C);
}
Output:
a
A
b
B
c
C
Output:
10
Output:
10
CHAPTER 5
EXERCISE-5.2 Write a program to find the number of and sum of all integers greater than
100 and less than 200 that are divisible by 7.
Solution:
/*..number between 100-200 divisible by 7*/
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,r,sum;
sum=0;
clrscr();
for(i=100;i<=200;i++)
{
r=i%7;
if(r==0)
{
printf(" %d",i);
sum=sum+i;
}
printf(Sum=%d,sum);
}
getch();
}
EXERCISE-5.3 A set of two linear equations with two unknowns x1 and x2 is given below:
ax1 +bx2=m and cx1+dx2=n
The set has unique solution
x1= and x2=
provided the determinate ad-cb is not equal to zero.
Write a program that will read the values of constants a,b,c,d,m and n and compute the values
of x1 and x2 .An appropriate message should be printed if ad-cb=0.
Solution:
/*.two linear equation*/
#include<stdio.h>
#include<conio.h>
void main()
{
float a,b,c,d,m,n,x1,x2;
clrscr();
printf("Input a,b,c,d,m,n:\n");
scanf("a=%f b=%f c=%f d=%f m=%f n=%f",&a,&b,&c,&d,&m,&n);
x1=(m*d-b*n)/(a*d-c*b);
x2=(n*a-m*c)/(a*d-c*b);
if((a*d-c*b)!=0)
printf("x1=%f x2= %f",x1,x2);
else
printf("The value is infinity.\n");
getch();
}
EXERCISE-5.4 Given a list of marks ranging from 0 to 100, write a program to print
number of students:
(a)Who have obtained more than 80 marks,
(b) who have obtained more than 60 marks,
(c)Who have obtained more than 40 marks,
(d) who have obtained 40 or less marks,
(e)In the range 81 to 100,
(f) in the range 61 to 80,
(g)in the range 41 to 60,
and (h) in the range 0 to 40.
The program should use a minimum numbers of if statements.
Solution:
/*.marks obtain*/
#include<stdio.h>
#include<conio.h>
void main()
{
int marks,count,a,b,c,d,i;
a=0; b=0; c=0;d=0;
clrscr();
printf("Input 20 boy's marks\n");
for(i=1;i<=20;i++)
{
scanf("%d",&marks);
if(marks>80)
a++;
else if(marks>60)
b++;
else if(marks>40)
c++;
else if(marks<=40)
d++;
}
printf("Number of students who have obtained more than 80 marks=%d\nNumber
of
students who have obtained more than 60 marks=%d\n Number of students
who have obtained more than 40 marks=%d\n Number of students who have obtained 40 or
less marks=%d",a,b,c,d);
getch();
}
getch();
}
Discount
Mill cloth
Handloom items
0-100
5%
101-200
5%
7.5%
201-300
7.5%
10.0%
Above300
10.0%
15.0%
Write a program using switch and if statements to compute the net amount to be paid by a
coustomer.
Solution:
/*.marketing of a showroom*/
#define MC1 0
#define MC2 0.05
#define MC3 0.075
#define MC4 0.10
#define HI1 0.05
#define HI2 0.075
#define HI3 0.10
#define HI4 0.15
#include<stdio.h>
#include<conio.h>
void main()
{
float price,net,discount;
int level,jobnumber;
clrscr();
input:
printf("Enter level jobnumber and purchase amount\n");
printf("Enter zero for level to End\n");
scanf("%d%d%f",&level,&jobnumber,&price);
if(level==0) goto stop;
if(0<=price<=100)
level=1;
else if(101<=price<=200)
level=2;
else if(201<=price<=300)
level=3;
else
level=4;
switch(level)
{
case 1:
discount=MC1+HI1;
break;
case 2:
discount=MC2+HI2;
break;
case 3:
discount=MC3+HI3;
break;
case 4:
discount=MC4+HI4;
break;
default:
printf("Error in level code\n");
goto stop;
}
net=price-(price*discount);
printf("Net amount=%f\n",net);
goto input;
stop:printf("\n\nEND OF THE PROGRAM");
getch();
}
EXERCISE-5.9 Write a program that will read the value of x and evaluate the following
function
y=
using
(a) nested if statements.
(b) else if statements and
(c) conditional operator ?
Solution:
/*evaluate the equation..*/
(a)nested if statements:
#include<stdio.h>
#include<conio.h>
void main()
{
float x,y;
clrscr();
printf("Input x\n");
scanf("%f",&x);
if(x!=0)
{
if(x>0)
printf("y=1");
if(x<0)
printf("y=-1");
}
if(x==0)
printf("y=0");
getch();
}
(b)else if statements:
#include<stdio.h>
#include<conio.h>
void main()
{
float x,y;
clrscr();
printf("Input x\n");
scanf("%f",x);
if(x!=0)
{
if(x>0)
{
printf("1");
}
else
printf("-1");
}
else
printf("0");
getch();
}
(c)conditional operator:
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
float y,x;
printf("Input x\n");
scanf("%f",&x);
y=(x!=0)?((x>0)?1:-1):0;
printf("%d",y);
getch();
}
}
else if(discriminant<0)
printf("Roots are imaginary\n");
else
{
root1=-b+sqrt(discriminant)/2*a;
root2=-b-sqrt(discriminant)/2*a;
printf("Root1=%f Root2=%f",root1,root2);
}
getch();
}
EXERCISE-5.11: Write a program to read three integer values from the keyboard and
displays the output stating that they are the sides of right-angled triangle.
Solution:
/*right-angled triangle..*/
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,x,y,z;
clrscr();
printf("Input three integer values a b and c\n");
scanf("%d%d%d",&a,&b,&c);
x=a*a;
y=b*b;
z=c*c;
if(a>b&&a>c&&(x==y+z))
printf("The values are sides of right-angled triangle");
else if(b>a&&b>c&&(y==x+z))
printf("The values are sides of right-angled triangle");
else if(c>a&&c>b&&z==x+y)
printf("The values are sides of right-angled triangle");
else
printf("The values are not sides of right-angled triangle");
getch();
}
EXERCISE-5.12: An electricity board charges the following rates for the use of electricity:
For the first 200 units: 80 per unit
For the next 100 units: 90per unit
Beyond 300 units: Rs.1.00 per unit
All users are charged a minimum of Rs. 100 as meter charge. If the total amount is more than
Rs.400, then an additional surcharge of 15% of total amount is charged. Write a program to
read the names of users and number of units consumed and print out the charges with names.
Solution:
/*.pay bill..*/
#include<stdio.h>
#include<conio.h>
void main()
{
float units,total,net;
char name;
clrscr();
printf("Input users name and units\n");
scanf("%s %f",&name,&units);
{
if(units<=200)
total=100+0.80*units;
else if(units<=300)
total=100+0.90*units;
else if(units>300)
total=100+1.00*units;
}
if(total>400)
{
net=total+total*0.15;
printf("Total=%f",net); }
else
printf("Total=%f",total);
getch();
}
EXERCISE-5.13: Write a program to compute and display the sum of all integers that are
divisible by 6 but not divisible by 4 and lie between 0 to 100. The program should also count
and display the number of such values.
Solution:
/*numbers between 0-100 divisible by 6 but not divisible by 4.*/
#include<stdio.h>
#include<conio.h>
void main()
{
int i,count;
count=0;
clrscr();
for(i=0;i<=100;i++)
{
if(i%6==0&&i%4!=0)
{
count=count+1;
printf(" %d",i);
}
}
printf(\n);
printf("count=%d",count);
getch();
}
EXERCISE-5.14 Write an interactive program that could read a positive integer number and
decide whether the number is a prime number display the output accordingly. Modify the
program to count all prime numbers that lie 100 to 200. [Note: A prime number is positive
integer that is divisible only by 1 or by itself]
Solution:
/*.prime number */
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,count;
count=0;
clrscr();
printf("\n\nSeries of prime number from 100 to 200:\n");
for(i=100;i<=200;i++)
{
for(j=2;j<=i;j++)
{
if(i%j==0)
break;
}
if(i==j)
{
printf("%4d\n",i);
count+=1;
}
}
printf("The countable number is: %d",count);
getch();
}
s=1;
c=2;
t=3;
printf(Input the value of x and character value\n);
scanf("%d",&x);
r=x*(180/3.1416);
scanf("%d",&d);
{
if(d==1)
result=sin(r);
else if(d==2)
result=cos(r);
else if(d==3)
result==tan(r);
else
printf("no response.");
}
printf("\n%f",result);
getch();
}
Solution-2:
(ii) switch statement:
/*.trigonometric function..*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int i,x;
float v,r;
char t;
clrscr();
printf("Input the value of x\n");
scanf("%d",&x);
r=x*(180/3.1416);
printf("Input charecter");
scanf("%c",&t);
switch(t)
{
case 's':
case 'S':
v=sin(r);
case 'c':
case 'C':
v=cos(r);
case 't':
case 'T':
v=tan(r);
}
printf("%f",v);
getch();
}
Chapter-06
Ans: (n-1)
(b) The
statements is use to skip a part of the statements in a loop.
Ans: continue.
(c) A for loop with the no test condition is known as
loop.
Ans: infinite
(d) The sentinel controlled loop is also; known as
loop.
Ans: indefinite repetition.
(e)In a counter controlled loop, variable known as
is used to count the
loop
operation.
Ans: definite repetition.
6.8 explain the operation of each of the following for loops.
(a)for (n=1;n!=10;n+=2)
sum=sum+n;
Ans :The loop repeats 5 times.
(b)for(n=5;n<=m;n-=1)
sum+=n;
Ans: The continue until n<=m where m initializes from 5 and decrements by 1.
(c) for(n=1;n<=5)
sum+=n;
Ans: Since theren is no increment or decrement condition the loop repeats 5
times.
(d) for(n=1; ;n+=1)
sum+=n;
Ans: The loop repeats infinity times.
(e)for(n=1;n<5;n++)
n=n-1;
Ans: The loop repeats infinity times.
6.9: what would be the output of each of the following code segments?
(a)count=5;
while(count-- >0)
printf(count);
Output:
5 4 3 2 1
(b)count=5;
while(-- count>0)
Printf(count);
Output:
4 3 2 1
(c) count=5;
do printrf(count);
while(count>0)
Output:
5 4 3 2 1
(d)for(m=10;m>7;m-=2)
printf(m);
output;
10 8
6.11:Analyse each of the program segment that follow the determine how many times
the body of each loop will be executed.
(a)x=5;
y=50;
while(x<=y)
{
x=y/x;
..
..
}
Ans: Infinity times
(b) m=1;
do
{
.
m+=2;
}
while(m<10)
Ans: 5 times.
(c) int i;
for(i=0;i<=5;i=i+2/3)
{
..
.
}
Ans: Infinity times.
(d) int m=10;
Int n=7;
while(m%n>=0)
{
m+=1;
n+=2;
.
}
Ans: 4 times.
6.12: Find errors, if any, in each of the following looping segments. Assume that all
the variables have been declared and assigned values.
(a)while(count!=10);
{
count=1;
sum+=x;
count+=1;
}
Error: while(count!=10);
Correct Ans: while(count!=10)
(b) name=0;
do
{
name+=1;
printf(my name is Dinar\n);
while(name=1);
Error: while (name=1);
Correct Ans: while(name==1);
(c) do;
total+=value;
scanf(%f,&value);
while(value!=999);
Error: do;
Correct Ans: do
(E) m=1;
n=0;
for(p=10;p>0;)
p-=1;
printf(%f,p);
Error: for(p=10;p>0;)
p-=1;
printf(%f,p);
Correct ans: for(p=10;p>0;)
{
p-=1;
printf(%f,p);
}
6.13:Write a for statement to pront each of the following sequence of integers:
(a) 1,2,4,8,16,32
Ans: for(i=1;i<=32;i=i*2)
printf(%d,i);
(b) 1,3,9,27,81,243
Ans: for(i=1;i<=243;i=i*i)
printf(%d,i);
(c) -4,-2,0,4
for(i=-4;i<=4;i=i+2)
printf(%d,i);
(d) -10,-12,-14,-18,-26,-42
for(i=-10;i<=-42;i=i-2)
printf(%d,i);
6.14: Change the following for loops to while loops :
(a)for(m=1;m<10;m=m+1)
printf(m);
Ans: m=1;
while(m<10)
{
.
m++;
}
printf(m);
(b)for(;scanf(%d,&m)!=-1;)
printf(m);
Ans:
while(scanf(%d,&m)!=-1)
printf(m);
6.16: What is the output of following code?
Int m=100,n=0;
while(n==0)
{
if(m<10)
break;
m=m-10;
}
Output: No output
6.17: What is output of the following code?
int m=0;
do
{
if(m>10)
continue;
m=m+10;
}
while(m<50);
printf(%d,m);
Output: 50
6.18: What is the output of the following code?
int n=0,m=1;
do
{
printf(m);
m++;
}
while(m<=n);
Output: 1
6.19: What is the output of the following code?
int n=0,m;
for(m=1;m<=n+1;m++)
printrf(m);
Output: 1
6.20: When do we use the following statement?
for(; ;)
Ans : When we need an infinity loop the statement for(; ;) can be used.
CHAPTER-7
4 Votes
7.1 Write a program for fitting a straight line through a set of points (xi,
yi),i=1,2,3.n. The straight line equation is:
Y =mx+c
and the values of m and c are given by:
m=((n (xi,yi))- (xi)(yi) )/( n(xi2)-(xi)2)
c=1/n(yi -m(xi))
All summations are from 1 to n.
Answer:
#include
#include
void main()
{
int i,n=10,v1,v2,x[10],y[10];
int total_x,total_y,total_xy,total_x2;
float m,c,temp,temp1;
clrscr();
printf(Enter the values for x: );
for(i=0;i<10;i++)
{
scanf(%d,&v1);
x[i]=v1;
}
printf(Enter the values for y: );
for(i=0;i<10;i++)
{
scanf(%d,&v2);
y[i]=v2;
}
total_x=total_y=total_xy=total_x2=0;
for(i=0;i<10;i++)
{
total_x=total_x+x[i];
total_y=total_y+y[i];
total_xy=total_xy+(x[i]*y[i]);
total_x2=total_x2+(x[i]*x[i]);
}
temp= total_x*total_y;
temp1=total_x*total_x;
m=((n*total_xy)-(temp))/((n*total_x2)-temp1);
c=((total_y)-(m*total_x))/n;
printf( \nThe equation of the straight line is: );
printf( Y=%fX+%f,m,c);
getch();
}
Output
Enter the values for x:
1 2 3 4 5 6 7 8 9 10
Enter the values for y:
1 2 3 4 5 6 7 8 9 10
The equation of the straight line is:
Y=1.00000X+0.000000
7.2 The daily maximum temperature recorded in 10 cities during the month of
January (for all 31 days) have been tabulated as follows:
City
1 2 3 4 5 6 10
Day
1
2
3
.
.
.
.
31
7.2. Write a program to read the table elements into a two-dimensional array
temperature, and to find the city and day corresponding to
a) the highest temperature
b) the lowest temperature
Answer:
//Write a program to read the table elements into a two-dimensional array
temperature, and to find the city and day corresponding to
//a) the highest temperature
//b) the lowest temperature
// Date : 16/03/2010
#include
#include
void main()
{
int Temp[2][2];
int i,j,City1,City2,MaxTemp,MinTemp;
clrscr();
printf(Enter temperature:\n\n);
for(i=0;i<2;i++) { printf(For City %d ->\n,i);
for(j=0;j<2;j++) { printf(For Day %d ->,j);
scanf(%d,&Temp[i][j]);
}
}
clrscr();
printf(Temperature Matix : \n);
printf( City \n );
for(i=0;i<2;i++)
printf(%d ,i+1);
printf(\n Day\n);
for(i=0;i<2;i++)
{
printf( %d ,i+1);
for(j=0;j<2;j++)
{
printf( %d,Temp[i][j]);
}
printf(\n);
}
MinTemp=MaxTemp=Temp[0][0];
City1=0;
City2=0;
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
if(MaxTemp<Temp[i][j]) { MaxTemp=Temp[i][j]; City1=j+1; }
if(MinTemp>Temp[i][j])
{
MinTemp=Temp[i][j];
City2=j+1;
}
}
}
printf(\n\nHighest Temperature of City %d is %d\n,City1,MaxTemp);
printf(Lowest Temperature of City %d is %d\n,City2,MinTemp);
getch();
}
Program:
/* An election is contested by 5 candidates.
The candidate are numbered are 1 to 5 and the voting is done by marking the
candidate number on the ballot paper. Write a program to read the ballots and count
the votes cast for each candidate using an array variable count. In case, a number,
read is outside the range 1 to 5,the ballot should be considered as a spoilt ballot
and the program should also count the number of spoilt ballots. */
// Date March 16,2010
#include
#include
void main()
{
int i,vote[5],c1=0,c2=0,c3=0,c4=0,c5=0,count=0,count_sp=0,v;
clrscr();
printf(Enter your votes for 5 candidates:);
for(i=1;i<=5;i++)
{
scanf(%d,&v);
vote[i]=v;
}
for(i=1;i<=5;i++)
{
if(vote[i]==1)
c1=c1+1;
else
{
if(vote[i]==2)
c2=c2+1;
else
{
if(vote[i]==3)
c3=c3+1;
else
{
if(vote[i]==4)
c4=c4+1;
else
if(vote[i]==5)
c5=c5+1;
}
}
}
}
2
Votes to Candidate 1: 2
Votes to Candidate 2: 1
Votes to Candidate 3:1
Votes to Candidate 4:0
Votes to Candidate 5:0
The number of valid votes is: 4
The number of spoilt votes is: 1
7.51 The annual examination results of 10 students are tabulated as follows:
Roll No. Subject1 Subject2 Subject3
.
.
.
.
__________________________________________________________________
__
Write a program to read the data and determine the following:
(a) Total marks obtained by each student.
(b) The highest marks in each subject and the Roll No. of the student who
secured it.
(c) The student who obtained the highest total marks.
Program:
max2=sub2[0];
max3=sub3[0];
max=total[0];
roll1=0;
roll2=0;
roll3=0;
roll=0;
for (i=0;i<MAX;i++)
{
if(max1<sub1[i])
{
max1=sub1[i];
roll1=i+1;
}
if(max2<sub2[i])
{
max2=sub2[i];
roll2=i+1;
}
if(max3<sub3[i])
{
max3=sub3[i];
roll3=i+1;
}
if(max<total[i])
{
max=total[i];
roll=i+1;
}
}
printf(\nThe highest marks in subject1 is %d and the roll number is
%d,max1,roll1);
printf(\nThe highest marks in subject2 is %d and the roll number is
%d,max2,roll2);
printf(\nThe highest marks in subject3 is %d and the roll number is
%d,max3,roll3);
printf(\n The highest total marks is %d and the roll number is %d ,max,roll);
getch();
}
7.6 Given are one dimensional arrays A and B which are sorted in ascending
order. Write a program to merge them into a single sorted array C that
contains
every item form array A and B, in ascending order.
Program:
// Given are one dimensional arrays A and B which are sorted in ascending
// order. Write a program to merge them into a single sorted array C that contains
// every item form array A and B, in ascending order.
//Date: 16/03/2010
#include
#include
#define MAX 50
void main()
{
int a[MAX],b[MAX],c[MAX];
int ax,bx,cx,n,m,mn;
clrscr();
ax=bx=cx=0;
printf(Enter no. of elements of array : );
scanf(%d %d,&n,&m);
printf(Enter elements of first array :\n);
for(i=0;i<n;i++)
scanf(%d,&a[i]);
printf(Enter elements of Second array :);
for(i=0;i<m;i++)
scanf(%d,&b[i]);
mn=m+n;
while(ax
{
if(a[ax]<b[bx])
{
c[cx]=a[ax];
ax++;
}
else
{
c[cx]=b[bx];
bx++;
}
cx++;
}
if(ax==n)
{
while(bx<m)
{
c[cx]=b[bx];
bx++;
cx++;
}
}
else
{
while(ax<n)
{
c[cx]=a[ax];
ax++;
cx++;
}
}
//sorted array
printf(the sorted array is : \n);
for(i=0;i<mn;i++)
printf(%d ,c[i]);
getch();
}
7.7 Write a program that will read the values of elements of A and B and
produce the
product matrix C.
Program:
// Write a program that will read the values of elements of A and B and produce the
// product matrix C.
//Date: 16/03/2010
#include
#include
#define MAX 10
void main()
{
int a[MAX][MAX],b[MAX][MAX],c[MAX][MAX];
int i,j,k,row,col;
clrscr();
printf(Enter row of matrix);
scanf(%d,&row);
printf(Enter column of matrix);
scanf(%d,&col);
printf(Enter first matrix\n);
for(i=0;i<row;i++)
for(j=0;j<col;j++)
scanf(%d,&a[i][j]);
printf(\nEnter second matrix \n);
for(i=0;i<row;i++)
for(j=0;j<col;j++)
scanf(%d,&b[i][j]);
printf(\nFirst matrix is : \n);
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
printf(%d ,a[i][j]);
printf(\n);
}
printf(\nSecond matrix is\n);
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
printf(%d ,b[i][j]);
printf(\n);
}
for(i=0;i<row;i++)
for(j=0;j<col;j++)
{ c[i][j]=0;
for(k=0;k<col;k++)
c[i][j]=c[i][j]+(a[i][k]*b[k][j]);
}
printf(\nMultiplication is\n);
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
printf(%d ,c[i][j]);
printf(\n);
}
getch();
}
Program:
j=4;
for(i=0;i<=4;i++)
{
A[i][j]=0;
j;
}
a=4;
for(i=1;i<=4;i++) { for(j=4;j>=a;j)
{
A[i][j]=-1;
}
a;
}
printf(Array is:\n\n);
for(i=0;i<=4;i++)
{
for(j=0;j<=4;j++) printf(%d ,A[i][j]); printf(\n); } getch(); } 7.9 Write a
program to implement selection sort. Algorithm: Step 1: Read Array A. Step 2:
For k=0 to 9 repeat Step 3 to Step 8. Step 3: Compute Small=A[k] & Loc=k. Step
4: For i=0 to 9 repeat Step 5 to Step 7 otherwise go to Step 7. Step 5: Check
Small>A[i] then go to Step 6 otherwise go to Step 4.
Step 6: Compute Small=A[i] & Loc=i.
Step 7: Compute A[Loc]=A[k], A[k]=Small.
Loc=i;
}
A[Loc]=A[k];
A[k]=Small;
}
printf(Sorted Array is:\n\n);
for(i=0;i<=9;i++)
printf(%d ,A[i]);
getch();
}
7.10 Write a program to implement Binary Search algorithm.
Program:
//Write a program to implement Binary Search algorithm.
// Date : 16/03/2010
#include
#include
void main()
{
int Str[10];
int i,Beg,End,Mid,Item;
clrscr();
Beg=0;
End=9;
Mid=(Beg+End)/2;
printf(Enetr Any Sorted Array:\n);
for(i=0;i<10;i++)
scanf(%d,&Str[i]);
printf(Enter Item Which U want to Search:\n);
scanf(%d,&Item);
while((Item!=Str[Mid])&&(Beg<=End))
{
if(Item<Str[Mid]) End=Mid-1; else Beg=Mid+1; Mid=(Beg+End)/2; }
if(Beg>End)
printf(Item Not Found\n);
else
printf(%d Found At Index %d\n,Item,Mid);
getch();
}
7.11 Write a program that will compute the length of a given character string.
Program:
//Write a program that will compute the length of a given character string.
// Date : 16/03/2010
#include
#include
void main()
{
char Str[50];
int i,Len;
clrscr();
Len=0;
printf(Enter a String:\n);
scanf(%[^\n]s,&Str);
for(i=0;Str[i]!=;i++)
Len=Len+1;
printf(Length of String is %d,Len);
getch();
}
7.12 Write a program that will count the number occurrences of a specified
character in a
given line of text.
Program:
//Write a program that will count the number occurrences of a specified character
in a
// given line of text.
// Date : 16/03/2010
#include
#include
void main()
{
char Str[50],CheckChar;
int i,Count,Len;
clrscr();
Count=0;
printf(Enter a String:\n);
scanf(%[^\n]s,&Str);
Len=strlen(Str);
fflush(stdin);
printf(Enter a charatcer:\n);
scanf(%c,&CheckChar);
for(i=0;i<=Len;i++)
if(CheckChar==Str[i])
Count=Count+1;
printf(Number of occurences of %c is %d,CheckChar,Count);
getch();
}
7.13 Write a program to read a matrix of size m*n and print its transpose.
Program:
//Write a program to read a matrix of size m*n and print its transpose.
// Date : 16/03/2010
#include
#include
#define MAX 10
void main()
{
int A[MAX][MAX],C[MAX][MAX];
int Row,Col,i,j;
clrscr();
printf(%d ,C[i][j]);
printf(\n);
}
getch();
}
7.3 An election is contested by 5 candidates. The candidate are numbered are
1 to 5
and the voting is done by marking the candidate number on the ballot
paper. Write
a program to read the ballots and count the votes cast for each candidate
using an
array variable count. In case, a number, read is outside the range 1 to 5,the
ballot
should be considered as a spoilt ballot and the program should also count
the
number of spoilt ballots.
Algorithm:
Flowchart:
Program:
for(i=1;i<=5;i++)
{
if(vote[i]==1)
c1=c1+1;
else
{
if(vote[i]==2)
c2=c2+1;
else
{
if(vote[i]==3)
c3=c3+1;
else
{
if(vote[i]==4)
c4=c4+1;
else
if(vote[i]==5)
c5=c5+1;
}
}
}
}
printf( votes to candidate1=%d,c1);
printf( \nvotes to candidate2=%d,c2);
printf(\n votes to candidate3=%d,c3);
printf( \nvotes to candidate4=%d,c4);
printf( \nvotes to candidate5=%d,c5);
for(i=1;i<=5;i++)
{
if(vote[i]<=5)
count=count+1;
else
count_sp=count_sp+1;
}
printf( The number of valid votes is:%d,count);
printf( \nThe number of spoilt votes is:%d,count_sp);
getch();
}
Output
Enter your votes for 5 candidates:
1
3
1
8
2
Votes to Candidate 1: 2
Votes to Candidate 2: 1
Votes to Candidate 3:1
Votes to Candidate 4:0
Votes to Candidate 5:0
The number of valid votes is: 4
The number of spoilt votes is: 1
Roll No.
Subject1
Subject2
Subject3
.
.
.
.
__________________________________________________________________
__
(b) The highest marks in each subject and the Roll No. of the student who
secured it.
(c)
Algorithm:
Step1. Declare
Step2. For i=0 to i<10, Enter sub1[i]
Step3. For i=0 to i<10, Enter sub2[i]
Step4. For i=0 to i<10, Enter sub3[i]
Program:
Subject1
Subject2
.
.
.
.
Write a program to read the data and determine the following:
(a)
Subject3
(b)
The highest marks in each subject and the Roll No. of the student who
secured it.
(c)
*/
// Date March 16,2010
#include<stdio.h>
#include<conio.h>
#define MAX 10
void main()
{
int i,roll,m1,m2,m3,sub1[MAX],sub2[MAX],sub3[MAX];
int total_sub1,total_sub2,total_sub3,total[MAX];
int max,max1,max2,max3,roll1,roll2,roll3;
clrscr();
printf(Enter the marks for subject1 of all the students: );
for(i=0;i<MAX;i++)
scanf(%d,&sub1[i]);
printf(Enter the marks for subject2 of all the students: );
for(i=0;i<MAX;i++)
scanf(%d,&sub2[i]);
printf(Enter the marks for subject3 of all the students: );
for(i=0;i<MAX;i++)
scanf(%d,&sub3[i]);
total_sub1=total_sub2=total_sub3=0;
for(i=0;i<MAX;i++)
{
total_sub1=total_sub1+sub1[i];
total_sub2=total_sub2+sub2[i];
total_sub3=total_sub3+sub3[i];
total[i]=sub1[i]+sub2[i]+sub3[i];
}
for(i=0;i<MAX;i++)
{
printf(The total marks obtained by the student%d is =%d\n,i+1,total[i]);
}
max1=sub1[0];
max2=sub2[0];
max3=sub3[0];
max=total[0];
roll1=0;
roll2=0;
roll3=0;
roll=0;
for (i=0;i<MAX;i++)
{
if(max1<sub1[i])
{
max1=sub1[i];
roll1=i+1;
}
if(max2<sub2[i])
{
max2=sub2[i];
roll2=i+1;
}
if(max3<sub3[i])
{
max3=sub3[i];
roll3=i+1;
}
if(max<total[i])
{
max=total[i];
roll=i+1;
}
}
7.6 Given are one dimensional arrays A and B which are sorted in ascending
order. Write a program to merge them into a single sorted array C that
contains
every item form array A and B, in ascending order.
Algorithm:
Program:
// Given are one dimensional arrays A and B which are sorted in ascending
//
order. Write a program to merge them into a single sorted array C that
contains
//
//Date: 16/03/2010
#include<stdio.h>
#include<conio.h>
#define MAX 50
void main()
{
int a[MAX],b[MAX],c[MAX];
int ax,bx,cx,n,m,mn;
clrscr();
ax=bx=cx=0;
printf(Enter no. of elements of array : );
scanf(%d %d,&n,&m);
printf(Enter elements of first array :\n);
for(i=0;i<n;i++)
scanf(%d,&a[i]);
printf(Enter elements of Second array :);
for(i=0;i<m;i++)
scanf(%d,&b[i]);
mn=m+n;
while(ax<n && bx<m)
{
if(a[ax]<b[bx])
{
c[cx]=a[ax];
ax++;
}
else
{
c[cx]=b[bx];
bx++;
}
cx++;
}
if(ax==n)
{
while(bx<m)
{
c[cx]=b[bx];
bx++;
cx++;
}
}
else
{
while(ax<n)
{
c[cx]=a[ax];
ax++;
cx++;
}
}
//sorted array
printf(the sorted array is : \n);
for(i=0;i<mn;i++)
printf(%d ,c[i]);
getch();
}
7.7 Write a program that will read the values of elements of A and B and
produce the
product matrix C.
Algorithm:
Flowchart:
Program:
// Write a program that will read the values of elements of A and B and produce the
// product matrix C.
//Date: 16/03/2010
#include<stdio.h>
#include<conio.h>
#define MAX 10
void main()
{
int a[MAX][MAX],b[MAX][MAX],c[MAX][MAX];
int i,j,k,row,col;
clrscr();
printf(Enter row of matrix);
scanf(%d,&row);
printf(Enter column of matrix);
scanf(%d,&col);
printf(Enter first matrix\n);
for(i=0;i<row;i++)
for(j=0;j<col;j++)
scanf(%d,&a[i][j]);
printf(\nEnter second matrix \n);
for(i=0;i<row;i++)
for(j=0;j<col;j++)
scanf(%d,&b[i][j]);
printf(\nFirst matrix is : \n);
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
printf(%d ,a[i][j]);
printf(\n);
}
printf(\nSecond matrix is\n);
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
printf(%d ,b[i][j]);
printf(\n);
}
for(i=0;i<row;i++)
for(j=0;j<col;j++)
{ c[i][j]=0;
for(k=0;k<col;k++)
c[i][j]=c[i][j]+(a[i][k]*b[k][j]);
}
printf(\nMultiplication is\n);
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
printf(%d ,c[i][j]);
printf(\n);
}
getch();
}
Algorithm:
Flowchart:
Program:
//
//
//Display the contents of the matrix using not more than two printf statements.
// Date : 16/03/2010
#include<stdio.h>
#include<conio.h>
void main()
{
int A[5][5];
int a,i,k,j;
clrscr();
a=3;
for(i=0;i<=3;i++)
{
for(j=0;j<=a;j++)
{
A[i][j]=+1;
}
a;
}
j=4;
for(i=0;i<=4;i++)
{
A[i][j]=0;
j;
}
a=4;
for(i=1;i<=4;i++)
{
for(j=4;j>=a;j)
{
A[i][j]=-1;
}
a;
}
printf(Array is:\n\n);
for(i=0;i<=4;i++)
{
for(j=0;j<=4;j++)
printf(%d ,A[i][j]);
printf(\n);
}
getch();
}
7.9 Write a program to implement selection sort.
Algorithm:
Flowchart:
Program:
clrscr();
printf(Enter Elements of Array:\n);
for(i=0;i<=9;i++)
scanf(%d,&A[i]);
for(k=0;k<=9;k++)
{
Small=A[k];
Loc=k;
for(i=k;i<=9;i++)
if(Small>A[i])
{
Small=A[i];
Loc=i;
}
A[Loc]=A[k];
A[k]=Small;
}
printf(Sorted Array is:\n\n);
for(i=0;i<=9;i++)
printf(%d ,A[i]);
getch();
}
Algorithm:
Flowchart:
Program:
scanf(%d,&Str[i]);
printf(Enter Item Which U want to Search:\n);
scanf(%d,&Item);
while((Item!=Str[Mid])&&(Beg<=End))
{
if(Item<Str[Mid])
End=Mid-1;
else
Beg=Mid+1;
Mid=(Beg+End)/2;
}
if(Beg>End)
printf(Item Not Found\n);
else
printf(%d Found At Index %d\n,Item,Mid);
getch();
}
7.11 Write a program that will compute the length of a given character string.
Algorithm:
Program:
//Write a program that will compute the length of a given character string.
// Date : 16/03/2010
#include<stdio.h>
#include<conio.h>
void main()
{
char Str[50];
int i,Len;
clrscr();
Len=0;
printf(Enter a String:\n);
scanf(%[^\n]s,&Str);
for(i=0;Str[i]!=;i++)
Len=Len+1;
printf(Length of String is %d,Len);
getch();
}
7.12 Write a program that will count the number occurrences of a specified
character in a
given line of text.
Algorithm:
Flowchart:
Program:
//Write a program that will count the number occurrences of a specified character
in a
//
// Date : 16/03/2010
#include<stdio.h>
#include<conio.h>
void main()
{
char Str[50],CheckChar;
int i,Count,Len;
clrscr();
Count=0;
printf(Enter a String:\n);
scanf(%[^\n]s,&Str);
Len=strlen(Str);
fflush(stdin);
printf(Enter a charatcer:\n);
scanf(%c,&CheckChar);
for(i=0;i<=Len;i++)
if(CheckChar==Str[i])
Count=Count+1;
printf(Number of occurences of %c is %d,CheckChar,Count);
getch();
}
7.13 Write a program to read a matrix of size m*n and print its transpose.
Algorithm:
Step 2: Display A
Step 3: For i=0 to Col repeat Step 4 to Step 5 otherwise go to Step 6
Step 4: For j=0 to Row repeat Step 5 otherwise go to Step 3
Step 5: Compute C[i][j]=A[j][i]
Step 6: Display C
Flowchart:
Program:
//Write a program to read a matrix of size m*n and print its transpose.
// Date : 16/03/2010
#include<stdio.h>
#include<conio.h>
#define MAX 10
void main()
{
int A[MAX][MAX],C[MAX][MAX];
int Row,Col,i,j;
clrscr();
printf(Enter Number of Rows:\n);
scanf(%d,&Row);
printf(Enter Number of Column:\n);
scanf(%d,&Col);
printf(Enter Matrix:\n);
for(i=0;i<Row;i++)
for(j=0;j<Col;j++)
scanf(%d,&A[i][j]);
clrscr();
printf(Matrix:\n);
for(i=0;i<Row;i++)
{
for(j=0;j<Col;j++)
printf(%d ,A[i][j]);
printf(\n);
}
for(i=0;i<Col;i++)
for(j=0;j<Row;j++)
C[i][j]=A[j][i];
printf(Transpose of Matrix:\n);
for(i=0;i<Col;i++)
{
for(j=0;j<Row;j++)
printf(%d ,C[i][j]);
printf(\n);
}
getch();
}
7.14 Every book published by international publishers should carry an
International
Standard Book Number (ISBN). It is a 10 character 4 part number as
shown below.
0-07-041183-2
The first part denotes the region, the second represents publisher, the third
identifies the book and the fourth is the check digit. The check digit is
computed as follows:
Check digit is the remainder when Sum is divided by 11. Write a program that
reads a given ISBN number and check whether it represents a valid ISBN.
Algorithm:
Step 1: Read Array ISBN.
Step 2: Compute Sum=Sum+(i*ISBN[i]) for i=0 to 9
Step 3: Compute CheckDig=Sum%11
Step 4: Check CheckDig=ISBN[10] then go to Step 5 Otherwise go to Step 6
Step 5: Display Valid ISBN
Step 6: Display Invalid ISBN
Flowchart:
Program:
//Write a program that reads a given ISBN number and check whether it represents
a valid ISBN.
// Date : 16/03/2010
#include<stdio.h>
#include<conio.h>
#define MAX 10
void main()
{
int ISBN[11];
int i,j,Sum,CheckDig;
clrscr();
Sum=0;
printf(Enter ISBN Number:\n);
for(i=1;i<=10;i++)
scanf(%d,&ISBN[i]);
for(i=1;i<=9;i++)
Sum=Sum+(i*ISBN[i]);
CheckDig=Sum%11;
if(CheckDig==ISBN[10])
printf(\nValid ISBN\n);
else
printf(\nInvalid ISBN\n);
getch();
}
7.15 Write a program to read two matrices A and B and print the following:
a)
A + B and
b)
A B.
Algorithm:
Flowchart:
Program:
//Write a program to read two matrices A and B and print the following:
//a)
A + B and
//b)
A B.
// Date : 16/03/2010
#include<stdio.h>
#include<conio.h>
#define MAX 10
void main()
{
int A[MAX][MAX],B[MAX][MAX],C[MAX][MAX];
int Row,Col,i,j;
clrscr();
printf(Enter Number of Rows:\n);
scanf(%d,&Row);
printf(Enter Number of Column:\n);
scanf(%d,&Col);
printf(Enter First Matrix:\n);
for(i=0;i<Row;i++)
for(j=0;j<Col;j++)
scanf(%d,&A[i][j]);
printf(Enter Second Matrix:\n);
for(i=0;i<Row;i++)
for(j=0;j<Col;j++)
scanf(%d,&B[i][j]);
clrscr();
printf(First Matrix:\n);
for(i=0;i<Row;i++)
{
for(j=0;j<Col;j++)
printf(%d ,A[i][j]);
printf(\n);
}
printf(Second Matrix:\n);
for(i=0;i<Row;i++)
{
for(j=0;j<Col;j++)
printf(%d ,B[i][j]);
printf(\n);
}
for(i=0;i<Row;i++)
for(j=0;j<Col;j++)
C[i][j]=A[i][j]+B[i][j];
printf(Addition of Matrix:\n);
for(i=0;i<Row;i++)
{
for(j=0;j<Col;j++)
printf(%d ,C[i][j]);
printf(\n);
}
for(i=0;i<Row;i++)
for(j=0;j<Col;j++)
C[i][j]=A[i][j]-B[i][j];
printf(Subtration of Matrix:\n);
for(i=0;i<Row;i++)
{
for(j=0;j<Col;j++)
printf(%d ,C[i][j]);
printf(\n);
}
getch();
}
Algorithm:
Step 1: Read Array ISBN.
Step 2: Compute Sum=Sum+(i*ISBN[i]) for i=0 to 9
Step 3: Compute CheckDig=Sum%11
Step 4: Check CheckDig=ISBN[10] then go to Step 5 Otherwise go to Step 6
Step 5: Display Valid ISBN
Step 6: Display Invalid ISBN
Flowchart:
Program:
//Write a program that reads a given ISBN number and check whether it represents
a valid ISBN.
// Date : 16/03/2010
#include
#include
#define MAX 10
void main()
{
int ISBN[11];
int i,j,Sum,CheckDig;
clrscr();
Sum=0;
printf(Enter ISBN Number:\n);
for(i=1;i<=10;i++)
scanf(%d,&ISBN[i]);
for(i=1;i<=9;i++)
Sum=Sum+(i*ISBN[i]);
CheckDig=Sum%11;
if(CheckDig==ISBN[10])
printf(\nValid ISBN\n);
else
printf(\nInvalid ISBN\n);
getch();
}
7.15 Write a program to read two matrices A and B and print the following:
a) A + B and
b) A B.
Program:
//Write a program to read two matrices A and B and print the following:
//a) A + B and
//b) A B.
// Date : 16/03/2010
#include
#include
#define MAX 10
void main()
{
int A[MAX][MAX],B[MAX][MAX],C[MAX][MAX];
int Row,Col,i,j;
clrscr();
printf(Enter Number of Rows:\n);
scanf(%d,&Row);
printf(Enter Number of Column:\n);
scanf(%d,&Col);
for(i=0;i<Row;i++)
for(j=0;j<Col;j++)
C[i][j]=A[i][j]+B[i][j];
printf(Addition of Matrix:\n);
for(i=0;i<Row;i++)
{
for(j=0;j<Col;j++)
printf(%d ,C[i][j]);
printf(\n);
}
for(i=0;i<Row;i++)
for(j=0;j<Col;j++)
C[i][j]=A[i][j]-B[i][j];
printf(Subtration of Matrix:\n);
for(i=0;i<Row;i++)
{
for(j=0;j<Col;j++)
printf(%d ,C[i][j]);
printf(\n);
}
getch();
}
CHAPTER-8
8.3 Write a program to extract a portion of a character string and print the
extracted string. Assume that m characters are extracted, starting with the
nth character.
Answer:
clrscr();
printf("Enter A String:--\n");
scanf("%[^\n]s",Str1);
printf("\nEnter Number of Characters Which U Wnat to
Extract-->\n");
scanf("%d",&m);
printf("\nEnter Beginnig Index from Which U Want to
Extract-->\n");
scanf("%d",&n);
printf("\nExtracted String is:--\n\n");
for(i=n-1;i
getch();
}
8.7 A Maruti car dealer maintains a reecord of sales of various vehicles in the
following
form:
Vehicle Type
Month of sales
Price
MARUTI-800
02/01
210000
MARUTI-DX
07/01
265000
GYPSY
04/02
MARUTI-VAN
08/02
315750
240000
Write a program to read this data into a table of strings and output the
details of a
particular vehicle sold during a specified period. The program should
request the user to
input the vehicle type and the period (starting month, ending month).
Answer:
#include<stdio.h>
#include<conio.h>
#define MAX 10
void main()
{
char Veh[MAX][MAX]={};
char Vehicle[MAX];
int St_Mon[MAX],En_Mon[MAX],StMon,EnMon;
Month of Sales
Price\n);
for(i=0;i<n;i++)
{
printf(%s
0%d / 0%d
%ld\n,Veh[i],St_Mon[i],En_Mon[i],Price[i]);
}
printf(Enter The Type of Vehicle\n);
scanf(%s,Vehicle);
8.9 Write a program that reads the cost of an item in the form RRRR.PP
(where RRRR denotes Rupees and PP denotes Paise) and converts the value to
a string of words that express the numeric value in words. For example, if we
input 125.75 the ouput should beONE HUNDRED TWENTY FIVE AND
PAISE SEVENTY FIVE.
Answer:
#include<stdio.h>
#include<conio.h>
void main()
{
float Cost,Pai,Re,j;
int Rup,i,R;
clrscr();
printf("\nEnter Cost of an ITEM-->\n");
scanf("%f",&Cost);
Rup = Cost;
Pai = (Cost - Rup)*100;
i=Rup/100;
switch(i)
{
case 1: printf("ONE HUNDRED "); break;
case 2: printf("TWO HUNDRED "); break;
case 3: printf("THREE HUNDRED "); break;
case 4: printf("FOUR HUNDRED "); break;
case 5: printf("FIVE HUNDRED "); break;
case 6: printf("SIX HUNDRED "); break;
case 7: printf("SEVEN HUNDRED "); break;
case 8: printf("EIGHT HUNDRED "); break;
case 9: printf("NINE HUNDRED "); break;
}
i=Rup%100;
R=i/10;
Re=(float)i/10;
switch(R)
{
case 1: printf("TEN"); break;
case 2: printf("TWENTY "); break;
}
switch(R)
{
case 1: printf("ONE"); break;
case 2: printf("TWO "); break;
case 3: printf("THREE "); break;
case 4: printf("FOUR "); break;
case 5: printf("FIVE "); break;
case 6: printf("SIX "); break;
case 7: printf("SEVEN "); break;
case 8: printf("EIGHT "); break;
case 9: printf("NINE "); break;
}
getch();
}
8.10 Develop a program that will read and store the details of a list of students
in the format
Roll No.
Name
Marks Obtained
..
..
b)
c)
Answer:
#include<stdio.h>
#include<conio.h>
#define MAX 50
void main()
{
char Stu_Name[MAX][MAX]={};
//char Stu_Name1[MAX][MAX]={};
char Temp[MAX]=";
int roll_No[MAX],Marks[MAX],n,i,In[MAX],Roll_No1[MAX],Marks1[MAX];
int Temp1,Temp2;
int j;
clrscr();
printf(How Many Student Name U Want to Enter\n\n);
scanf(%d,&n);
printf(Enter Roll No. & Students Name & Total Marks:\n);
for(i=0;i<n;i++)
{
scanf(%d,&Roll_No[i]);
scanf(%s,Stu_Name[i]);
scanf(%d,&Marks[i]);
}
clrscr();
printf(Roll No
Name
Marks\n);
for(i=0;i<n;i++)
{
printf(%d%s%d\n,Roll_No[i],Stu_Name[i],Marks[i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if(strcmp(Stu_Name[j],Stu_Name[j+1])>0)
{
strcpy(Temp,Stu_Name[j]);
strcpy(Stu_Name[j],Stu_Name[j+1]);
strcpy(Stu_Name[j+1],Temp);
Temp1=Roll_No[j];
Roll_No[j]=Roll_No[j+1];
Roll_No[j+1]=Temp1;
Temp2=Marks[j];
Marks[j]=Marks[j+1];
Marks[j+1]=Temp2;
}
}
}
printf(\nAccording to Student Names:\n);
printf(Roll No
for(i=0;i<n;i++)
{
Name
Marks\n);
printf(%d
%s
%d\n,Roll_No[i],Stu_Name[i],Marks[i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if(Roll_No[j]>Roll_No[j+1])
{
strcpy(Temp,Stu_Name[j]);
strcpy(Stu_Name[j],Stu_Name[j+1]);
strcpy(Stu_Name[j+1],Temp);
Temp1=Roll_No[j];
Roll_No[j]=Roll_No[j+1];
Roll_No[j+1]=Temp1;
Temp2=Marks[j];
Marks[j]=Marks[j+1];
Marks[j+1]=Temp2;
}
}
}
printf(\nAccording to Marks:\n);
printf(Roll No
Name
Marks\n);
for(i=0;i<n;i++)
{
printf(%d
%s
%d\n,Roll_No[i],Stu_Name[i],Marks[i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if(Marks[j]<Marks[j+1])
{
strcpy(Temp,Stu_Name[j]);
strcpy(Stu_Name[j],Stu_Name[j+1]);
strcpy(Stu_Name[j+1],Temp);
Temp1=Roll_No[j];
Roll_No[j]=Roll_No[j+1];
Roll_No[j+1]=Temp1;
Temp2=Marks[j];
Marks[j]=Marks[j+1];
Marks[j+1]=Temp2;
}
}
}
Name
Marks\n);
for(i=0;i<n;i++)
{
printf(%d
%s
%d\n,Roll_No[i],Stu_Name[i],Marks[i]);
}
getch();
}
8.11 Write a program to read to strings and compare them using the function
strcmp() and print a mesaage that the first string is equal, less or greater than
the second one.
Answer:
#include<stdio.h>
#include<conio.h>
#define MAX 50
void main()
{
char Str1[MAX],Str2[MAX];
clrscr();
fflush(stdin);
if(strcmp(Str1,Str2)==0)
printf(\nBoth Strings are Equal\n);
else if(strcmp(Str1,Str2)<0)
printf(\nFirst String is Less Than\n);
else
printf(\nFirst String is Greater Than\n);
getch();
}
8.12 Write a program to read a line of text from the keyboard and print out
the number of occurrences of a given substring using the function strstr().
Answer:
#include<stdio.h>
#include<conio.h>
#define MAX 50
void main()
{
char *Str1,*Str2,*Str3;
int i,Len,Len1,Count;
clrscr();
Count=0;
printf(Enter Text:\n);
scanf(%[^\n]s,Str1);
fflush(stdin);
printf(Enter Substring:\n);
scanf(%[^\n]s,Str2);
Len=strlen(Str1);
Len1=strlen(Str2);
strcpy(Str3,Str1);
for(i=0;i<Len;i++)
{
if((Str3=strstr(Str3,Str2))!=NULL)
{
i=i+Len1;
Count=Count+1;
}
strcpy(Str3,(Str3+Len1));
}
8.13 Write a program that will copy m consecutive characters from a string s1
beginning at position n into another string s2.
Answer:
#include<stdio.h>
#include<conio.h>
#define MAX 50
void main()
{
char Str1[MAX],Str2[MAX];
int i,m,n,j;
clrscr();
printf(Enter A String:\n);
scanf(%[^\n]s,Str1);
for(i=n-1,j=0;i<m+n;i++,j++)
{
Str2[j]=Str1[i];
}
Str2[m]=;
Answer:
#include<stdio.h>
#include<conio.h>
#define MAX 50
void main()
{
char Stu_Name[MAX][MAX],Name[MAX];
int Roll_No[MAX],n,i,Roll,Index;
clrscr();
for(i=0;i<n;i++)
{
scanf(%d,&Roll_No[i]);
scanf(%s,Stu_Name[i]);
}
for(i=0;i<n;i++)
{
if(Roll==Roll_No[i])
{
Index=i;
}
}
for(i=0;i<n;i++)
{
if(strcmp(Stu_Name[i],Name)==0)
{
Index=i;
}
}
8.15 Given a string char str[ ] =123456789; Write a program that displays
the following:
1
232
34543
4567654
567898765
answer:
#include<stdio.h>
#include<conio.h>
void main()
{
char Str[]=123456789;
int i,j,k,l;
clrscr();
for(i=0;i<5;i++)
{
for(k=4;k>i;k)
{
printf( );
}
for(j=0,l=i;j<=i;j++,l++)
{
printf(%c,Str[l]);
}
l=l-2;
for(k=0;k<i;k++,l)
printf(%c,Str[l]);
printf(\n);
}
getch();