main() / void main() / int main() / int main(void) / int main(int argc, char *argv[])
A very common question is "What's the difference between void main and int main?". This particular FAQ tries to answer that and more, covering other versions of the main() implementation.
The first thing to note is that this is one of those topics that people seem to like to argue over for hours, days and more. Some arguments are valid, some are not, and some are just plain old opinion.
The C and C++ standards differ when it comes to main(), so I'll detail each one separately.
For C
Under C89, main() is acceptable, although it is advisable to use the C99 standard, under which only these are acceptable:
int main ( void )int main ( int argc, char *argv[] )
Slight variations of the above are acceptable, where int can be replaced by a typedef name defined as int, or the type of argv can be written as char ** argv, and so on.
The first option is used when you do not require access to the command line arguments.
The names argc and argv are identifiers that can be changed if you so desire, but sticking to argc/argv is convention.
The return type of main() must always be an int, this allows a return code to be passed to the invoker.
Under C89, the return statement at the end of main() is required, whereas under C99 if no return statement is present, return 0 is implied. However, it is good programming practice to always use a return statement, even if you don't have to.
For C++
The following are acceptable uses:
int main ( int argc, char *argv[] )int main ()
The first option follows similar conventions to those used by C99.
The second option is used when you do not require access to the command line arguments, and is equivalent to the int main(void) option used by C99.
Again, the return type must always be an int, and the function should return 0; at the end, but it is not required by the standard.
(C) The difference between int main() and int main(void)
A common misconception for C programmers, is to assume that a function prototyped as follows takes no arguments:
int foo();
In fact, this function is deemed to take an unknown number of arguments. Using the keyword void within the brackets is the correct way to tell the compiler that the function takes NO arguments.
What's the deal with void main()
Under regular function calling/returning in C and C++, if your don't ever want to return anything from a function, you define it's return type as void. For example, a function that takes no arguments, and returns nothing can be prototyped as:
void foo(void);
A common misconception is that the same logic can be applied to main(). Well, it can't, main() is special, you should always follow the standard and define the return type as int. There are some exceptions where void main() is allowed, but these are on specialised systems only. If you're not sure if you're using one of these specialised systems or not, then the answer is simply no, you're not. If you were, you'd know it.
Be warned that if you post your "void main" code on the forums, you're going to get told to correct it. Responding with "my teacher said it's OK" is no defence; teachers have a bad habit of being wrong. Be safe, and post only standard code, and you'll find people concentrate on answering your other problems, rather than waste time telling you about this type of thing.
But what about int main(int argc, char *argv[], char *envp[])
As an extension to the guaranteed standard, an additional parameter to main() can, on some systems, be used to gain access to the environment variables. This is isn't guaranteed to work on all compilers, so use it with care if you want to keep your code portable.
===================================================================
A very common question is "What's the difference between void main and int main?". This particular FAQ tries to answer that and more, covering other versions of the main() implementation.
The first thing to note is that this is one of those topics that people seem to like to argue over for hours, days and more. Some arguments are valid, some are not, and some are just plain old opinion.
The C and C++ standards differ when it comes to main(), so I'll detail each one separately.
For C
Under C89, main() is acceptable, although it is advisable to use the C99 standard, under which only these are acceptable:
int main ( void )int main ( int argc, char *argv[] )
Slight variations of the above are acceptable, where int can be replaced by a typedef name defined as int, or the type of argv can be written as char ** argv, and so on.
The first option is used when you do not require access to the command line arguments.
The names argc and argv are identifiers that can be changed if you so desire, but sticking to argc/argv is convention.
The return type of main() must always be an int, this allows a return code to be passed to the invoker.
Under C89, the return statement at the end of main() is required, whereas under C99 if no return statement is present, return 0 is implied. However, it is good programming practice to always use a return statement, even if you don't have to.
For C++
The following are acceptable uses:
int main ( int argc, char *argv[] )int main ()
The first option follows similar conventions to those used by C99.
The second option is used when you do not require access to the command line arguments, and is equivalent to the int main(void) option used by C99.
Again, the return type must always be an int, and the function should return 0; at the end, but it is not required by the standard.
(C) The difference between int main() and int main(void)
A common misconception for C programmers, is to assume that a function prototyped as follows takes no arguments:
int foo();
In fact, this function is deemed to take an unknown number of arguments. Using the keyword void within the brackets is the correct way to tell the compiler that the function takes NO arguments.
What's the deal with void main()
Under regular function calling/returning in C and C++, if your don't ever want to return anything from a function, you define it's return type as void. For example, a function that takes no arguments, and returns nothing can be prototyped as:
void foo(void);
A common misconception is that the same logic can be applied to main(). Well, it can't, main() is special, you should always follow the standard and define the return type as int. There are some exceptions where void main() is allowed, but these are on specialised systems only. If you're not sure if you're using one of these specialised systems or not, then the answer is simply no, you're not. If you were, you'd know it.
Be warned that if you post your "void main" code on the forums, you're going to get told to correct it. Responding with "my teacher said it's OK" is no defence; teachers have a bad habit of being wrong. Be safe, and post only standard code, and you'll find people concentrate on answering your other problems, rather than waste time telling you about this type of thing.
But what about int main(int argc, char *argv[], char *envp[])
As an extension to the guaranteed standard, an additional parameter to main() can, on some systems, be used to gain access to the environment variables. This is isn't guaranteed to work on all compilers, so use it with care if you want to keep your code portable.
===================================================================
DIFFERENCE BETWEEN MALLOC CALLOC
===============================================================
DIFFERENCE BETWEEN MALLOC CALLOC
Both malloc and calloc do the same thing with almost the same results, they allocate a block of n * sizeof ( T ) bytes of memory. The difference is in how the functions are called and how the memory block is initialized. malloc is called like so:
p = malloc ( n * sizeof ( T ) );
where the user programmer performs the operation to find the final block size. calloc takes two arguments and performs the operation itself:
p = calloc ( n, sizeof ( T ) );
malloc leaves the block of memory uninitialized, but calloc zero fills the memory. This does not mean that
p = calloc ( 1, sizeof ( int * ) );
will result in a pointer to int with a value of NULL. Zero filled memory does not mean that the memory is filled with the data type's equivalent of 0, so this zero fill cannot be relied on except with integral values such as int or char. Pointers and floating point may use a different representation for 0 than all bits zero.
Because of this zero fill, calloc can be slightly less efficient than malloc.
DIFFERENCE BETWEEN MALLOC CALLOC
Both malloc and calloc do the same thing with almost the same results, they allocate a block of n * sizeof ( T ) bytes of memory. The difference is in how the functions are called and how the memory block is initialized. malloc is called like so:
p = malloc ( n * sizeof ( T ) );
where the user programmer performs the operation to find the final block size. calloc takes two arguments and performs the operation itself:
p = calloc ( n, sizeof ( T ) );
malloc leaves the block of memory uninitialized, but calloc zero fills the memory. This does not mean that
p = calloc ( 1, sizeof ( int * ) );
will result in a pointer to int with a value of NULL. Zero filled memory does not mean that the memory is filled with the data type's equivalent of 0, so this zero fill cannot be relied on except with integral values such as int or char. Pointers and floating point may use a different representation for 0 than all bits zero.
Because of this zero fill, calloc can be slightly less efficient than malloc.
DIFFERENCES BETWEEN C and CPP
===================================================
DIFFERENCES BETWEEN C and CPP
Put simply, C++ is an extended C meant to improve safety, give the programmer more options, simplify higher level programming, and offer a better approach to large scale programming. C++ is also a larger language with more features and complexity than C, but C++ can improve productivity with its greater amount of features. A list of features that C++ supports which C does not includes:
Classes
Member functions
Constructors and destructors
Derived classes
Virtual functions
Abstract classes
Access control (public, private, protected)
friend functions
Pointers to members
static members
mutable members
Operator overloading
References
Templates
Inline functions
Default arguments
Function overloading
Namespaces
Exception handling
Run-time type identification
// comments
True const
Declarations as statements
Automatically typedef'd struct tags
Type safe linkage
new and delete
bool keyword
Safer and more robust casting
===============================================================
DIFFERENCES BETWEEN C and CPP
Put simply, C++ is an extended C meant to improve safety, give the programmer more options, simplify higher level programming, and offer a better approach to large scale programming. C++ is also a larger language with more features and complexity than C, but C++ can improve productivity with its greater amount of features. A list of features that C++ supports which C does not includes:
Classes
Member functions
Constructors and destructors
Derived classes
Virtual functions
Abstract classes
Access control (public, private, protected)
friend functions
Pointers to members
static members
mutable members
Operator overloading
References
Templates
Inline functions
Default arguments
Function overloading
Namespaces
Exception handling
Run-time type identification
// comments
True const
Declarations as statements
Automatically typedef'd struct tags
Type safe linkage
new and delete
bool keyword
Safer and more robust casting
===============================================================
DIFFERENCES BETWEEN COUT,PRINTF
=================================================================
DIFFERENCES BETWEEN COUT,PRINTF
printf is a function that takes a variable number of arguments, the first argument being a format string explaining how to treat further arguments. cout is an object of the std::ostream class. The two are completely different in form, yet the result is the same, data sent to stdout.
This question is difficult to answer completely as often phrased since cout and printf are wildly different, but most often one of three questions are meant:
Q: "Which is faster?" A: printf, but the difference is too slight to worry about.
Q: "Which is better?" A: Neither, it depends heavily on personal preference and what needs to be done.
Q: "Which should I use?" A: You should use whichever you feel most comfortable with.It is preferred that you use iostreams with C++ instead of the C functions because iostreams are type safe and extensible for user defined types, both of which are very good reasons.
===================================================================
DIFFERENCES BETWEEN COUT,PRINTF
printf is a function that takes a variable number of arguments, the first argument being a format string explaining how to treat further arguments. cout is an object of the std::ostream class. The two are completely different in form, yet the result is the same, data sent to stdout.
This question is difficult to answer completely as often phrased since cout and printf are wildly different, but most often one of three questions are meant:
Q: "Which is faster?" A: printf, but the difference is too slight to worry about.
Q: "Which is better?" A: Neither, it depends heavily on personal preference and what needs to be done.
Q: "Which should I use?" A: You should use whichever you feel most comfortable with.It is preferred that you use iostreams with C++ instead of the C functions because iostreams are type safe and extensible for user defined types, both of which are very good reasons.
===================================================================
DIFFERENCES BETWEEN NULL AND 0, \0
==============================================================
DIFFERENCES BETWEEN NULL AND 0, \0
NULL is a macro defined in several standard headers, 0 is an integer constant, '\0' is a character constant, and nul is the name of the character constant. All of these are *not* interchangeable:
NULL is to be used for pointers only since it may be defined as ((void *)0), this would cause problems with anything but pointers.
0 can be used anywhere, it is the generic symbol for each type's zero value and the compiler will sort things out.
'\0' should be used only in a character context.
nul is not defined in C or C++, it shouldn't be used unless you define it yourself in a suitable manner, like:
#define nul '\0'
=============================================================
DIFFERENCES BETWEEN NULL AND 0, \0
NULL is a macro defined in several standard headers, 0 is an integer constant, '\0' is a character constant, and nul is the name of the character constant. All of these are *not* interchangeable:
NULL is to be used for pointers only since it may be defined as ((void *)0), this would cause problems with anything but pointers.
0 can be used anywhere, it is the generic symbol for each type's zero value and the compiler will sort things out.
'\0' should be used only in a character context.
nul is not defined in C or C++, it shouldn't be used unless you define it yourself in a suitable manner, like:
#define nul '\0'
=============================================================
Saturday, July 26, 2008
C programming Theoretical and Programs
=====================================================
C programming Theoretical and Programs
===========================================================
(1)List the five c compiler?Ans:
Name Work on O.S Name of microprocessor
1. Turbo c M.S DOS 8086
2. Ansic c LINUX/UNIX 80386
3. Borland c WINDOW 80386
4. Microsoft c M.S DOS 8086
5. Visual c++ WINDOW 80386
Note:- 8086 is 16 bit microprocessor while 80386 is 32 bit microprocessor.
(2) Describe turbo c compiler?Ans:Name Work on O.S Name of microprocessor
1. Turbo c M.S DOS 8086
2. Ansic c LINUX/UNIX 80386
3. Borland c WINDOW 80386
4. Microsoft c M.S DOS 8086
5. Visual c++ WINDOW 80386
Note:- 8086 is 16 bit microprocessor while 80386 is 32 bit microprocessor.
Turbo c compiler is one of the most popular c compiler.It is based on DOS operating system.It uses 8086 microprocessor which is 16 bit microprocessor. It has 20 address buses
and 16 data bus. It’s word length is two byte.
(3) What is hexadecimal number system ?
Ans:
In hexadecimal number system we use 16 differen digit so it’s base is 16
TABLE
Hexadecimal digit decimal equivalent binary equivalent
0 0 0000
1 1 0001
2 2 0010
3 3 0011
4 4 0100
5 5 0101
6 6 0110
7 7 0111
8 8 1000
9 9 1001
A 10 1010
B 11 1011
C 12 1100
D 13 1101
E 14 1110
F 15 1111
To convert the binary number into hexadecimal number:
Make the group of four binary digit from right to left put the equivalent hexadecimal digit using TABLE.
e.g binary number =11000111110101
group of four digit from right 11 0001 1111 0101
to make group of four digit of left most digit 11,add two zero to the leftt side i.e 0011
now put the eqivalent hexadecimal digit form table
0011 0001 1111 0101
3 1 F 5
So,equivalent hexadecimal number will be 31F5
(4) What will address range which can repersent in 20 bit ?Ans:
in binary in hexadecimal
Minimum possible number 0000 0000 0000 0000 0000 0000in binary in hexadecimal
Maximum possible number 1111 1111 1111 1111 1111 FFFF
In c any hexadecimal number statr with 0x 0r 0X
So,address range will be 0×0000 to 0xFFFF
It is 1MB memory range.
Note.
2^10 = 1KB
2^20 = 1MB
2^30 = 1GB
Where 10,20,30 are number of bit.
(5)What is difference betweent TSR and TSO program?
Ans :-
TSO means terminate but stay outside.It is those program, which release the main memory after the executon of the program.e.g Vcd cutter, turbo c compiler.
TSR means terminate but stay residence .It is those program, which after the execution of the program does not release the RAM (main memory).e.g antivirus.
(1) Why there are so many c compilers?
Ans:
(3)How many keywords are in c?
Ans:
43
(6)What is difference between .com program and .exe program?
Ans:
Both .com and .exe program are executable program but .com program execute faster than .exe program.All driver are .com program.
(2) How many type of error in c.
Ans:
183
Memory orgnization
To be a good programmer is very necessay to understand the memory structure.
(1) What is memory cell?Ans :-
TSO means terminate but stay outside.It is those program, which release the main memory after the executon of the program.e.g Vcd cutter, turbo c compiler.
TSR means terminate but stay residence .It is those program, which after the execution of the program does not release the RAM (main memory).e.g antivirus.
(1) Why there are so many c compilers?
Ans:
(3)How many keywords are in c?
Ans:
43
(6)What is difference between .com program and .exe program?
Ans:
Both .com and .exe program are executable program but .com program execute faster than .exe program.All driver are .com program.
(2) How many type of error in c.
Ans:
183
Memory orgnization
To be a good programmer is very necessay to understand the memory structure.
Ans:
Entire RAM has divided in number of equal part, which is known as memory cell.Capcity of each cell is to store one-byte data.
i.e char a resevre one memory cell while float a reseve four memory cell.
Each memory cell has unique addresss.Address are always in whole number an incresing order.
(6) What is residence memory?Ans:
RAM has divided into two parts:
(1) Extended memory (useless)
(2) Residence memory :
When any program is excuted it is stored in the residence memory .For turbo c, it has 1MB residence memory i.e when we open turbo c it store 1MB in the RAM.
(3) What is physical address ?
Ans:
20 bit address of the memory cell is known as physical address or real address.In 20 bit we can repersent address from 0×00000 to 0xFFFFF.
(4) What is segmentation?
Ans:
Residential memory of RAM of size 1MB has divided into 16 equal part.These part is called segment.Each segment has size is 64KB.
1MB=16*64KB
This process of division is known as segmentation.
(5) What is necesity of segmentation?
Ans:
Physical address are 20 bit.But we have no pointer of 20 bit.So pointer can not access whole residential address .So to solve this problem we have three different pointer and segmentation has done.
(6) What is offset address?
Ans:
Each segment has divided into two parts.
1. Segment no (4bit)
2. Offset address (16 bit)
Each segment has same offset address but different segment number.
Suppose physical address is 0×500F1
Then it’s segment number is 5 and offset address is 00F1.
(7) Write a program to find the offset address of any variable?
Ans:
Void main ()
{
int x;
scanf(“%d”,&x);
printf(“%p”,x);
}
Note. %p is used to find the offset address (in hexadecimal) of any variable.
(
Ans:
Segment number 8 has special name which is known as data segment.
It has divided into four parts.
1. Stack area:-
All automatic variables are created into stack area.Default storage class of any local variable is auto.This variable may be int, char, float, array, pointer, struct, union etc.It also return fuction argument and return address.It follow LIFO datastructure. It has two part one for initialize variable another for non-initialize variable.All initialize variable are more nearer than unintialize variable and vice versa.
2. Data area :
All static and extern variable are created in the data area.
3. Heap area:
Malloc and calloc always allocate memory in the heap area.It is used for dynamic memory allocation.It’s size depends upon free space in the memory.
4. Code area:
Fuction pointer can only access code area.Size of this area is always fixed and it is read only area.
(10) What will output:
void main()
{
int a=5,b=6,c=7;
printf(“%d,%d,%d”);
}
Ans:
Output: 7 6 5
Explanation:
Default sotrage class int a=5 is auto.Since it automatic variable it will create in the stack area.
It will store in the stack as
Stack always follows LIFO datastructure.
In the printf statement name of variable is not written explicitly.So default output will content of
Stack which will be in the LIFO order i.e 7 6 5.
(9) what will be output:
void main()
{
int a=5 ,b,c=7;
printf(“%d %d %d”);
}
Ans:
Output: 7 5 garbage value
Explanation:
Automatic variable a and c has initialized while b has not initilize.Initialize variable are more nearer than non initialize variable .They will be stored in the stack.So due to LIFO first output will be 7 then 6 (since a is more nearer than b with respect to c) then any garbage value will be output which is persent in the stack.
(7) How many number system in c.Ans:
There are three type of number system in c:
1. Decimal number e.g 25
2. Octal number e.g 025
3. Hexadecimal number e.g 0×25
( There are three type of number system in c:
1. Decimal number e.g 25
2. Octal number e.g 025
3. Hexadecimal number e.g 0×25
Octal integer constant (Since it starts with zero)
Note. C has not binary integer constant.
(9) Pointer(1) What is pointer ?
Ans:
Pointer is variable which contain address of another variable.
e.g
1.
int i=3;
meaning:-
6024 any arbitary address.Ans:
Pointer is variable which contain address of another variable.
e.g
1.
int i=3;
meaning:-
2.
int i=3;
int *j;
j=&i;
meanig:
Here j is pointer it contain 6024 which is address of another variable i.
3.
int i=3;
int *j;
int **k;
j=&i; //line 1
k=&j; //line 2
Meaning:
Here both i and j are pointer where j contian 6024 which is address of variable I while j contain 8085 which is address of another variable which contain address of variable i.
Now printf(“%d”, **k);
printf(“%d”,*j);
printf(“%d”,i);
Above all printf will give same output : 3
Explanation:
* and & are canceled to each other i.e *&a=a
So **k
= **(&j) //from line 1
= *(*&j)
= *j //* and & are canceled to each other.
= *(&i) //from line 2
= *&i
= i
Thus **k = i ,hence **k is giving output 3
Same way *j = i
(2) What will output:
void main()
{
int i=3;
int *j;
int **k;
j=&i;
k=&j;
printf(“%u %u %u”,i,j,k);
}
Ans:
Here 6024 ,8085,9091 is any arbitrary address,it may be different.
Since contiant of i is 3,so first output will 3
Contiant of j is 6024 ,so second output will 6024
Containt of k is 8085 ,so third output will 9091
Output: 3 6024 8085
Note:-Address is always a whole number ,which can not be negative so we generally is %u instead of %d
(3) How can I know given statement is correct or not when pointer is very complex format ?
Int
(2) What is null pointer and generic pointer ?
Ans:
Null pointer:- A pointer which dosen’t point any data is known as null pointer.
Generic pointer :-A pointer to the void is known as geric pointer.
e.g void *p;
here p is generic pointer.
(1)How many pointers are in turbo C?
Ans:
3
1. Near pointer (16 bit)
2. Far pointer (32 bit)
3. Huge pointer (32 bit)
(2) What is memory model and how many memory models?
Ans:
According to size of program in code area,data area and stack area
there are six type of memory model:
1. Tiny
2. Small (default)
3. Medium
4. Compact
5. Large
6. Huge
Note: - to change memory model (in turbo c) go to
Option->compiler->code generation.
Memory model decides default type of pointer.
(10) What will output
Void main(){
int * p,b;
b=sizeof(p);
printf(“%d”,b);
}
Ans:
Output: 2 or 4
Explanation:
Since in this question it has not written p is which type pointer. So it’s output will depends upon which memory model has selected. Default memory model is small.
Memory model default pointer size
Tiny, Small (default), Medium near 2
Compact,Large,Huge far 4
(11) near pointer?
Ans:
It is 16 bit pointer.It can hold the address of variable only within 64KB data segment.It store only offset address of any vriable.This offset address in cyclic in nature .
If you will increment the near pointer then offset address will reach maximum value FFFF (in hexadecimal) then 0000 and so on.
For example :
#include
void main()
{ What is
int a=5,I;
int *ptr; //by default it is near pointer.
ptr=&a;
for(i=0;i<300;i++)
{
printf(“\n %p”,ptr);
p++;
delay(100);
}
}
Output :
Here int two byte data type so every time offset address is increasing by two. %p gives offset address in hexadecimal number. We can perform ++,– ,+,- relation operator (>,<,==,….)
operation on offset address. We cannot perform following task.
1. Addition of two offset address.
2. Multiplication and division of two offset address or one offset address and another number.
(12) What is far pointer?Ans:
Far pointer is 32 bit pointer .It can hold the address of variable in the data segment as well as outside of the data segment. This type modifier is usually used when compiling in the tiny, small or compact memory model to force the pointer to be far.
It stores both offset address as well as segment number of any variable But when you will increment far address then only offset address will increase (same way as in near pointer).Its segment number never will change.Far pointer is 32 bit pointer .It can hold the address of variable in the data segment as well as outside of the data segment. This type modifier is usually used when compiling in the tiny, small or compact memory model to force the pointer to be far.
In dos.h header file there two function that can find offset address and segment address which take the argument far pointer and return unsigned int.
Syntax:
unsigned int FP_OFF(void far *)
unsigned int FP_SEG(void far *)
e.g
#include
void main()
{
unsigned int i,offset_addr,seg_addr,;
char far *ptr=(char far *) 5555 FF99;
for (i=0;i<300;i++)
{
seg_addr=FP_SEG(p);
offset_addr=FP_OFF(p);
printf(“\n seg_addr %x offset_addr %x ”,seg_addr,offset_addr);
p++;
delay(100);
}
}
Output :
You can see its segment address is not changing. In relational operation whole far address is compared.
(13)How we can calculate actual memory address if we know segment and offset address ?Ans:
Physical address =segment address * 16 + offset address; (in decimal)
Physical address =segment address * 0×10 + offset address; (in hexadecimal)
e.gPhysical address =segment address * 16 + offset address; (in decimal)
Physical address =segment address * 0×10 + offset address; (in hexadecimal)
Let far address is 0×59994444 (in c hexadecimal integer constant has prefix 0x);
offset address is 0×4444
segment address is 0×5999
temp=segmemt addrss *0×10
=0×59990 (trick : only write one 0 to the right side )
=0×9990 (since offset address can have only for hexadecimal digit ,so remove the last digit)
Physical address =temp+offset address
=0×9990+0×4444
=0×0DDD4 (physical address are 20 bit so it is represented in 5 hexadecimal digit)
Process of conversion of 32 bit far address into 20 bit physical address is called normalization.
Note: It is possible that two different far address can represent same physical address.
(14)What is huge pointer ?
Ans : Is is 32 bit pointer .Its is similar to far pointer but any arithmetic or relational operation is performed in huge address then first it is normalize.
What will be output:
void main()
{
int huge *a=(int huge *)0×59990005;
int huge *b=(int huge *)0×59980015;
if(a==b)
printf(”power of pointer”);
else
printf(”power of c”);
getch();
}
Output: power of pointer
void main()
{
int huge *a=(int huge *)0×59990005;
int huge *b=(int huge *)0×59980015;
if(a==b)
printf(”power of pointer”);
else
printf(”power of c”);
getch();
}
Output: power of pointer
Explanation:
Here we are performing relational operation between two huge address. So first both a and b will normalize.
a=(0×5999)* (0×10)+(0×0005)=0×9990+0×0005=0×9995
b=(0×5998)* (0×10)+(0×0015)=0×9980+0×0015=0×9995
Here both huge address is representing same physical address. So a==b is true.
Note. Two 32 bit address can be represent same physical address but two physical address must be unique.
a=(0×5999)* (0×10)+(0×0005)=0×9990+0×0005=0×9995
b=(0×5998)* (0×10)+(0×0015)=0×9980+0×0015=0×9995
Here both huge address is representing same physical address. So a==b is true.
Note. Two 32 bit address can be represent same physical address but two physical address must be unique.
How will we read complex pointer ?
Rule 1. Give the first priority to the name (identifier) of pointer variable.
Rule 2. Give the least priority of to the data type with modifier (like int,char,float,unsigned int,static int etc.)
Rule 3. () and [] operator has higher priority (associativity left to right ) than * ,& (associativity right to left)
Priority: It means operator which have highest priority will execute first.
Associativity: If associativity is left to right and two operator have same priority then left most operator will have more priority and vice versa.
(to read the priority and associativity of each operator visit in the link operator then precedence table)
Rule 2. Give the least priority of to the data type with modifier (like int,char,float,unsigned int,static int etc.)
Rule 3. () and [] operator has higher priority (associativity left to right ) than * ,& (associativity right to left)
Priority: It means operator which have highest priority will execute first.
Associativity: If associativity is left to right and two operator have same priority then left most operator will have more priority and vice versa.
(to read the priority and associativity of each operator visit in the link operator then precedence table)
How to read
If right side of name (identifier) is ( ) then read function after the function read all as return type
Read [ ] operator array and after this read as it contain
Read * pointer if you have not encounter function or array otherwise read address.
If you will read example then you will understand more easily.
Example 1.If right side of name (identifier) is ( ) then read function after the function read all as return type
Read [ ] operator array and after this read as it contain
Read * pointer if you have not encounter function or array otherwise read address.
If you will read example then you will understand more easily.
Read the pointer
int (*ptr)();
Ans:
Give first priority to ptr :1
There are two () operator ,associativity of () operator is left to right so left most operator will have more priority.
Left most ( ) operator will execute first . So give * to the second priority and right most ( ) operator to the third priority.
Give fourth priority to int
Read : ptr is pointer to such function which return type is integer data type.
Program:
#include
#include
void main()
{
int (*ptr1)();
int (*ptr2)();
void (*ptr3)();
ptr1=puts;
ptr2=getch;
ptr3=clrscr;
(*ptr3)();
(*ptr1)(”POWER OF POINTER”);
(*ptr2)();
Output: POWER OF POINTER
Example 2.
char *arr [3];
Ans :
arr is array of size 3 which contain address of char#include
#include
void main()
{
int (*ptr1)();
int (*ptr2)();
void (*ptr3)();
ptr1=puts;
ptr2=getch;
ptr3=clrscr;
(*ptr3)();
(*ptr1)(”POWER OF POINTER”);
(*ptr2)();
Output: POWER OF POINTER
Example 2.
char *arr [3];
Ans :
program:
void main()
{
char *arr[3];
char a=1,b=2,c=3;
arr[0]=&a;
arr[1]=&b;
arr[2]=&c;
clrscr();
printf(”%u %u”,&b,arr[1]);
printf(”\n%d”,*arr[2]);
getch();
}
Output :
any addresss same address 3
Example 3void main()
{
char *arr[3];
char a=1,b=2,c=3;
arr[0]=&a;
arr[1]=&b;
arr[2]=&c;
clrscr();
printf(”%u %u”,&b,arr[1]);
printf(”\n%d”,*arr[2]);
getch();
}
Output :
any addresss same address 3
char (*ptr)[5];
Ans:
ptr is pointer to array of size 5 which contain char data type.
Program:
#include
#include
void main()
{
char arr[5]={1,2,3,4,5};
char (*ptr)[5];
ptr=&arr;
ptr=(*ptr)+2;
clrscr();
printf(”%d”,**ptr);
getch();
}
Output: 3
Example 4
unsigned long int ( * avg ())[ 3]
#include
#include
void main()
{
char arr[5]={1,2,3,4,5};
char (*ptr)[5];
ptr=&arr;
ptr=(*ptr)+2;
clrscr();
printf(”%d”,**ptr);
getch();
}
Output: 3
Example 4
unsigned long int ( * avg ())[ 3]
Program:
#include
#include
unsigned long int (* avg())[3]
{
static unsigned long int arr[3]={1,2,3};
return &arr;
}
void main()
{
unsigned long int (*ptr)[3];
ptr=avg();
clrscr();
printf(”%d”,*(*ptr+2));
getch();
}
Output :3
#include
#include
unsigned long int (* avg())[3]
{
static unsigned long int arr[3]={1,2,3};
return &arr;
}
void main()
{
unsigned long int (*ptr)[3];
ptr=avg();
clrscr();
printf(”%d”,*(*ptr+2));
getch();
}
Output :3
C PROGRAMS
C PROGRAMS
==========================================
c program for to check Armstrong number
void main()
{
int num,r,sum=0,temp;
clrscr();
printf(”\nEnter a number:-”);
scanf(”%d”,&num);
temp=num;
while(num!=0)
{
r=num%10;
num=num/10;
sum=sum+(r*r*r);
}
if(sum==temp)
printf(”\nThe number %d is an armstrong number”,temp);
else
printf(”\nThe number %d is not an armstrong number”,temp);
getch();
}
{
int num,r,sum=0,temp;
clrscr();
printf(”\nEnter a number:-”);
scanf(”%d”,&num);
temp=num;
while(num!=0)
{
r=num%10;
num=num/10;
sum=sum+(r*r*r);
}
if(sum==temp)
printf(”\nThe number %d is an armstrong number”,temp);
else
printf(”\nThe number %d is not an armstrong number”,temp);
getch();
}
c program to reverse any number
void main()
{
int num,out;
clrscr();
scanf(”%d”,&num);
out=rev(num);
printf(”%d”,out);
getch();
}
int rev(int num)
{
static sum,r;
if(num)
{
r=num%10;
sum=sum*10+r;
rev(num/10);
}
else return 0;
return sum;
}
{
int num,out;
clrscr();
scanf(”%d”,&num);
out=rev(num);
printf(”%d”,out);
getch();
}
int rev(int num)
{
static sum,r;
if(num)
{
r=num%10;
sum=sum*10+r;
rev(num/10);
}
else return 0;
return sum;
}
c program for to check prime number
void main()
{
int num,i,count=0;
clrscr();
printf(”\nEnter a number:”);
scanf(”%d”,&num);
for(i=1;i<=num;i++)
{
if(num%i==0)
count++;
}
if(count==2)
printf(”%d is a prime number”,num);
else
printf(”%d is not a prime number”,num);
getch();
}
{
int num,i,count=0;
clrscr();
printf(”\nEnter a number:”);
scanf(”%d”,&num);
for(i=1;i<=num;i++)
{
if(num%i==0)
count++;
}
if(count==2)
printf(”%d is a prime number”,num);
else
printf(”%d is not a prime number”,num);
getch();
}
c program to find sum of the digit of number
void main()
{
int num,sum=0,r;
clrscr();
printf(”\nEnter a number:”);
scanf(”%d”,&num);
while(num)
{
r=num%10;
num=num/10;
sum=sum+r;
}
printf(”sum=%d”,sum);
getch();
}
{
int num,sum=0,r;
clrscr();
printf(”\nEnter a number:”);
scanf(”%d”,&num);
while(num)
{
r=num%10;
num=num/10;
sum=sum+r;
}
printf(”sum=%d”,sum);
getch();
}
Write a c program to check the given number is palindrome number or not
void main()
{
int num,r,sum=0,temp;
clrscr();
printf(”\nEnter a number:”);
scanf(”%d”,&num);
temp=num;
while(num)
{
r=num%10;
num=num/10;
sum=sum*10+r;
}
if(temp==sum)
printf(”\n%d is a palindrome”,temp);
else
printf(”\n%d is not a palindrome”,temp);
getch();
}
{
int num,r,sum=0,temp;
clrscr();
printf(”\nEnter a number:”);
scanf(”%d”,&num);
temp=num;
while(num)
{
r=num%10;
num=num/10;
sum=sum*10+r;
}
if(temp==sum)
printf(”\n%d is a palindrome”,temp);
else
printf(”\n%d is not a palindrome”,temp);
getch();
}
Write c program to find g.c.d of two number
void main()
{
int n1,n2;
clrscr();
printf(”\nEnter two numbers:”);
scanf(”%d %d”,&n1,&n2);
while(n1!=n2)
{
if(n1>n2)
n1=n1-n2;
else
n2=n2-n1;
}
printf(”\nGCD=%d”,n1);
getch();
}
{
int n1,n2;
clrscr();
printf(”\nEnter two numbers:”);
scanf(”%d %d”,&n1,&n2);
while(n1!=n2)
{
if(n1>n2)
n1=n1-n2;
else
n2=n2-n1;
}
printf(”\nGCD=%d”,n1);
getch();
}
WRITE A C PROGRAM TO FIND GENERIC ROOT OF A NUMBER.
void main()
{
long int num,sum,r;
clrscr();
printf(”\nEnter a number:-”);
scanf(”%ld”,&num);
while(num>10)
{
sum=0;
while(num)
{
r=num%10;
num=num/10;
sum+=r;
}
if(sum>10)
num=sum;
else
break;
}
printf(”\nSum of the digits in single digit is: %ld”,sum);
getch();
}
{
long int num,sum,r;
clrscr();
printf(”\nEnter a number:-”);
scanf(”%ld”,&num);
while(num>10)
{
sum=0;
while(num)
{
r=num%10;
num=num/10;
sum+=r;
}
if(sum>10)
num=sum;
else
break;
}
printf(”\nSum of the digits in single digit is: %ld”,sum);
getch();
}
WRITE A C PROGRAM FOR INSERT AN ELMENT IN AN ARRAY AT DESIRED POSITION
void main()
{
int a[50],size,num,i,pos,temp;
clrscr();
printf(”\nEnter size of the array: “);
scanf(”%d”,&size);
printf(”\nEnter %d elements in to the array: “,size);
for(i=0;iscanf(”%d”,&a[i]);
printf(”\nEnter position and number to insert: “);
scanf(”%d %d”,&pos,&num);
i=0;
while(i!=pos-1)
i++;
temp=size++;
while(i{
a[temp]=a[temp-1];
temp–;
}
a[i]=num;
for(i=0;iprintf(” %d”,a[i]);
getch();
}
{
int a[50],size,num,i,pos,temp;
clrscr();
printf(”\nEnter size of the array: “);
scanf(”%d”,&size);
printf(”\nEnter %d elements in to the array: “,size);
for(i=0;iscanf(”%d”,&a[i]);
printf(”\nEnter position and number to insert: “);
scanf(”%d %d”,&pos,&num);
i=0;
while(i!=pos-1)
i++;
temp=size++;
while(i{
a[temp]=a[temp-1];
temp–;
}
a[i]=num;
for(i=0;iprintf(” %d”,a[i]);
getch();
}
WRITE A C PROGRAM FOR GREATEST AMONG 3 NUMBERS USING CONDITIONAL OPERATOR
void main()
{
int a,b,c,big;
clrscr();
printf(”\nEnter 3 numbers:”);
scanf(”%d %d %d”,&a,&b,&c);
big=(a>b&&a>c?a:b>c?b:c);
printf(”\nThe biggest number is:%d”,big);
getch();
}
{
int a,b,c,big;
clrscr();
printf(”\nEnter 3 numbers:”);
scanf(”%d %d %d”,&a,&b,&c);
big=(a>b&&a>c?a:b>c?b:c);
printf(”\nThe biggest number is:%d”,big);
getch();
}
c PROGRAM FOR DELETE ELEMENT FROM AN ARRAY AT DESIRED POSITION
void main()
{
int a[50],i,pos,size;
clrscr();
printf(”\nEnter size of the array: “);
scanf(”%d”,&size);
printf(”\nEnter %d elements in to the array: “,size);
for(i=0;i
scanf(”%d”,&a[i]);
printf(”\nEnter position where to delete: “);
scanf(”%d”,&pos);
i=0;
while(i!=pos-1)
i++;
while(i<10)
{
a[i]=a[i+1];
i++;
}
size–;
for(i=0;i
printf(” %d”,a[i]);
getch();
}
{
int a[50],i,pos,size;
clrscr();
printf(”\nEnter size of the array: “);
scanf(”%d”,&size);
printf(”\nEnter %d elements in to the array: “,size);
for(i=0;i
scanf(”%d”,&a[i]);
printf(”\nEnter position where to delete: “);
scanf(”%d”,&pos);
i=0;
while(i!=pos-1)
i++;
while(i<10)
{
a[i]=a[i+1];
i++;
}
size–;
for(i=0;i
printf(” %d”,a[i]);
getch();
}
C PROGRAM FOR QUICK SORT
void main()
{
int x[20],size,i;
clrscr();
printf(”\nEnter size of the array :”);
scanf(”%d”,&size);
printf(”\nEnter %d elements :”,size);
for(i=0;ix[pivot])
j–;
if(i {
temp=x[i];
x[i]=x[j];
x[j]=temp;
}
}
temp=x[pivot];
x[pivot]=x[j];
x[j]=temp;
quicksort(x,first,j-1);
quicksort(x,j+1,last);
}
}
{
int x[20],size,i;
clrscr();
printf(”\nEnter size of the array :”);
scanf(”%d”,&size);
printf(”\nEnter %d elements :”,size);
for(i=0;ix[pivot])
j–;
if(i {
temp=x[i];
x[i]=x[j];
x[j]=temp;
}
}
temp=x[pivot];
x[pivot]=x[j];
x[j]=temp;
quicksort(x,first,j-1);
quicksort(x,j+1,last);
}
}
C PROGRAM FOR CONVERSION FROM DECIMAL TO OCTAL
void main()
{
int i=0,j=0,rem=0,a[10],b[10];
long int num;
clrscr();
printf(”\nEnter a number :”);
scanf(”%ld”,&num);
while(num)
{
if(num<
{
a[j++]=num;
break;
}
else
{
a[j++]=num%8;
num=num/8;
}
}
for(i=j-1;i>=0;i–)
b[rem++]=a[i];
printf(”\nOctal equivalent :”);
for(j=0;j
printf(”%d”,b[j]);
getch();
}
{
int i=0,j=0,rem=0,a[10],b[10];
long int num;
clrscr();
printf(”\nEnter a number :”);
scanf(”%ld”,&num);
while(num)
{
if(num<
{
a[j++]=num;
break;
}
else
{
a[j++]=num%8;
num=num/8;
}
}
for(i=j-1;i>=0;i–)
b[rem++]=a[i];
printf(”\nOctal equivalent :”);
for(j=0;j
printf(”%d”,b[j]);
getch();
}
C PROGRAM FOR REVERSE A NUMBER USING RECURSION
void main()
{
int num,rev;
clrscr();
printf(”\nEnter a number :”);
scanf(”%d”,&num);
rev=reverse(num);
printf(”\nAfter reverse the no is :%d”,rev);
getch();
}
int sum=0,r;
reverse(int num)
{
if(num)
{
r=num%10;
sum=sum*10+r;
reverse(num/10);
}
else
return sum;
}
{
int num,rev;
clrscr();
printf(”\nEnter a number :”);
scanf(”%d”,&num);
rev=reverse(num);
printf(”\nAfter reverse the no is :%d”,rev);
getch();
}
int sum=0,r;
reverse(int num)
{
if(num)
{
r=num%10;
sum=sum*10+r;
reverse(num/10);
}
else
return sum;
}
C PROGRAM FOR REMOVE DUPLICATE ELEMENTS IN AN ARRAY
void main()
{
int arr[50];
int *p;
int i,j,k,size,n;
clrscr();
printf(”\nEnter size of the array: “);
scanf(”%d”,&n);
printf(”\nEnter %d elements into the array: “,n);
for(i=0;i
scanf(”%d”,&arr[i]);
size=n;
p=arr;
for(i=0;i
{
for(j=0;j
{
if(i==j)
{
continue;
}
else if(*(p+i)==*(p+j))
{
k=j;
size–;
while(k < size)
{
*(p+k)=*(p+k+1);
k++;
}
j=0;
}
}
}
printf(”\nThe array after removing duplicates is: “);
for(i=0;i < size;i++)
{
printf(” %d”,arr[i]);
}
getch();
}
{
int arr[50];
int *p;
int i,j,k,size,n;
clrscr();
printf(”\nEnter size of the array: “);
scanf(”%d”,&n);
printf(”\nEnter %d elements into the array: “,n);
for(i=0;i
scanf(”%d”,&arr[i]);
size=n;
p=arr;
for(i=0;i
{
for(j=0;j
{
if(i==j)
{
continue;
}
else if(*(p+i)==*(p+j))
{
k=j;
size–;
while(k < size)
{
*(p+k)=*(p+k+1);
k++;
}
j=0;
}
}
}
printf(”\nThe array after removing duplicates is: “);
for(i=0;i < size;i++)
{
printf(” %d”,arr[i]);
}
getch();
}
C PROGRAM FOR BINARY SEARCH THROUGH RECURSSION
void main()
{
int a[10],i,n,m,c,l,u;
clrscr();
printf(”Enter the size of an array->”);
scanf(”%d”,&n);
printf(”\nEnter the elements of the array->”);
for(i=0;i
{
scanf(”%d”,&a[i]);
}
printf(”\nThe elements of an array are->”);
for(i=0;i
{
printf(” %d”,a[i]);
}
printf(”\nEnter the number to be search->”);
scanf(”%d”,&m);
l=0,u=n-1;
c=binary(a,n,m,l,u);
if(c==0)
printf(”\nThe number is not in the list”);
else
printf(”\nThe number is found”);
getch();
}
int binary(int a[],int n,int m,int l,int u)
{
int mid,c=0;
if(l<=u)
{
mid=(l+u)/2;
if(m==a[mid])
{
c=1;
}
else
if(m
{
return binary(a,n,m,l,mid-1);
}
else
return binary(a,n,m,mid+1,u);
}
else
return c;
}
{
int a[10],i,n,m,c,l,u;
clrscr();
printf(”Enter the size of an array->”);
scanf(”%d”,&n);
printf(”\nEnter the elements of the array->”);
for(i=0;i
{
scanf(”%d”,&a[i]);
}
printf(”\nThe elements of an array are->”);
for(i=0;i
{
printf(” %d”,a[i]);
}
printf(”\nEnter the number to be search->”);
scanf(”%d”,&m);
l=0,u=n-1;
c=binary(a,n,m,l,u);
if(c==0)
printf(”\nThe number is not in the list”);
else
printf(”\nThe number is found”);
getch();
}
int binary(int a[],int n,int m,int l,int u)
{
int mid,c=0;
if(l<=u)
{
mid=(l+u)/2;
if(m==a[mid])
{
c=1;
}
else
if(m
{
return binary(a,n,m,l,mid-1);
}
else
return binary(a,n,m,mid+1,u);
}
else
return c;
}
C PROGRAM FOR LINEAR SEARCH
void main()
{
int a[10],i,n,m,c=0;
clrscr();
printf(”Enter the size of an array->”);
scanf(”%d”,&n);
printf(”\nEnter the elements of the array->”);
for(i=0;i
{
scanf(”%d”,&a[i]);
}
printf(”\nThe elements of an array are->”);
for(i=0;i
{
printf(” %d”,a[i]);
}
printf(”\nEnter the number to be search->”);
scanf(”%d”,&m);
for(i=0;i
{
if(a[i]==m)
{
c=1;
break;
}
}
if(c==0)
printf(”\nThe number is not in the list”);
else
printf(”\nThe number is found”);
getch();
}
{
int a[10],i,n,m,c=0;
clrscr();
printf(”Enter the size of an array->”);
scanf(”%d”,&n);
printf(”\nEnter the elements of the array->”);
for(i=0;i
{
scanf(”%d”,&a[i]);
}
printf(”\nThe elements of an array are->”);
for(i=0;i
{
printf(” %d”,a[i]);
}
printf(”\nEnter the number to be search->”);
scanf(”%d”,&m);
for(i=0;i
{
if(a[i]==m)
{
c=1;
break;
}
}
if(c==0)
printf(”\nThe number is not in the list”);
else
printf(”\nThe number is found”);
getch();
}
ACROMATIC STRING(PRINTING INITIALS)
void main()
{
int i=0,j=0;
char *str1,*str2,*str3;
clrscr();
puts(”Enter first string”);
gets(str1);
puts(”Enter second string”);
gets(str2);
printf(”Before concatenation the strings are\n”);
puts(str1);
puts(str2);
while(*str1)
{
str3[i++]=*str1++;
}
while(*str2)
{
str3[i++]=*str2++;
}
str3[i]=”;
printf(”After concatenation the strings are\n”);
puts(str3);
getch();
}
{
int i=0,j=0;
char *str1,*str2,*str3;
clrscr();
puts(”Enter first string”);
gets(str1);
puts(”Enter second string”);
gets(str2);
printf(”Before concatenation the strings are\n”);
puts(str1);
puts(str2);
while(*str1)
{
str3[i++]=*str1++;
}
while(*str2)
{
str3[i++]=*str2++;
}
str3[i]=”;
printf(”After concatenation the strings are\n”);
puts(str3);
getch();
}
C PROGRAM FOR CONCATENATION OF TWO STRINGS USING POINTER
void main()
{
int i=0,j=0;
char *str1,*str2,*str3;
clrscr();
puts(”Enter first string”);
gets(str1);
puts(”Enter second string”);
gets(str2);
printf(”Before concatenation the strings are\n”);
puts(str1);
puts(str2);
while(*str1)
{
str3[i++]=*str1++;
}
while(*str2)
{
str3[i++]=*str2++;
}
str3[i]=”;
printf(”After concatenation the strings are\n”);
puts(str3);
getch();
}
{
int i=0,j=0;
char *str1,*str2,*str3;
clrscr();
puts(”Enter first string”);
gets(str1);
puts(”Enter second string”);
gets(str2);
printf(”Before concatenation the strings are\n”);
puts(str1);
puts(str2);
while(*str1)
{
str3[i++]=*str1++;
}
while(*str2)
{
str3[i++]=*str2++;
}
str3[i]=”;
printf(”After concatenation the strings are\n”);
puts(str3);
getch();
}
C PROGRAM FOR SWAPPING OF STRINGS
void main()
{
int i=0,j=0,k=0;
char str1[20],str2[20],temp[20];
clrscr();
puts(”Enter first string”);
gets(str1);
puts(”Enter second string”);
gets(str2);
printf(”Before swaping the strings are\n”);
puts(str1);
puts(str2);
while(str1[i]!=”)
{
temp[j++]=str1[i++];
}
temp[j]=”;
i=0,j=0;
while(str2[i]!=”)
{
str1[j++]=str2[i++];
}
str1[j]=”;
i=0,j=0;
while(temp[i]!=”)
{
str2[j++]=temp[i++];
}
str2[j]=”;
printf(”After swaping the strings are\n”);
puts(str1);
puts(str2);
getch();
}
{
int i=0,j=0,k=0;
char str1[20],str2[20],temp[20];
clrscr();
puts(”Enter first string”);
gets(str1);
puts(”Enter second string”);
gets(str2);
printf(”Before swaping the strings are\n”);
puts(str1);
puts(str2);
while(str1[i]!=”)
{
temp[j++]=str1[i++];
}
temp[j]=”;
i=0,j=0;
while(str2[i]!=”)
{
str1[j++]=str2[i++];
}
str1[j]=”;
i=0,j=0;
while(temp[i]!=”)
{
str2[j++]=temp[i++];
}
str2[j]=”;
printf(”After swaping the strings are\n”);
puts(str1);
puts(str2);
getch();
}
C PROGRAM FOR FIND POWER OF A NUMBER USING RECURSION
void main()
{
int pow,num;
long int res;
long int power(int,int);
clrscr();
printf(”\nEnter a number: “);
scanf(”%d”,&num);
printf(”\nEnter power: “);
scanf(”%d”,&pow);
res=power(num,pow);
printf(”\n%d to the power %d is: %ld”,num,pow,res);
getch();
}
int i=1;
long int sum=1;
long int power(int num,int pow)
{
if(i<=pow)
{
sum=sum*num;
power(num,pow-1);
}
else
return sum;
}
{
int pow,num;
long int res;
long int power(int,int);
clrscr();
printf(”\nEnter a number: “);
scanf(”%d”,&num);
printf(”\nEnter power: “);
scanf(”%d”,&pow);
res=power(num,pow);
printf(”\n%d to the power %d is: %ld”,num,pow,res);
getch();
}
int i=1;
long int sum=1;
long int power(int num,int pow)
{
if(i<=pow)
{
sum=sum*num;
power(num,pow-1);
}
else
return sum;
}
C PROGRAM FOR FIND POWER OF A NUMBER
void main()
{
int pow,num,i=1;
long int sum=1;
clrscr();
printf(”\nEnter a number: “);
scanf(”%d”,&num);
printf(”\nEnter power: “);
scanf(”%d”,&pow);
while(i<=pow)
{
sum=sum*num;
i++;
}
printf(”\n%d to the power %d is: %ld”,num,pow,sum);
getch();
}
{
int pow,num,i=1;
long int sum=1;
clrscr();
printf(”\nEnter a number: “);
scanf(”%d”,&num);
printf(”\nEnter power: “);
scanf(”%d”,&pow);
while(i<=pow)
{
sum=sum*num;
i++;
}
printf(”\n%d to the power %d is: %ld”,num,pow,sum);
getch();
}
C PROGRAM TO FIND SUM OF DIGITS OF A NUMBER USING RECURSION
void main()
{
int num,x;
clrscr();
printf(”\nEnter a number: “);
scanf(”%d”,&num);
x=findsum(num);
printf(”Sum of the digits of %d is: %d”,num,x);
getch();
}
int r,s;
int findsum(int n)
{
if(n)
{
r=n%10;
s=s+r;
findsum(n/10);
}
else
return s;
}
{
int num,x;
clrscr();
printf(”\nEnter a number: “);
scanf(”%d”,&num);
x=findsum(num);
printf(”Sum of the digits of %d is: %d”,num,x);
getch();
}
int r,s;
int findsum(int n)
{
if(n)
{
r=n%10;
s=s+r;
findsum(n/10);
}
else
return s;
}
C PROGRAM TO FIND GCD OF A NUMBER USING RECURSION
void main()
{
int n1,n2,gcd;
clrscr();
printf(”\nEnter two numbers: “);
scanf(”%d %d”,&n1,&n2);
gcd=findgcd(n1,n2);
printf(”\nGCD of %d and %d is: %d”,n1,n2,gcd);
getch();
}
int findgcd(int x,int y)
{
while(x!=y)
{
if(x>y)
return findgcd(x-y,y);
else
return findgcd(x,y-x);
}
return x;
}
{
int n1,n2,gcd;
clrscr();
printf(”\nEnter two numbers: “);
scanf(”%d %d”,&n1,&n2);
gcd=findgcd(n1,n2);
printf(”\nGCD of %d and %d is: %d”,n1,n2,gcd);
getch();
}
int findgcd(int x,int y)
{
while(x!=y)
{
if(x>y)
return findgcd(x-y,y);
else
return findgcd(x,y-x);
}
return x;
}
C PROGRAM TO FIND FACTORIAL OF A NUMBER USING RECURSION
void main()
{
int num,f;
clrscr();
printf(”\nEnter a number: “);
scanf(”%d”,&num);
f=fact(num);
printf(”\nFactorial of %d is: %d”,num,f);
getch();
}
int fact(int n)
{
if(n==1)
return 1;
else
return(n*fact(n-1));
}
{
int num,f;
clrscr();
printf(”\nEnter a number: “);
scanf(”%d”,&num);
f=fact(num);
printf(”\nFactorial of %d is: %d”,num,f);
getch();
}
int fact(int n)
{
if(n==1)
return 1;
else
return(n*fact(n-1));
}
C PROGRAM FOR DISPLAY SOURCE CODE AS OUTPUT
#include”stdio.h”
void main()
{
FILE *p;
char ch;
clrscr();
p=fopen(”raja.c”,”r”);
while((ch=getc(p))!=-1)
putchar(ch);
fclose(p);
getch();
}
void main()
{
FILE *p;
char ch;
clrscr();
p=fopen(”raja.c”,”r”);
while((ch=getc(p))!=-1)
putchar(ch);
fclose(p);
getch();
}
C PROGRAM FOR INSERTION SORT
void main()
{
int i,j,s,temp,a[20];
clrscr();
printf(”\nEnter size of the array: “);
scanf(”%d”,&s);
printf(”\nEnter %d elements in to the array:”,s);
for(i=0;i
scanf(”%d”,&a[i]);
for(i=1;i
{
temp=a[i];
j=i-1;
while((temp=0))
{
a[j+1]=a[j];
j=j-1;
}
a[j+1]=temp;
}
printf(”\nAfter sorting the elements are: “);
for(i=0;i
printf(” %d”,a[i]);
getch();
}
{
int i,j,s,temp,a[20];
clrscr();
printf(”\nEnter size of the array: “);
scanf(”%d”,&s);
printf(”\nEnter %d elements in to the array:”,s);
for(i=0;i
scanf(”%d”,&a[i]);
for(i=1;i
{
temp=a[i];
j=i-1;
while((temp=0))
{
a[j+1]=a[j];
j=j-1;
}
a[j+1]=temp;
}
printf(”\nAfter sorting the elements are: “);
for(i=0;i
printf(” %d”,a[i]);
getch();
}
C PROGRAM FOR INSERTION SORT
void main()
{
int s,i,j,temp,a[20];
clrscr();
printf(”\nEnter size of the array :”);
scanf(”%d”,&s);
printf(”\nEnter %d elements in to the array:”);
for(i=0;i
scanf(”%d”,&a[i]);
for(i=0;i
{
for(j=i+1;j
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf(”\nThe array after sorting is: “);
for(i=0;i
printf(” %d”,a[i]);
getch();
}
{
int s,i,j,temp,a[20];
clrscr();
printf(”\nEnter size of the array :”);
scanf(”%d”,&s);
printf(”\nEnter %d elements in to the array:”);
for(i=0;i
scanf(”%d”,&a[i]);
for(i=0;i
{
for(j=i+1;j
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf(”\nThe array after sorting is: “);
for(i=0;i
printf(” %d”,a[i]);
getch();
}
C PROGRAM FOR SELECTION SORT
void main()
{
int s,i,j,temp,a[20];
clrscr();
printf(”\nEnter size of the array :”);
scanf(”%d”,&s);
printf(”\nEnter %d elements in to the array:”);
for(i=0;i
scanf(”%d”,&a[i]);
for(i=0;i
{
for(j=i+1;j
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf(”\nThe array after sorting is: “);
for(i=0;i
printf(” %d”,a[i]);
getch();
}
{
int s,i,j,temp,a[20];
clrscr();
printf(”\nEnter size of the array :”);
scanf(”%d”,&s);
printf(”\nEnter %d elements in to the array:”);
for(i=0;i
scanf(”%d”,&a[i]);
for(i=0;i
{
for(j=i+1;j
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf(”\nThe array after sorting is: “);
for(i=0;i
printf(” %d”,a[i]);
getch();
}
C PROGRAM FOR BUBBLE SORT
void main()
{
int s,temp,i,j,a[20];
clrscr();
printf(”\nEnter size of the array: “);
scanf(”%d”,&s);
printf(”\nEnter %d elements in to the array:”,s);
for(i=0;i
scanf(”%d”,&a[i]);
for(i=0;i
{
for(j=0;j
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf(”\nThe array after sorting is: “);
for(i=0;i
printf(” %d”,a[i]);
getch();
}
{
int s,temp,i,j,a[20];
clrscr();
printf(”\nEnter size of the array: “);
scanf(”%d”,&s);
printf(”\nEnter %d elements in to the array:”,s);
for(i=0;i
scanf(”%d”,&a[i]);
for(i=0;i
{
for(j=0;j
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf(”\nThe array after sorting is: “);
for(i=0;i
printf(” %d”,a[i]);
getch();
}
C PROGRAM FOR SORTING OF STRING
void main()
{
int i,j,n;
char str[20][20],temp[20];
clrscr();
puts(”Enter the no. of string to be sorted”);
scanf(”%d”,&n);
for(i=0;i<=n;i++)
gets(str[i]);
for(i=0;i<=n;i++)
for(j=i+1;j<=n;j++)
{
if(strcmp(str[i],str[j])>0)
{
strcpy(temp,str[i]);
strcpy(str[i],str[j]);
strcpy(str[j],temp);
}
}
printf(”The sorted string\n”);
for(i=0;i<=n;i++)
puts(str[i]);
getch();
}
{
int i,j,n;
char str[20][20],temp[20];
clrscr();
puts(”Enter the no. of string to be sorted”);
scanf(”%d”,&n);
for(i=0;i<=n;i++)
gets(str[i]);
for(i=0;i<=n;i++)
for(j=i+1;j<=n;j++)
{
if(strcmp(str[i],str[j])>0)
{
strcpy(temp,str[i]);
strcpy(str[i],str[j]);
strcpy(str[j],temp);
}
}
printf(”The sorted string\n”);
for(i=0;i<=n;i++)
puts(str[i]);
getch();
}
C PROGRAM FOR COUNTING DIFFERENT CHARACTERS IN A STRING
main()
{
int a[26],A[26],i,c=0;
char str[100];
clrscr();
puts(”Enter a string->”);
gets(str);
for(i=0;i<26;i++)
{
a[i]=0;
A[i]=0;
}
for(i=0;str[i]!=”;i++)
{
c=str[i];
if(c<97)
{
c=c-65;
A[c]++;
}
else
{
c=c-97;
a[c]++;
}
}
for(i=0;i<26;i++)
{
if(a[i]!=0)
printf(”\n%c occurs %d times”,i+97,a[i]);
}
for(i=0;i<26;i++)
{
if(A[i]!=0)
printf(”\n%c occurs %d times”,i+97,A[i]);
}
getch();
}
{
int a[26],A[26],i,c=0;
char str[100];
clrscr();
puts(”Enter a string->”);
gets(str);
for(i=0;i<26;i++)
{
a[i]=0;
A[i]=0;
}
for(i=0;str[i]!=”;i++)
{
c=str[i];
if(c<97)
{
c=c-65;
A[c]++;
}
else
{
c=c-97;
a[c]++;
}
}
for(i=0;i<26;i++)
{
if(a[i]!=0)
printf(”\n%c occurs %d times”,i+97,a[i]);
}
for(i=0;i<26;i++)
{
if(A[i]!=0)
printf(”\n%c occurs %d times”,i+97,A[i]);
}
getch();
}
C PROGRAM FOR CONVERSION OF FAREHNITE TO CENTIGRADE
void main()
{
float c,f;
clrscr();
printf(”Enter temp. in farehnite”);
scanf(”%f”,&f);
c=(5*(f-32))/9;//Formula for conversion
printf(”The temp. in centigrade is->%f”,c);
getch();
}
{
float c,f;
clrscr();
printf(”Enter temp. in farehnite”);
scanf(”%f”,&f);
c=(5*(f-32))/9;//Formula for conversion
printf(”The temp. in centigrade is->%f”,c);
getch();
}
Write a c program to find the perfect number
void main(){int n,i=1,sum=0;
clrscr();
printf(”\nEnter a number:-”);
scanf(”%d”,&n);
while(i
{
if(n%i==0)
sum=sum+i;
i++;
}
if(sum==n)
printf(”\nThe no %d is a perfect number”,i);
else
printf(”\nThe no %d is not a perfect number”,i);
getch();
}
WRITE A C PROGRAM TO FIND TRASPOSE OF A MATRIX
void main()
{
int a[10][10],b[10][10],i,j,k=0,m,n;
clrscr();
printf(”\nEnter the row and column of matrix”);
scanf(”%d %d”,&m,&n);
printf(”\nEnter the First matrix->”);
for(i=0;i
for(j=0;j
scanf(”%d”,&a[i][j]);
printf(”\nThe matrix is\n”);
for(i=0;i
{
printf(”\n”);
for(j=0;j
{
printf(”%d\t”,a[i][j]);
}
}
for(i=0;i
for(j=0;j
b[i][j]=0;
for(i=0;i
{
for(j=0;j
{
b[i][j]=a[j][i];
printf(”\n%d”,b[i][j]);
}
}
printf(”\n\nTraspose of a matrix is -> “);
for(i=0;i
{
printf(”\n”);
for(j=0;j
{
printf(”%d\t”,b[i][j]);
}
}
getch();
}
{
int a[10][10],b[10][10],i,j,k=0,m,n;
clrscr();
printf(”\nEnter the row and column of matrix”);
scanf(”%d %d”,&m,&n);
printf(”\nEnter the First matrix->”);
for(i=0;i
for(j=0;j
scanf(”%d”,&a[i][j]);
printf(”\nThe matrix is\n”);
for(i=0;i
{
printf(”\n”);
for(j=0;j
{
printf(”%d\t”,a[i][j]);
}
}
for(i=0;i
for(j=0;j
b[i][j]=0;
for(i=0;i
{
for(j=0;j
{
b[i][j]=a[j][i];
printf(”\n%d”,b[i][j]);
}
}
printf(”\n\nTraspose of a matrix is -> “);
for(i=0;i
{
printf(”\n”);
for(j=0;j
{
printf(”%d\t”,b[i][j]);
}
}
getch();
}
Write a c program to find SUM OF DIAGONAL ELEMENTS OF A MATRIX
void main()
{
int a[10][10],i,j,sum=0,m,n;
clrscr();
printf(”\nEnter the row and column of matrix”);
scanf(”%d %d”,&m,&n);
printf(”\nEnter the First matrix->”);
for(i=0;i < m;i++)
for(j=0;j < n;j++)
scanf(”%d”,&a[i][j]);
printf(”\nThe matrix is\n”);
for(i=0;i
{
printf(”\n”);
for(j=0;j
{
printf(”%d\t”,a[i][j]);
}
}
for(i=0;i
{
for(j=0;j
{
if(i==j)
sum=sum+a[i][j];
}
}
printf(”\n\nSum of the diagonal elements of a matrix is -> “);
printf(”%d”,sum);
getch();
}
{
int a[10][10],i,j,sum=0,m,n;
clrscr();
printf(”\nEnter the row and column of matrix”);
scanf(”%d %d”,&m,&n);
printf(”\nEnter the First matrix->”);
for(i=0;i < m;i++)
for(j=0;j < n;j++)
scanf(”%d”,&a[i][j]);
printf(”\nThe matrix is\n”);
for(i=0;i
{
printf(”\n”);
for(j=0;j
{
printf(”%d\t”,a[i][j]);
}
}
for(i=0;i
{
for(j=0;j
{
if(i==j)
sum=sum+a[i][j];
}
}
printf(”\n\nSum of the diagonal elements of a matrix is -> “);
printf(”%d”,sum);
getch();
}
WRITE A C PROGRAM FOR MULTIPLICATION OF TWO MATRICES
void main()
{
int a[5][5],b[5][5],c[5][5],i,j,k,sum=0,m,n,o,p;
clrscr();
printf(”\nEnter the row and column of first matrix”);
scanf(”%d %d”,&m,&n);
printf(”\nEnter the row and column of second matrix”);
scanf(”%d %d”,&o,&p);
if(n!=o)
{
printf(”Matrix mutiplication is not possible”);
printf(”\nColumn of first matrix must be same as row of second matrix”);
}
else
{
printf(”\nEnter the First matrix->”);
for(i=0;i
for(j=0;j
scanf(”%d”,&a[i][j]);
printf(”\nEnter the Second matrix->”);
for(i=0;i
for(j=0;j
scanf(”%d”,&b[i][j]);
printf(”\nThe First matrix is\n”);
for(i=0;i
{
printf(”\n”);
for(j=0;j
{
printf(”%d\t”,a[i][j]);
}
}
printf(”\nThe Second matrix is\n”);
for(i=0;i
{
printf(”\n”);
for(j=0;j
{
printf(”%d\t”,b[i][j]);
} }
for(i=0;i
for(j=0;j
c[i][j]=0;
for(i=0;i
{
for(j=0;j
{
sum=0;
for(k=0;k
sum=sum+a[i][k]*b[k][j];
c[i][j]=sum;
}
}
}
printf(”\nThe multiplication of two matrix is\n”);
for(i=0;i
{
printf(”\n”);
for(j=0;j
{
printf(”%d\t”,c[i][j]);
}
}
getch();
}
{
int a[5][5],b[5][5],c[5][5],i,j,k,sum=0,m,n,o,p;
clrscr();
printf(”\nEnter the row and column of first matrix”);
scanf(”%d %d”,&m,&n);
printf(”\nEnter the row and column of second matrix”);
scanf(”%d %d”,&o,&p);
if(n!=o)
{
printf(”Matrix mutiplication is not possible”);
printf(”\nColumn of first matrix must be same as row of second matrix”);
}
else
{
printf(”\nEnter the First matrix->”);
for(i=0;i
for(j=0;j
scanf(”%d”,&a[i][j]);
printf(”\nEnter the Second matrix->”);
for(i=0;i
for(j=0;j
scanf(”%d”,&b[i][j]);
printf(”\nThe First matrix is\n”);
for(i=0;i
{
printf(”\n”);
for(j=0;j
{
printf(”%d\t”,a[i][j]);
}
}
printf(”\nThe Second matrix is\n”);
for(i=0;i
{
printf(”\n”);
for(j=0;j
{
printf(”%d\t”,b[i][j]);
} }
for(i=0;i
for(j=0;j
c[i][j]=0;
for(i=0;i
{
for(j=0;j
{
sum=0;
for(k=0;k
sum=sum+a[i][k]*b[k][j];
c[i][j]=sum;
}
}
}
printf(”\nThe multiplication of two matrix is\n”);
for(i=0;i
{
printf(”\n”);
for(j=0;j
{
printf(”%d\t”,c[i][j]);
}
}
getch();
}
WRITE A C PROGRAM TO FIND SECOND LARGEST NUMBER IN AN UNSORTED ARRAY
main()
{
int un[10], i, big1, big2;
printf(”Enter array elements: “);
for ( i = 0; i < 10; ++i )
scanf(”%d”, &un[i]);
big1 = un[0];
for ( i = 1; i < 10; ++i )
{
if ( big1 < un[i] )
big1 = un[i];
if ( big1 != un[0] )
big2 = un[0];
else
big2 = un[1];
}
for ( i = 1; i < 10; ++i )
{
if ( big1 != un[i] && big2 < un[i] )
big2 = un[i];
}
printf(”Second largest: %d\n”, big2);
return 0;
}
{
int un[10], i, big1, big2;
printf(”Enter array elements: “);
for ( i = 0; i < 10; ++i )
scanf(”%d”, &un[i]);
big1 = un[0];
for ( i = 1; i < 10; ++i )
{
if ( big1 < un[i] )
big1 = un[i];
if ( big1 != un[0] )
big2 = un[0];
else
big2 = un[1];
}
for ( i = 1; i < 10; ++i )
{
if ( big1 != un[i] && big2 < un[i] )
big2 = un[i];
}
printf(”Second largest: %d\n”, big2);
return 0;
}
WRITE C PROGRAM TO FIND SUM OF THE SERIES 1+2+3+———+n
void main()
{
int r;
clrscr();
printf(”\nEnter the number range: “);
scanf(”%d”,&r);
printf(”\nSum of the series is: %d”,(r*(r+1))/2);
getch();
}
{
int r;
clrscr();
printf(”\nEnter the number range: “);
scanf(”%d”,&r);
printf(”\nSum of the series is: %d”,(r*(r+1))/2);
getch();
}
WRITE A C PROGRAM TO FIND LARGEST NUMBER IN AN ARRAY
void main()
{
int a[50],size,i,big;
clrscr();
printf(”\nEnter the size of the array: “);
scanf(”%d”,&size);
printf(”\nEnter %d elements in to the array: ” ,size);
for(i=0;i
scanf(”%d”,&a[i]);
big=a[0];
for(i=1;i
{
if(big
big=a[i];
}
printf(”\nBiggest: %d”,big);
getch();
}
{
int a[50],size,i,big;
clrscr();
printf(”\nEnter the size of the array: “);
scanf(”%d”,&size);
printf(”\nEnter %d elements in to the array: ” ,size);
for(i=0;i
scanf(”%d”,&a[i]);
big=a[0];
for(i=1;i
{
if(big
big=a[i];
}
printf(”\nBiggest: %d”,big);
getch();
}
WRITE C PROGRAM TO FIND ADDITION & SUBTRACTION OF TWO COMPLEX NUMBERS
void main()
{
int a,b,c,d,x,y;
clrscr();
printf(”\nEnter the first complex number:”);
scanf(”%d%d”,&a,&b);
printf(”\nEnter the second complex number:”);
scanf(”%d%d”,&c,&d);
if(b<0)
printf(”%d-i\n”,a-b);
else
printf(”d+i\n”,a+b);
if(d<0)
printf(”d-i\n”,c-d);
else
printf(”%d+i\n”,c+d);
printf(”\nADDITION “);
x=a+c;
y=b+d;
if(y>0)
printf(”%d-i%d”,x,-y);
else
printf(”%d+i%d”,x,+y);
printf(”\n\nSUBTRACTION “);
x=a-c;
y=b-d;
if(y<0)
printf(”%d-i%d”,x,-y);
else
printf(”%d+i%d”,x,+y);
getch();
}
{
int a,b,c,d,x,y;
clrscr();
printf(”\nEnter the first complex number:”);
scanf(”%d%d”,&a,&b);
printf(”\nEnter the second complex number:”);
scanf(”%d%d”,&c,&d);
if(b<0)
printf(”%d-i\n”,a-b);
else
printf(”d+i\n”,a+b);
if(d<0)
printf(”d-i\n”,c-d);
else
printf(”%d+i\n”,c+d);
printf(”\nADDITION “);
x=a+c;
y=b+d;
if(y>0)
printf(”%d-i%d”,x,-y);
else
printf(”%d+i%d”,x,+y);
printf(”\n\nSUBTRACTION “);
x=a-c;
y=b-d;
if(y<0)
printf(”%d-i%d”,x,-y);
else
printf(”%d+i%d”,x,+y);
getch();
}
WRITE A C PROGRAM TO FIND COPY DATA FROM ONE FILE TO ANOTHER FILE
#include”stdio.h”
void main()
{
FILE *p,*q;
char file1[20],file2[20];
char ch;
clrscr();
printf(”\nEnter the source file name to be copied:”);
gets(file1);
p=fopen(file1,”r”);
if(p==NULL)
{
printf(”cannot open %s”,file1);
exit(0);
}
printf(”\nEnter the destination file name:”);
gets(file2);
q=fopen(file2,”w”);
if(q==NULL)
{
printf(”cannot open %s”,file2);
exit(0);
}
while((ch=getc(p))!=EOF)
putc(ch,q);
printf(”\nCOMPLETED”);
fclose(p);
fclose(q);
getch();
}
void main()
{
FILE *p,*q;
char file1[20],file2[20];
char ch;
clrscr();
printf(”\nEnter the source file name to be copied:”);
gets(file1);
p=fopen(file1,”r”);
if(p==NULL)
{
printf(”cannot open %s”,file1);
exit(0);
}
printf(”\nEnter the destination file name:”);
gets(file2);
q=fopen(file2,”w”);
if(q==NULL)
{
printf(”cannot open %s”,file2);
exit(0);
}
while((ch=getc(p))!=EOF)
putc(ch,q);
printf(”\nCOMPLETED”);
fclose(p);
fclose(q);
getch();
}
WRITE A C PROGRAM FOR STRING PALINDROME
#include”string.h”
void main()
{
char *str,*rev;
int i,j;
clrscr();
printf(”\nEnter a string:”);
scanf(”%s”,str);
for(i=strlen(str)-1,j=0;i>=0;i–,j++)
rev[j]=str[i];
rev[j]=”;
if(strcmp(rev,str))
printf(”\nThe string is not a palindrome”);
else
printf(”\nThe string is a palindrome”);
getch();
}
void main()
{
char *str,*rev;
int i,j;
clrscr();
printf(”\nEnter a string:”);
scanf(”%s”,str);
for(i=strlen(str)-1,j=0;i>=0;i–,j++)
rev[j]=str[i];
rev[j]=”;
if(strcmp(rev,str))
printf(”\nThe string is not a palindrome”);
else
printf(”\nThe string is a palindrome”);
getch();
}
WRITE A C PROGRAM TO DELETE THE VOWELS FROM A STRING
void main()
{
char str[20],s[20];
int i,j=0;
clrscr();
printf(”Enter any string->”);
scanf(”%s”,str);
printf(”The string is->%s”,str);
for(i=0;i<=strlen(str);i++)
{
if(str[i]==’a’str[i]==’e’str[i]==’i’str[i]==’o’str[i]==’u')
str[i]=’ ‘;
else
s[j++]=str[i];
}
s[j]=”;
printf(”\nThe string without vowel is->%s”,s);
getch();
}
{
char str[20],s[20];
int i,j=0;
clrscr();
printf(”Enter any string->”);
scanf(”%s”,str);
printf(”The string is->%s”,str);
for(i=0;i<=strlen(str);i++)
{
if(str[i]==’a’str[i]==’e’str[i]==’i’str[i]==’o’str[i]==’u')
str[i]=’ ‘;
else
s[j++]=str[i];
}
s[j]=”;
printf(”\nThe string without vowel is->%s”,s);
getch();
}
WRITE A C PROGRAM TO CONVERSION FROM LOWER CASE TO UPPER CASE
void main()
{
char str[20];
int i;
clrscr();
printf(”Enter any string->”);
scanf(”%s”,str);
printf(”The string is->%s”,str);
for(i=0;i<=strlen(str);i++)
{
if(str[i]>=97&&str[i]<=122)
str[i]=str[i]-32;
}
printf(”\nThe string in lowercase is->%s”,str);
getch();
}
{
char str[20];
int i;
clrscr();
printf(”Enter any string->”);
scanf(”%s”,str);
printf(”The string is->%s”,str);
for(i=0;i<=strlen(str);i++)
{
if(str[i]>=97&&str[i]<=122)
str[i]=str[i]-32;
}
printf(”\nThe string in lowercase is->%s”,str);
getch();
}
WRITE A C PROGRAM FOR ADDITION OF TWO MATRICES
void main()
{
int a[3][3],b[3][3],c[3][3],i,j;
clrscr();
printf(”Enter the First matrix->”);
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf(”%d”,&a[i][j]);
printf(”\nEnter the Second matrix->”);
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf(”%d”,&b[i][j]);
printf(”\nThe First matrix is\n”);
for(i=0;i<3;i++)
{
printf(”\n”);
for(j=0;j<3;j++)
printf(”%d\t”,a[i][j]);
}
printf(”\nThe Second matrix is\n”);
for(i=0;i<3;i++)
{
printf(”\n”);
for(j=0;j<3;j++)
printf(”%d\t”,b[i][j]);
}
for(i=0;i<3;i++)
for(j=0;j<3;j++)
c[i][j]=a[i][j]+b[i][j];
printf(”\nThe Addition of two matrix is\n”);
for(i=0;i<3;i++)
{
printf(”\n”);
for(j=0;j<3;j++)
printf(”%d\t”,c[i][j]);
}
getch();
}
{
int a[3][3],b[3][3],c[3][3],i,j;
clrscr();
printf(”Enter the First matrix->”);
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf(”%d”,&a[i][j]);
printf(”\nEnter the Second matrix->”);
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf(”%d”,&b[i][j]);
printf(”\nThe First matrix is\n”);
for(i=0;i<3;i++)
{
printf(”\n”);
for(j=0;j<3;j++)
printf(”%d\t”,a[i][j]);
}
printf(”\nThe Second matrix is\n”);
for(i=0;i<3;i++)
{
printf(”\n”);
for(j=0;j<3;j++)
printf(”%d\t”,b[i][j]);
}
for(i=0;i<3;i++)
for(j=0;j<3;j++)
c[i][j]=a[i][j]+b[i][j];
printf(”\nThe Addition of two matrix is\n”);
for(i=0;i<3;i++)
{
printf(”\n”);
for(j=0;j<3;j++)
printf(”%d\t”,c[i][j]);
}
getch();
}
WRITE A C PROGRAM TO CONVERSION FROM UPPERCASE TO LOWER CASE
void main()
{
char str[20];
int i;
clrscr();
printf(”Enter any string->”);
scanf(”%s”,str);
printf(”The string is->%s”,str);
for(i=0;i<=strlen(str);i++)
{
if(str[i]>=65&&str[i]<=90)
str[i]=str[i]+32;
}
printf(”\nThe string in uppercase is->%s”,str);
getch();
}
{
char str[20];
int i;
clrscr();
printf(”Enter any string->”);
scanf(”%s”,str);
printf(”The string is->%s”,str);
for(i=0;i<=strlen(str);i++)
{
if(str[i]>=65&&str[i]<=90)
str[i]=str[i]+32;
}
printf(”\nThe string in uppercase is->%s”,str);
getch();
}
WRITE A C PROGRAM FOR SWAPING OF TWO ARRAYS
void main()
{
int a[10],b[10],c[10],i;
clrscr();
printf(”Enter First array->”);
for(i=0;i<10;i++)
scanf(”%d”,&a[i]);
printf(”\nEnter Second array->”);
for(i=0;i<10;i++)
scanf(”%d”,&b[i]);
printf(”Arrays before swapping”);
printf(”\nFirst array->”);
for(i=0;i<10;i++)
{
printf(”%d”,a[i]);
}
printf(”\nSecond array->”);
for(i=0;i<10;i++)
{
printf(”%d”,b[i]);
}
for(i=0;i<10;i++)
{
//write any swapping technique
c[i]=a[i];
a[i]=b[i];
b[i]=c[i];
}
printf(”\nArrays after swapping”);
printf(”\nFirst array->”);
for(i=0;i<10;i++)
{
printf(”%d”,a[i]);
}
printf(”\nSecond array->”);
for(i=0;i<10;i++)
{
printf(”%d”,b[i]);
}
getch();
}
{
int a[10],b[10],c[10],i;
clrscr();
printf(”Enter First array->”);
for(i=0;i<10;i++)
scanf(”%d”,&a[i]);
printf(”\nEnter Second array->”);
for(i=0;i<10;i++)
scanf(”%d”,&b[i]);
printf(”Arrays before swapping”);
printf(”\nFirst array->”);
for(i=0;i<10;i++)
{
printf(”%d”,a[i]);
}
printf(”\nSecond array->”);
for(i=0;i<10;i++)
{
printf(”%d”,b[i]);
}
for(i=0;i<10;i++)
{
//write any swapping technique
c[i]=a[i];
a[i]=b[i];
b[i]=c[i];
}
printf(”\nArrays after swapping”);
printf(”\nFirst array->”);
for(i=0;i<10;i++)
{
printf(”%d”,a[i]);
}
printf(”\nSecond array->”);
for(i=0;i<10;i++)
{
printf(”%d”,b[i]);
}
getch();
}
WRITE A C PROGRAM FOR CONVERSION OF BINARY TO DECIMAL
void main()
{
long int no,n=0,j=1,rem,no1;
clrscr();
printf(”Enter any number any binary form->”);
scanf(”%ld”,&no);
no1=no;
while(no!=0)
{
rem=no%10;
n=n+rem*j;
j=j*2;
no=no/10;
}
printf(”\nThe value of binary no. %ld is ->%ld”,no1,n);
getch();
}
{
long int no,n=0,j=1,rem,no1;
clrscr();
printf(”Enter any number any binary form->”);
scanf(”%ld”,&no);
no1=no;
while(no!=0)
{
rem=no%10;
n=n+rem*j;
j=j*2;
no=no/10;
}
printf(”\nThe value of binary no. %ld is ->%ld”,no1,n);
getch();
}
WRITE A C PROGRAM FOR CONVERSION OF DECIMAL TO BINARY
void main()
{
int n,m,no=0,a=1,rem;
clrscr();
printf(”Enter any decimal number->”);
scanf(”%d”,&n);
m=n;
while(n!=0)
{
rem=n%2;
no=no+rem*a;
n=n/2;
a=a*10;
}
printf(”The value %d in binary is->”,m);
printf(”%d”,no);
getch();
}
{
int n,m,no=0,a=1,rem;
clrscr();
printf(”Enter any decimal number->”);
scanf(”%d”,&n);
m=n;
while(n!=0)
{
rem=n%2;
no=no+rem*a;
n=n/2;
a=a*10;
}
printf(”The value %d in binary is->”,m);
printf(”%d”,no);
getch();
}
WRITE A C PROGRAM FOR CHECKING LEAP YEAR
void main()
{
int year;
clrscr();
printf(”Enter any year->”);
scanf(”%d”,&year);
if(((year%4==0)&&(year%100!=0))(year%400==0))
printf(”%d is a leap year”,year);
else
printf(”%d is not a leap year”,year);
getch();
}
{
int year;
clrscr();
printf(”Enter any year->”);
scanf(”%d”,&year);
if(((year%4==0)&&(year%100!=0))(year%400==0))
printf(”%d is a leap year”,year);
else
printf(”%d is not a leap year”,year);
getch();
}
WRITE A C PROGRAM FOR PRINTING ASCII VALUE
void main()
{
int i;
clrscr();
for(i=0;i<=255;i++)
{
printf(”%d -> %c “,i,i);
delay(10);
}
getch();
}
{
int i;
clrscr();
for(i=0;i<=255;i++)
{
printf(”%d -> %c “,i,i);
delay(10);
}
getch();
}
WRITE A C PROGRAM TO FIND FIBONACCI SERIES
void main()
{
int i=0,j=1,k=2,r,f;
clrscr();
printf(”Enter the number range:”);
scanf(”%d”,&r);
printf(”\nFIBONACCI SERIES: “);
printf(”%d %d”,i,j);
while(k{
f=i+j;
i=j;
j=f;
printf(” %d”,j);
k++;
}
getch();
}
{
int i=0,j=1,k=2,r,f;
clrscr();
printf(”Enter the number range:”);
scanf(”%d”,&r);
printf(”\nFIBONACCI SERIES: “);
printf(”%d %d”,i,j);
while(k{
f=i+j;
i=j;
j=f;
printf(” %d”,j);
k++;
}
getch();
}
WRITE A C PROGRAM TO FIND FACTORIAL OF A NUMBER
void main()
{
int i=1,f=1,num;
clrscr();
printf(”\nEnter a number:”);
scanf(”%d”,&num);
while(i<=num)
{
f=f*i;
i++;
}
printf(”\nFactorial of %d is:%d”,num,f);
getch();
}
{
int i=1,f=1,num;
clrscr();
printf(”\nEnter a number:”);
scanf(”%d”,&num);
while(i<=num)
{
f=f*i;
i++;
}
printf(”\nFactorial of %d is:%d”,num,f);
getch();
}
WRITE A C PROGRAM FOR SWAP TWO VARIABLES WITHOUT USING THIRD VARIABLE
void main()
{
int a,b;
clrscr();
printf(”\nEnter two numbers:”);
scanf(”%d %d”,&a,&b);
printf(”\nBefore swapping a=%d b=%d”,a,b);
a=a^b;
b=b^a;
a=a^b;
printf(”\nAfter swapping a=%d b=%d”,a,b);
getch();
}
{
int a,b;
clrscr();
printf(”\nEnter two numbers:”);
scanf(”%d %d”,&a,&b);
printf(”\nBefore swapping a=%d b=%d”,a,b);
a=a^b;
b=b^a;
a=a^b;
printf(”\nAfter swapping a=%d b=%d”,a,b);
getch();
}
WRITE A C PROGRAM TO FIND PRIME FACTORS OF A NUMBER
void main()
{
int num,i=1,j,k;
clrscr();
printf(”\nEnter a number:”);
scanf(”%d”,&num);
while(i<=num)
{
k=0;
if(num%i==0)
{
j=1;
while(j<=i)
{
if(i%j==0)
k++;
j++;
}
if(k==2)
printf(”\n%d is a prime factor”,i);
}
i++;
}
getch();
}
{
int num,i=1,j,k;
clrscr();
printf(”\nEnter a number:”);
scanf(”%d”,&num);
while(i<=num)
{
k=0;
if(num%i==0)
{
j=1;
while(j<=i)
{
if(i%j==0)
k++;
j++;
}
if(k==2)
printf(”\n%d is a prime factor”,i);
}
i++;
}
getch();
}
WRITE A C PROGRAM FOR CONCATENATE MANY FILES AND STORE THEM IN A FILE NAMED FILES.
#include”stdio.h”
void concatenate(FILE *fp1,FILE *fp2,char *argv[],int argc);
void main(int argc,char *argv[])
{
FILE *fp1,*fp2;
concatenate(fp1,fp2,argv,argc);
getch();
}
void concatenate(FILE *fp1,FILE *fp2,char **argv,int argc)
{
int i,ch;
fp2=fopen(”files”,”a”);
for(i=1;i
{
fp1=fopen(argv[i],”r”);
while((ch=getc(fp1))!=EOF)
putc(ch,fp2);
}
}
void concatenate(FILE *fp1,FILE *fp2,char *argv[],int argc);
void main(int argc,char *argv[])
{
FILE *fp1,*fp2;
concatenate(fp1,fp2,argv,argc);
getch();
}
void concatenate(FILE *fp1,FILE *fp2,char **argv,int argc)
{
int i,ch;
fp2=fopen(”files”,”a”);
for(i=1;i
{
fp1=fopen(argv[i],”r”);
while((ch=getc(fp1))!=EOF)
putc(ch,fp2);
}
}
WRITE A CPROGRAM TO FIND MULTIPLICATION TABLE
void main()
{
int r,i,j,k;
clrscr();
printf(”\nEnter the number range:-”);
scanf(”%d”,&r);
for(i=1;i<=r;i++)
{
for(j=1;j<=10;j++)
printf(” %d*%d=%d”,i,j,i*j);
printf(”\n”);
}
getch();
}
{
int r,i,j,k;
clrscr();
printf(”\nEnter the number range:-”);
scanf(”%d”,&r);
for(i=1;i<=r;i++)
{
for(j=1;j<=10;j++)
printf(” %d*%d=%d”,i,j,i*j);
printf(”\n”);
}
getch();
}
WRITE A C PROGRAM FOR WRITING OF ENTIRE ARRAY TO A FILE
#include”stdio.h”
void main()
{
FILE *p;
int i,a[10];
if((p=fopen(”myfile.dat”,”wb”))==NULL)
{
printf(”\nUnable to open file myfile.dat”);
exit(1);
}
printf(”\nEnter ten values, one value on each line\n”);
for(i=0;i<10;i++)
scanf(”%d”,&a[i]);
fwrite(a,sizeof(a),1,p);
fclose(p);
getch();
}
void main()
{
FILE *p;
int i,a[10];
if((p=fopen(”myfile.dat”,”wb”))==NULL)
{
printf(”\nUnable to open file myfile.dat”);
exit(1);
}
printf(”\nEnter ten values, one value on each line\n”);
for(i=0;i<10;i++)
scanf(”%d”,&a[i]);
fwrite(a,sizeof(a),1,p);
fclose(p);
getch();
}
WRITE A C PROGRAM FOR READING OF STRINGS FROM A FILE
#include”stdio.h”
void main()
{
char str[70];
FILE *p;
clrscr();
if((p=fopen(”string.txt”,”r”))==NULL)
{
printf(”\nUnable t open file string.txt”);
exit(1);
}
while(fgets(str,70,p)!=NULL)
puts(str);
fclose(p);
}
void main()
{
char str[70];
FILE *p;
clrscr();
if((p=fopen(”string.txt”,”r”))==NULL)
{
printf(”\nUnable t open file string.txt”);
exit(1);
}
while(fgets(str,70,p)!=NULL)
puts(str);
fclose(p);
}
WRITE A C PROGRAM TO CREATE A FILE AND STORE DATA IN IT
#include”stdio.h”
void main()
{
FILE *fp;
char ch;
clrscr();
fp=fopen(”file.txt”,”w”);
printf(”\nEnter data to be stored in to the file:”);
while((ch=getchar())!=EOF)
putc(ch,fp);
fclose(fp);
getch();
}
void main()
{
FILE *fp;
char ch;
clrscr();
fp=fopen(”file.txt”,”w”);
printf(”\nEnter data to be stored in to the file:”);
while((ch=getchar())!=EOF)
putc(ch,fp);
fclose(fp);
getch();
}
WRITE A C PROGRAM TO PASSING ONE-DIMENSIONAL ARRAY TO A FUNCTION
#include “stdio.h”
#define N 5
void fstore1D(int a[], int a_size);
void fretrieve1D(int a[], int a_size);
void fedit1D(int a[], int a_size);
int main()
{
int a[N];
printf(”Input data into the matrix:\n”);
fstore1D(a, N);
fretrieve1D(a, N);
fedit1D(a, N);
fretrieve1D(a, N);
return 0;
}
void fstore1D(int a[], int n)
{
int i;
for ( i = 0; i < n; ++i )
scanf(”%d”, &a[i]);
}
void fretrieve1D(int a[], int n)
{
int i;
for ( i = 0; i < n; ++i )
printf(”%6d “, a[i]);
printf(”\n”);
}
void fedit1D(int a[], int n)
{
int i, q;
for ( i = 0; i < n; ++i )
{
printf(”Prev. data: %d\nEnter 1 to edit 0 to skip.”, a[i]);
scanf(”%d”, &q);
if ( q == 1 )
{
printf(”Enter new value: “);
scanf(”%d”, &a[i]);
}
}
}
#define N 5
void fstore1D(int a[], int a_size);
void fretrieve1D(int a[], int a_size);
void fedit1D(int a[], int a_size);
int main()
{
int a[N];
printf(”Input data into the matrix:\n”);
fstore1D(a, N);
fretrieve1D(a, N);
fedit1D(a, N);
fretrieve1D(a, N);
return 0;
}
void fstore1D(int a[], int n)
{
int i;
for ( i = 0; i < n; ++i )
scanf(”%d”, &a[i]);
}
void fretrieve1D(int a[], int n)
{
int i;
for ( i = 0; i < n; ++i )
printf(”%6d “, a[i]);
printf(”\n”);
}
void fedit1D(int a[], int n)
{
int i, q;
for ( i = 0; i < n; ++i )
{
printf(”Prev. data: %d\nEnter 1 to edit 0 to skip.”, a[i]);
scanf(”%d”, &q);
if ( q == 1 )
{
printf(”Enter new value: “);
scanf(”%d”, &a[i]);
}
}
}
WRITE A C PROGRAM FOR WRITING OF STRINGS TO A FILE
#include”stdio.h”
void main()
{
FILE *p;
char str[70];
if((p=fopen(”string.txt”,”w”))==NULL)
{
printf(”\nUnable to open file string.txt”);
exit(1);
}
else
printf(”\nEnter a set of strings,Press just enter key to finish\n: “);
while(strlen(gets(str))>0)
{
fputs(str,p);
fputs(”\n”,p);
}
fclose(p);
getch();
}
void main()
{
FILE *p;
char str[70];
if((p=fopen(”string.txt”,”w”))==NULL)
{
printf(”\nUnable to open file string.txt”);
exit(1);
}
else
printf(”\nEnter a set of strings,Press just enter key to finish\n: “);
while(strlen(gets(str))>0)
{
fputs(str,p);
fputs(”\n”,p);
}
fclose(p);
getch();
}
WRITE A C PROGRAM TO PASSING 2-DIMENSIONAL ARRAY TO A FUNCTION
#include “stdio.h”
#define M 3
#define N 5
void fstore2D(int a[][N]);
void fretrieve2D(int a[][N]);
int main()
{
int a[M][N];
printf(”Input data in matrix (%d X %d)\n”, M, N);
fstore2D(a);
fretrieve2D(a);
return 0;
}
void fstore2D(int a[][N])
{
int i, j;
for ( i = 0; i < M; ++i )
{
for ( j = 0; j < N; ++j )
scanf(”%d”, &a[i][j]);
}
}
void fretrieve2D(int a[][N])
{
int i, j;
for ( i = 0; i < M; ++i )
{
for ( j = 0; j < N; ++j )
printf(”%6d “, a[i][j]);
printf(”\n”);
}
}
#define M 3
#define N 5
void fstore2D(int a[][N]);
void fretrieve2D(int a[][N]);
int main()
{
int a[M][N];
printf(”Input data in matrix (%d X %d)\n”, M, N);
fstore2D(a);
fretrieve2D(a);
return 0;
}
void fstore2D(int a[][N])
{
int i, j;
for ( i = 0; i < M; ++i )
{
for ( j = 0; j < N; ++j )
scanf(”%d”, &a[i][j]);
}
}
void fretrieve2D(int a[][N])
{
int i, j;
for ( i = 0; i < M; ++i )
{
for ( j = 0; j < N; ++j )
printf(”%6d “, a[i][j]);
printf(”\n”);
}
}