My Basket
You have nothing in your shopping cart yet.
Title: C Programing
Description: Some Basic Programs Og C Programing
Document Preview
Extracts from the notes are below, to see the PDF you'll receive please use the links above
C Programs with Solutions
C
Programs with Solutions
By
S
...
E
...
D)
...
, MACEEE
...
Ltd
...
Ltd
...
laxmipublications
...
com
Copyright © 2011 by Laxmi Publications Pvt
...
All rights reserved
...
Price: < 180
...
First Edition: 2011
OFFICES
✆
✆
✆
✆
✆
✆
Bangalore
Cochin
Hyderabad
Kolkata
Mumbai
080-26 75 69 30
0484-237 70 04, 405 13 03
040-24 65 23 33
033-22 27 43 84
022-24 91 54 15, 24 92 78 69
Ranchi
✆
✆
✆
✆
✆
0651-221 47 64
UCP–9612–180–C PROGRAM WITH SOLUTION–ANA
Typeset at : Monu Printographics, Delhi
Chennai
Guwahati
Jalandhar
Lucknow
Patna
044-24 34 47 26
0361-251 36 69, 251 38 81
0181-222 12 72
0522-220 99 16
0612-230 00 97
C—
Printed at : Ajit Printers, Delhi
Dedicated to
My Son
Master A
...
C Concepts
2
...
Fundamentals—C Programs
4
...
Sample Questions
6
...
Questions
Pages
1–11
12–51
52–112
113–248
249–261
262–279
280–286
Preface
This book gives a rich collection of C programs
...
This
book will be useful for students of BE, MCA, BCA, MSc, and BSc, which have C programming language as a part of the course
...
The second chapter
focuses on introduction C programming
...
Fourth chapter focuses on C debugging
...
Sixth chapter deals with the short questions
and answers
...
Suggestions for improvement will be appreciated and incorporated
...
0 OVERVIEW OF C PROGRAMMING
C language is one of the most popular computer languages today because it is a structured,
high level, machine independent language
...
C is called a high level,
compiler language
...
C is one of a large number of high level languages which can be used for general purpose
programming, i
...
, anything from writing small programs for personal amusement to writing complex
applications
...
Before C, high level languages were criticized by machine
code programmers because they shielded the user from the working details of the computer
...
This is vitally important for writing lengthy programs because complex problems are
only manageable with a clear organization and program structure
...
These provide an excellent basis for controlling
the flow of programs
...
The richness of
a language shapes what it can talk about
...
C tries to make the best of a computer by linking as closely as possible to the local environment
...
It is a robust
language whose rich set of built-in functions and operators can be used to write any complex program
...
Programs written in C are efficient and fast
...
C is highly portable
...
Another feature of C is its ability to extend itself
...
1 INTRODUCTION
C is a remarkable language
...
C is a structured language
...
It is easy for debugging, testing, and maintenance if a language is a structured
one
...
2 STRUCTURE OF A C PROGRAM
Include header file section
Global declaration section
main()
{
Declaration part
Executable part
}
User-defined functions
{
Statements
}
Include Header File Section
C program depends upon some header files for function definition that are used in program
...
h
...
Global Declaration
This section declares some variables that are used in more than one function
...
This section must be declared outside of all the functions
...
The function main() is
a starting point of every C program
...
Declaration Part
The declaration part declares the entire variables that are used in executable part
...
Initialisation means providing initial value
to the variables
...
This part conatins
a set of statements or a single statement
...
User Defined Function
The functions defined by the user are called user-defined functions
...
1
...
Creation of program
Programs should be written in C editor
...
The default extension is C
...
Compilation of a program
The source program statements should be translated into object programs which is suitable
for execution by the computer
...
If
there is no error, compilation proceeds and translated program are stored in another file
with the same file name with extension “
...
3
...
1
...
dot
^
Caret
;
Semicolon
*
Asterisk
:
Colon
-
Minus
'
Apostrophe
+
Plus
4
C PROGRAMS
WITH
SOLUTIONS
"
Quotation mark
<
Less than
!
Exclamation mark
>
Greater than
|
Vertical bar
()
Parenthesis left/right
/
Slash
[]
Bracket left/right
\
Back slash
{}
Braces left/right
~
Tilde
%
Percent
_
Underscore
#
Number sign or Hash
$
Dollar
=
Equal to
?
Question mark
@
At the rate
1
...
6 C KEYWORDS
Auto
Double
Int
Struct
Break
Else
Long
Switch
Case
Enum
Register
Typedef
Char
Extern
Return
Union
Const
Float
Short
Unsigned
Continue
For
Signed
Void
Default
Goto
Sizeof
Volatile
Do
If
Static
while
C CONCEPTS
5
1
...
They are user-defined names,
consisting sequence of letters and digits, with the letter as the first character
...
8 CONSTANTS
Values do not change during the execution of the program
Types:
1
...
It requires minimum two bytes and maximum four bytes
...
Eg: 2
...
342
2
...
Characters are also represented with a
single digit or a single special symbol or white space enclosed within a pair of single
quote marks
Eg: ‘a’, ‘8’, “ ”
...
Eg: “Hello”, “india”, “444”
1
...
Its value may be changed during the program
execution
...
1
...
4e – 38 to +3
...
7e – 308 to 1
...
4e – 4932 to 1
...
11 OPERATORS
It indicates an operation to be performed on data that yields value
...
12 INPUT AND OUTPUT
Reading data from input devices and displaying the results on the screen are the two main
tasks of any program
...
13
DECISION STATEMENTS
It checks the given condition and then executes its sub-block
...
Types:
1
...
If-else statement
3
...
Break statement
5
...
Goto statement
7
...
Nested switch ()case
9
...
14 LOOP CONTROL STATEMENTS
Loop is a block of statements which are repeatedly executed for certain number of times
...
For loop
2
...
While loop
4
...
do-while statement with while loop
C CONCEPTS
Statement
9
Syntax
For loop
For(initialize counter; test condition; re-evaluation parameter)
{
Statement;
Statement;
}
Nested for loop
for(initialize counter; test condition; re-evaluation parameter)
{
Statement;
Statement;
for(initialize counter; test condition; re-evaluation parameter)
Statement;
Statement;
}
}
While loop
While (test condition)
{
Body of the loop
}
Do while loop
do
{
Statement;
}
While(condition);
Do-while with while loop
Do while(condition)
{
Statement;
}
While (condition);
1
...
Types
1
...
Two dimensional arrays
3
...
Insertion
2
...
Searching
4
...
Merging
1
...
Group of characters, digits, symbols enclosed within
quotation marks are called as strings
...
17
11
FUNCTIONS
It is a self-contained block or a sub program of one or more statements that performs a
special task
...
Any change made in the formal argument does not effect the actual
arguments because formal arguments are photo copies of actual arguments
...
Function operates on address
rather than values
...
1
...
Chapter
2
INTRODUCTIONC PROGRAMS
1] Program to find sum of two numbers
...
h>
#include
...
#include
...
h>
void main()
{
int r;
float pi=3
...
000
circumference=31
...
#include
...
h>
void main()
{
int p,r,t,si;
clrscr();
printf(“enter principle, Rate of interest & time to find simple interest: ”);
scanf(“%d%d%d”,&p,&r,&t);
si=(p*r*t)/100;
printf(“simple intrest= %d”,si);
getch();
}
Output:
enter principle, rate of interest & time to find simple interest: 500
5
2
simple interest=50
4] Program to convert temperature from degree centigrade to Fahrenheit
...
h>
#include
...
8*c)+32;
printf(“temp in Fahrenheit=%f ”,f);
getch();
}
Output:
enter temp in centigrade: 32
temp in Fahrenheit=89
...
#include
...
h>
void main()
{
int s1,s2,s3,s4,s5,sum,total=500;
float per;
clrscr();
printf(“enter marks of 5 subjects: ”);
scanf(“%d%d%d%d%d”,&s1,&s2,&s3,&s4,&s5);
sum=s1+s2+s3+s4+s5;
printf(“sum=%d”,sum);
per=(sum*100)/total;
printf(“percentage=%f”,per);
getch();
}
Output:
enter marks of 5 subjects: 60
65
50
60
60
sum=300
percentage=60
...
#include
...
h>
I NTRODUCTIONC P ROGRAMS
void main()
{
int a,b;
clrscr();
printf(“enter value for a & b: ”);
scanf(“%d%d”,&a,&b);
a=a+b;
b=a-b;
a=a-b;
printf(“after swapping the value of a & b: %d %d”,a,b);
getch();
}
Output:
enter value for a & b: 4 5
after swapping the value of a & b: 5 4
7] Program to reverse a given number
...
h>
#include
...
#include
...
h>
void main()
{
int gs,bs,da,ta;
clrscr();
printf(“enter basic salary: ”);
scanf(“%d”,&bs);
da=(10*bs)/100;
ta=(12*bs)/100;
gs=bs+da+ta;
printf(“gross salary=%d”,gs);
getch();
}
Output:
enter basic salary: 100
gross salary=122
9] Program to print a table of any number
...
h>
#include
...
#include
...
h>
void main()
{
int a,b,c;
clrscr();
printf(“enter value of a, b & c: ”);
scanf(“%d%d%d”,&a,&b,&c);
if((a>b)&&(a>c))
printf(“a is greatest”);
if((b>c)&&(b>a))
printf(“b is greatest”);
if((c>a)&&(c>b))
printf(“c is greatest”);
getch();
}
Output:
enter value for a, b& c: 5
7
4
b is greatest
11] Program to show the use of conditional operator
...
h>
#include
...
#include
...
h>
void main()
{
int n;
clrscr();
printf(“enter any year: ”);
scanf(“%d”,&n);
if(n%4==0)
printf(“year is a leap year”);
else
printf(“year is not a leap year”);
getch();
}
Output:
enter any year: 1947
year is not a leap year
13] Program to find whether given no
...
#include
...
h>
void main()
{
int n;
clrscr();
printf(“enter any no: ”);
scanf(“%d”,&n);
if(n%2==0)
printf(“no is even”);
else
I NTRODUCTIONC P ROGRAMS
printf(“no is odd”);
getch();
}
Output:
enter any no: 5
no is odd
14] Program to shift inputed data by two bits to the left
...
h>
#include
...
Display Monday to Sunday
...
h>
#include
...
#include
...
h>
void main()
{
int a,b,n,s,m,su,d;
clrscr();
printf(“enter two no’s : ”);
scanf(“%d%d”,&a,&b);
printf(“enter 1 for sum\n2 for multiply\n3for subtraction\n4 for division: ”);
scanf(“%d”,&n);
switch(n)
{
case 1:
s=a+b;
printf(“sum=%d”,s);
break;
case 2:
m=a*b;
printf(“multiply=%d”,m);
break;
case 3:
su=a-b;
printf(“subtraction=%d”,su);
break;
case 4:
d=a/b;
printf(“divission=%d”,d);
break;
21
22
C PROGRAMS
WITH
SOLUTIONS
default:
printf(“wrong input”);
break;
}
getch();
}
Output:
enter two no’s: 8
4
enter 1 for sum
2 for multiply
3 for subtraction
4 for division: 1
sum=12
17] Program to display first 10 natural no
...
#include
...
h>
void main()
{
int i,sum=0;
clrscr();
for(i=1;i<=10;i++)
{
printf(“%d no is= %d\n”,i,I);
sum=sum+i;
}
printf(“sum =%d”,sum);
getch();
}
Output:
1 no is=1
2 no is=2
3 no is=3
4 no is=4
5 no is=5
I NTRODUCTIONC P ROGRAMS
6 no is=6
7 no is=7
8 no is=8
9 no is=9
10 no is=10
sum=55
18] Program to print stars Sequence1:
#include
...
h>
void main()
{
int i,j;
clrscr();
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
printf(“*”);
printf(“\n”);
}
getch();
}
Output:
*
**
***
****
*****
19]
Program to print stars Sequence2
...
h>
#include
...
#include
...
h>
void main()
{
int i,j,k;
clrscr();
for(i=1;i<=3;i++)
{
for(j=3;j>=i;j--)
printf(“ ”);
{
for(k=1;k<=i*2-1;k++)
printf(“*”);
}
printf(“\n”);
}
getch();
}
I NTRODUCTIONC P ROGRAMS
Output:
*
***
*****
21] Program to print Fibonacci series up to 100
...
h>
#include
...
#include
...
h>
void main()
{
int n,i,fact=1;
clrscr();
printf(“Enter any no: ”);
scanf(“%d”,&n);
25
26
C PROGRAMS
WITH
SOLUTIONS
for(i=n;i>=1;i--)
{
fact=fact*i;
}
printf(“Factorial=%d”,fact);
getch();
}
Output:
Enter a no: 5
Factorial=120
23] Program to find whether given no
...
or not
...
h>
#include
...
+1/n
...
h>
#include
...
+n
...
h>
#include
...
#include
...
h>
void main()
{
int a,b,c;
clrscr();
printf(“Read the integers from keyboard:- ”);
scanf(“%d %d”,&a,&b);
c=a&b;
printf(“\nThe Answer after ANDing is: %d ”,c);
getch();
}
Output:
Read the integers from keyboard:- 8 4
The Answer after ANDing is: 0
27] Program to add two number using pointers
...
h>
#include
...
#include
...
h>
void main()
{
int *p1,*p2,sum;
clrscr();
printf(“enter two no’s: ”);
scanf(“%d%d”,&*p1,&*p2);
sum=*p1+*p2;
printf(“sum=%d”,sum);
getch();
}
Output:
enter two no’s: 10
20
sum=30
29] Program to show sum of 10 elements of array & show the average
...
h>
#include
...
2f”,av);
getch();
}
Output:
enter elements of an array: 4
5
6
1
2
3
5
5
4
7
sum=42
average=4
...
in an array
...
h>
#include
...
#include
...
h>
void main()
{
int a[3][2],b[3][2],i,j;
clrscr();
printf(“enter value for a matrix: ”);
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
scanf(“%d”,&a[i][j]);
}
printf(“enter value for b matrix: ”);
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
scanf(“%d”,&b[i][j]);
}
printf(“\na matrix is\n\n”);
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
31
32
C PROGRAMS
WITH
SOLUTIONS
{
printf(“ %d ”,a[i][j]);
}
printf(“\n”);
}
printf(“\nb matrix is\n\n”);
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
{
printf(“ %d ”,b[i][j]);
}
printf(“\n”);
}
getch();
}
Output:
enter value for a matrix: 7
8
9
4
5
6
enter value for b matrix: 3
2
1
4
5
6
a matrix is
7 8
9 4
5 6
I NTRODUCTIONC P ROGRAMS
b matrix is
3 2
1 4
5 6
32] Program to find sum of two matrices
...
h>
#include
...
#include
...
h>
void main()
{
int a[5],b[5],c[5],i;
clrscr();
printf(“enter value for array a ”);
for(i=0;i<5;i++)
scanf(“%d”,&a[i]);
printf(“enter value for array b ”);
for(i=0;i<5;i++)
scanf(“%d”,&b[i]);
for(i=0;i<5;i++)
I NTRODUCTIONC P ROGRAMS
c[i]=a[i]-b[i];
printf(“subtraction”);
for(i=0;i<5;i++)
printf(“ %d ”,c[i]);
getch();
}
Output:
enter value for array a: 7
8
9
4
5
enter value for array b: 4
5
6
1
2
subtraction 3 3 3 3 3
34] Program to find multiplication of two matrices
...
h>
#include
...
#include
...
h>
void main()
{
int a[3][2],b[2][3],i,j;
clrscr();
printf(“Enter value for matrix: ”);
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
scanf(‘%d”,&a[i][j]);
}
printf(“Matrix:\n”);
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
printf(“ %d ”,a[i][j]);
printf(“\n”);
}
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
b[j][i]=a[i][j];
}
printf(“Transpose matrix:\n”);
for(i=0;i<2;i++)
{
for(j=0;j<3;j++)
printf(“ %d ”,b[i][j]);
printf(“\n”);
}
getch();
}
37
38
C PROGRAMS
WITH
SOLUTIONS
Output:
Enter value for matrix: 4
5
6
1
2
3
Matrix:
45
61
23
Transpose matrix:
462
513
36] Program to find the maximum number in array using pointer
...
h>
#include
...
#include
...
h>
void main()
{
char a[50];
clrscr();
printf(“enter any string: ”);
gets(a);
puts(a);
getch();
}
Output:
enter any string: hi everyone
hi everyone
38] Program to find square of a number using functions
...
h>
#include
...
#include
...
h>
void main()
{
void swap(int,int);
int a,b,r;
clrscr();
printf(“enter value for a&b: ”);
scanf(“%d%d”,&a,&b);
swap(a,b);
getch();
}
void swap(int a,int b)
{
int temp;
temp=a;
a=b;
b=temp;
printf(“after swapping the value for a & b is : %d %d”,a,b);
}
Output:
enter value for a&b: 4
5
after swapping the value for a & b : 5 4
I NTRODUCTIONC P ROGRAMS
40] Program to find factorial of a number using functions
...
h>
#include
...
#include
...
h>
void main()
{
void table();
clrscr();
table();
getch();
}
41
42
C PROGRAMS
WITH
SOLUTIONS
void table()
{
int n,i,r;
printf(“enter a no to know table: ”);
scanf(“%d”,&n);
for(i=1;i<=10;i++)
{
r=n*i;
printf(“%d*%d=%d\n”,n,i,r);
}
}
Output:
enter a no to know table: 2
2*1=2
2*2=4
2*3=6
2*4=8
2*5=10
2*6=12
2*7=14
2*8=16
2*9=18
2*10=20
42] Program to show call by value
...
h>
#include
...
#include
...
h>
void main()
{
int a,b,*aa,*bb,swap();
clrscr();
a=5;
b=10;
aa=&a;
bb=&b;
printf(“value of a= %d & value of b=%d before swap”,a,b);
swap(aa,bb);
printf(“\nvalue of a=%d & b=%d after swap”,a,b);
getch();
}
int swap(int *x,int *y)
{
int temp;
temp=*x;
43
44
C PROGRAMS
WITH
SOLUTIONS
*x=*y;
*y=temp;
}
Output:
value of a= 5 & value of b=10 before swap
value of a=10 & b=5 after swap
44] Program to find largest of two numbers using functions
...
h>
#include
...
#include
...
h>
void main()
{
int n;
clrscr();
printf(“enter number: ”);
scanf(“%d”,&n);
if(n<0)
printf(“invalid number”);
else
printf(“%d!=%d”,n,fact(n));
getch();
}
int fact(int x)
{
if(x==0)
return 1;
else
return(x*fact(x-1));
}
Output:
enter number: 5
5!=120
46] Program to find whether a string is palindrome or not
...
h>
#include
...
File Operations:
#include
...
h>
void main()
{
file *fp,*fp1;
char c;
clrscr();
fp=fopen(“test
...
end)\n”);
c=getchar();
while(c!=’#’)
{
fputc(c,fp);
c=getchar();
}
rewind(fp);
fp=fopen(“test
...
c”,”w”);
c=fgetc(fp);
while(c!=eof)
{
fputc(c,fp);
c=fgetc(fp);
}
fclose(fp);
fclose(fp1);
fp1=fopen(“tes
...
Merging One Dimensional Array – Excluding The Repeating Element
#include
...
h>
void main()
{
int a[50],b[50],n1,n2,i,x;
clrscr();
printf(“enter the number of elements in the first array”);
scanf(“%d”,&n1);
printf(“enter the elements\n”);
47
48
C PROGRAMS
WITH
SOLUTIONS
for(i=0;i{
printf(“enter a[%d]”,i+1);
scanf(“%d”,&a[i]);
}
printf(“enter the number of elements in the second array”);
scanf(“%d”,&n2);
printf(“enter the elements\n”);
for(i=0;i{
printf(“enter b[%d]”,i+1);
scanf(“%d”,&b[i]);
}
for(x=0;x{
for(i=0;i{
if(b[i]==a[x])
{
b[i]=’ ‘;
}
}
}
for(i=o;i{
printf(“%d”,a[i]);
}
for(i=0;i{
if(b[i]==’ ‘;)
continue;
else
printf(“%d”,b[i]);
}
getch();
}
I NTRODUCTIONC P ROGRAMS
Output:
Enter the number of elements in the first array
3
Enter the elements
3
5
7
Enter the number of elements in the first array
3
Enter the elements
2
5
9
3 5 7 2 9
49
...
#include
...
h>
#include
...
h>
#include
...
of words=%d”,spc+1);
printf(“\nno
...
of consonants=%d”,cons);
printf(“\nno
...
on special characters=%d”,punc);
getch();
}
Output:
Enter the statement
*Nothing is impossible in the world
...
of words=6
No
...
of consonants=19
No
...
of special characters=1
I NTRODUCTIONC P ROGRAMS
50
...
Display
their values in integer constants
...
h>
#include
...
A program to evaluate the equation y=xn when n is a non-negative integer
...
h>
#include
...
0;
count=1;
while(count<=n)
{
y=y*x;
count++;
}
printf(“x=%f; n=%d; x to power n=%f”, x, n,y);
}
Output:
Enter the values of x and n: 2
...
500000; n=4; x to power n= 39
...
A program to print the multiplication table from 1*1 to 12*10
...
h>
#include
...
Program uses a for loop to print the powers of 2 table for the power 0 to 20, both positive and
negative
...
h>
#include
...
0/(double)p;
printf(“10ld 10%d %20
...
000000000000
2
1
0
...
250000000000
8
3
0
...
062500000000
32
5
0
...
015625000000
128
7
0
...
003906250000
512
9
0
...
000976562500
2048
11
0
...
000244140625
8192
13
0
...
000061035156
32768
15
0
...
000015258789
131072
17
0
...
000003814697
524288
19
0
...
000000953674
4
...
A program to read the marks
obtained by each student in various subjects and to compute and print the total marks obtained
by each of them
...
h>
#include
...
The program illustrates the use of the break statement in a C program
...
h>
#include
...
000000
Average=23
...
A program to evaluate the series 1/1-x= 1+x+x2 +x3 +
...
#include
...
h>
#define LOOP 100
#define ACCURACY 0
...
of terms=%d”, sum,n);
end:
;
}
Output:
Input value of x:
...
265800; No
...
75
EXIT FROM LOOP
Sum=3
...
of terms=34
Input value of x:
...
Program illustrates the use of continue statement
...
h>
#include
...
h>
void main()
FUNDAMENTALS C PROGRAMS
{
int count, negative;
double number, sqroot;
printf(“enter 9999 to STOP”);
count=0;
negative=0;
while(count<=100)
{
printf(“enter a number:”);
scanf(“%lf”, &number);
if(number==9999)
break;
if(number<0)
{
printf(“Number is negative ”);
negative++;
continue;
}
sqroot=sqrt(number);
printf(“Number=%lf square root=%lf ”, number, sqroot);
count++;
}
printf(“Number of items done =%d”, count);
printf(“Negative items=%d”, negative);
printf(“END OF DATA”);
}
Output:
Enter 9999 to STOP
Enter a number: 25
...
000000
Square root =5
...
5
Number =40
...
363961
Enter a number:-9
59
60
C P ROGRAMS
WITH
SOLUTIONS
Number is negative
Enter a number: 16
Number= 16
...
000000
Enter a number: -14
...
000000
Square root=8
...
Program to print binomial coefficient table
...
h>
#include
...
Program to draw a histogram
...
h>
#include
...
Program of minimum cost problem
...
h>
#include
...
1)
{
cost=40-8*p+p*p;
if(p= =0)
63
64
C P ROGRAMS
WITH
SOLUTIONS
{
cost1=cost;
continue;
}
if(cost>=cost1)
break;
cost1=cost;
p1=p;
}
p=(p+p1)/2
...
2f AT p=%
...
00 AT p=4
...
Program for plotting of two functions (y1=exp(-ax); y2=exp(-ax2 /2))
...
h>
#include
...
h>
void main()
{
int i;
float a, x, y1, y2;
a=0
...
25)
y1=(int) (50*exp(-a*x)+0
...
5);
if(y1= =2)
{
if(x= = 2
...
5)
printf(“X |”);
else
printf(“ |”);
for(i=1; i<=y2-1; ++i)
printf(“ ”);
printf(“*”);
for(i=1; i<=(y1-y2-1); ++i)
printf(“_”);
printf(“0”);
continue;
}
if(x==2
...
Write a program using a single –subscribed variable to evaluate the following expressions:
10
Total =
∑ X2
2
the values of x1, x2,
...
i =1
#include
...
h>
void main()
{
int i;
float x[10], value, total;
printf(“ENTER 10 REAL NUMBERS”);
for(i=0; i<10; i++)
{
scanf(“%f”, &value);
x[i]=value;
}
total=0
...
2f”, i+1, x[i]);
printf(“total=%
...
1 2
...
3 4
...
5 6
...
7 8
...
9 10
...
10
X[2]=2
...
30
X[4]=4
...
50
X[6]=6
...
70
X[8]=8
...
90
X[10]=10
...
86
13
...
43 65 51 27 79 11 56 61 82 09 25 36 07 49 55 63 74 81 49 37 40 49 16 75 87 91 33 24 58 78 65
56 76 67 45 54 36 63 12 21 73 49 51 19 39 49 68 93 85 59
Write a program to count the number of students belonging to each of the following groups of
marks: 0-9,10-19,20 -29,----,100
...
h>
#include
...
Write a program for sorting the elements of an array in descending order
...
h>
#include
...
Write a program for finding the largest number in an array
#include
...
h>
void main()
{
int *arr, i, j, n, LARGE;
clrscr();
printf(“Enter the number of elements in the array”);
scanf(“%d”, &n);
arr=(int*) malloc(sizeof(int)*n);
for(i=0; i{
printf(“Enter a number”);
scanf(“%d”, &arr[i]);
}
LARGE=arr[0];
for(i=1; i{
if(arr[i]>LARGE)
LARGE=arr[i];
}
printf(“The largest number in the array is : %d”, LARGE);
getch();
}
FUNDAMENTALS C PROGRAMS
Output:
Enter the number of elements in the array:5
Enter a number: 32
Enter a number: 43
Enter a number: 23
Enter a number: 57
Enter a number: 47
The largest number in the array is : 57
16
...
#include
...
h>
#include
...
Write a program for finding the desired kth smallest element in an array
...
h>
#include
...
Program to sort a list of numbers and to determine median
...
h>
#include
...
0;
else
median=a[n/2+1];
for(i=1; mi<=n; i++)
printf(“%f”, a[i]);
printf(“median is %f”, median);
}
Output:
Enter the number of items
5
Input 5 values
1
...
222 3
...
444 5
...
555000 4
...
333000 2
...
111000
Median is 3
...
000000 8
...
000000 5
...
000000 3
...
500000
19
...
#include
...
h>
#include