Wednesday, August 17, 2011

C PROGRAMS




C LANGUAGE BOOK

XML SITEMAP For C Language Blog




Pankaj(Pank34aj)





   

Book Name-C Self Practical materials to learning



Author :-Pankaj(Pank34aj)





















☂★☢♔♊☀☂
Copyright © 2012 Pankaj(Pank34aj)~ - All Rights Reserved.
☂★☢♔♊☀☂






C is a programming language developed at AT & T’s  Bell
Laboratories of USA in  1972. It was designed and written by  a
man named Dennis Ritchie. An opinion that is often heard today
is – “C has been already superceded by languages like C++, C# and
Java, so why bother to learn C today” .C++, C# or Java make use
of a principle called Object Oriented Programming (OOP) to organize the
program. This organizing principle has lots of advantages to offer. But even
while using this organizing principle you would still need a good hold over the
language elements of C and the basic programming skills.

Major parts of popular operating systems like Windows,
UNIX, Linux is still written in C.  This is because even today
when it comes to performance (speed of execution) nothing
beats C.


HI THERE SOME C PROGRAM.HOPE YOU LIKE IT.I AM COOL.


C LANGUAGE BOOK

QUES1 BASIC AND SIMPLE ARITHMETIC


C LANGUAGE BOOK


The C Character Set
A character denotes any alphabet, digit or special symbol used to
represent information.  Figure shows the valid alphabets,
numbers and special symbols allowed in C.
 




C-Language keywords


Types of C Constants
C constants can be divided into two major categories:
(a) Primary Constants
(b) Secondary Constants 




Rules for Constructing Integer Constants

(a) An integer constant must have at least one digit. 
(b)It must not have a decimal point.
(c) It can be either positive or negative.
(d) If no sign precedes an integer constant it is assumed to be
positive.
(e) No commas or blanks are allowed within an integer constant.
(f) The allowable range for integer constants is -32768 to 32767. 



Rules for Constructing Real Constants

Real constants are often called Floating Point constants. The real
constants could be written in two forms—Fractional form and
Exponential form.

Following rules must be observed while constructing real
constants expressed in fractional form:

(a)A real constant must have at least one digit.
(b)It must have a decimal point.
(c)It could be either positive or negative.
(d)Default sign is positive.
(e)No commas or blanks are allowed within a real constant. 



In exponential form of representation, the real constant is
represented in two parts. The part appearing before ‘e’ is called
mantissa, whereas the part following ‘e’ is called exponent.

Following rules must be observed while constructing real
constants expressed in exponential form:

(a)The mantissa part and the exponential part should be
separated by a letter e.
(b)The mantissa part may have a positive or negative sign.
(c)Default sign of mantissa part is positive.
(d)The exponent must have at least one digit, which must be a
positive or negative integer. Default sign is positive.
(e)Range of real constants expressed in exponential form is 
-3.4e38 to 3.4e38. 


Rules for Constructing Character Constants

(a)A character constant is a single alphabet, a single digit or a
single special symbol enclosed within single inverted
commas.
(b)Both the inverted commas should point to the left.
For example, ’A’ is a valid character constant whereas ‘A’ is
not.
The maximum length of a character constant can be 1
character. 

DATA TYPES



















Hierarchy of Operations










Arithmetic Instruction

A C arithmetic instruction consists of a variable name on the left
hand side of = and variable names & constants on the right hand
side of =. The variables and constants appearing on the right hand
side of = are connected by arithmetic operators like +, -, *, and /.

Ex.:    int   ad ;
    float   kot, deta, alpha, beta, gamma ;
    ad = 3200 ;
    kot = 0.0056 ;
    deta = alpha * beta / gamma + 3.2 * 2 / 5 ;

Here, 

*, /, -, + are the arithmetic operators. 
= is the assignment operator. 
2, 5 and 3200 are integer constants. 
3.2 and 0.0056 are real constants. 
ad is an integer variable. 
kot, deta, alpha, beta, gamma are real variables.



Use of Logical Operators
C allows usage of three logical operators, namely, &&, || and !.
These are to be read as ‘AND’ ‘OR’ and ‘NOT’ respectively. 

The first two operators, && and  ||, allow two or more conditions
to be combined in an if statement.



 if ( ( per >= 40 ) && ( per < 50 ) )
printf ( "z" ) ;
else
printf("zz");

 z gets printed if both the conditions evaluate to true. If one of the conditions
evaluate to false then the whole thing is treated as false. and zz is printed.

Similarly
 if ( ( per >= 40 ) || ( per < 50 ) )
printf ( "z" ) ;
else
printf("zz");


 z gets printed if one of the conditions or both off the condition evaluate to true. If both conditions
evaluate to false then the whole thing is treated as false. and zz is printed.

The ! Operator
This ! operator reverses the result of the expression it operates on.















Hierarchy of Operators Revisited Including logical Operator













Comment

Single Line Comment can be used by using  // 
MultiLine Comment about the program should be enclosed within /*  */.

A comment can be split over more than one line, as in,
/* This is 
    a jazzy 
    comment */

Such a comment is often called a multi-line comment.


Assuming that you are using a Turbo C or Turbo C++ compiler
here are the steps that you need to follow to compile and execute
your first C program…

(a) Start the compiler at C> prompt. The compiler (TC.EXE is
usually present in C:\TC\BIN directory). 
(b) Select New from the File menu. 
(c) Type the program.
(d) Save the program using  F2 under a proper name (say
Program1.c).
(e) Use Ctrl + F9 to compile and execute the program.
(f) Use Alt + F5 to view the output. 

WAP TO PROGRAM TO PERFORM ADDITION,SUBTRACTION,MULTIPLICATION,DIVISION.


/* here stdio.h is the header file.it is used for standard input and output..it is enclosed in angle bracket or Double Quotes.Printf() function is used to display value on screen.scanf() function is used for inputting value from the user.to use header file we use #include it */


#include<stdio.h>
int main()
{

int i,j;
int d,e,k,h;

printf("\nENTER TWO NUMBERS : \n");
scanf("%d%d", &i, &j);
printf("\nTHE TWO NUMBERS ARE : \n%d%s%d", i," ", j);
d = i + j;
printf("\nTHE ADDITION OF TWO NUMBERS ARE: %d", d);
e = i - j;
printf("\n\nTHE SUBTRACTION OF TWO NUMBERS ARE : %d", e);
k = i * j;
printf("\n\nTHE MULTIPLICATION OF TWO NUMBERS ARE : %d", k);
h = i / j;
printf("\n\nTHE DIVISION OF TWO NUMBERS ARE : %d", h);

getch();
}



C LANGUAGE BOOK

QUES2 BASIC CONTINUE AND SUM AND AVG


C LANGUAGE BOOK


 Conditional Operators

The conditional operators  ? and  : are sometimes called ternary
operators since they take three arguments. In fact, they form a kind
of foreshortened if-then-else. Their general form is,

expression 1 ? expression 2 : expression 3
What this expression says is: “if expression 1 is true (that is, if its
value is non-zero), then the value returned will be  expression 2,
otherwise the value returned will be  expression 3”.

Control Instructions in C

As the name suggests the ‘Control Instructions’ enable us to
specify the order in which the various instructions in a program are
to be executed by the computer. In other words the control
instructions determine the ‘flow of control’ in a program. There
are four types of control instructions in C. They are:

(a)  Sequence Control Instruction
(b)  Selection or Decision Control Instruction
(c)  Repetition or Loop Control Instruction
(d)  Case Control Instruction

The Sequence control instruction ensures that the instructions are
executed in the same order in which they appear in the program.
Decision and Case control instructions allow the computer to take
a decision as to which instruction is to be executed next. The Loop
control instruction helps computer to execute a group of statements
repeatedly.

 Decision
a  decision control instruction
can be implemented in C using:

(a) The if statement 
(b) The if-else statement
(c) The conditional operators


The if Statement
 The general form of if statement looks
like this:

if ( this condition is true )
 execute this statement ;












The if-else Statement  

the general form of if-else statement is like this

if ( this condition is true )
 execute this statement1 ;
else
 execute this statement2 ;

 statement1 is executed if condition evaluates to true.
 if condition evaluates to false then  statement2 is executed.



Nested if-elses 

 It is perfectly all right if we write an entire if-else construct within
either the body of the if statement or the body of an else statement.
This is called ‘nesting’of  ifs.


Forms of if
The if statement can take any of the following forms:

(a)
if ( condition )
 do this ;

(b)
if ( condition )
{
 do this ;
 and this ;
}

(c)
if ( condition )
 do this ;
else
 do this ;

(d)
if ( condition )
{
 do this ;
  and this ;
}
else
{
 do this ;
 and this ;
}

(e)  if ( condition )
 do this ;
else
{
 if ( condition )
  do this ;
 else
 {
  do this ;
  and this ;
 }
}

 (f)
if ( condition )
{
 if ( condition )
  do this ;
 else
 {
  do this ;
  and this ;
 }
}
else
 do this ;
 


The else if Clause 
the general form of else if clause is like this

if ( this condition1 is true )
 execute this statement1 ;
else if ( this condition2 is true )
 execute this statement2 ;
else if ( this condition3 is true )
 execute this statement3 ;
else
execute this statement3 ;

In this Case every else is associated with its previous if.
The last else goes to work only if all the conditions evaluates to false. Even in
else if ladder the last else is optional.



WAP TO PRINT SUM AND AVERAGE OF 5 NUMBER THAT IS ENTERED BY USER.

#include<stdio.h>
int main(void)
{
int n, h, j, r, m;
float e,t;

scanf("%i%i%i%i%i", &h, &j, &n, &r, &m);
printf("\n%i%s%i%s%i%s%i%s%i", h," ", j, " ", n," ", r, " ",m);
e = h + j + n + r + m;

printf("\nthe sum is%f", e);
t = e / 5;

printf("\nthe average is%f", t);

getch();
}


C LANGUAGE BOOK

QUES3 BASIC CONTINUE AND LARGEST AND SECOND LARGESTNO


C LANGUAGE BOOK


Loops


The versatility of the computer lies in its ability to perform a set of
instructions repeatedly.  This involves repeating  some portion of
the program either a specified number of times or until a particular
condition  is  being  satisfied.  This  repetitive  operation is done
through a loop control instruction.




There are three methods by way of which we can repeat a part of a
program. They are:

(a)  Using a for statement
(b)  Using a while statement
(c)  Using a do-while statement


 The while Loop

It is often the case in programming that you want to do something
a fixed number of times.The while  loop  is  ideally  suited for  such cases.

The general form of while is as shown below:

initialise loop counter ;
while ( test loop counter using a condition )
{
  do this ;
  and this ;
 increment loop counter ;
}

here is example:-
 int   i = 1 ;
 while ( i <= 10 )
{
 printf ("%d\n", i ) ;
 i = i + 1 ;
 } 

 The for Loop

 The for allows us to specify three things about a loop in a single line: 

(a)  Setting a loop counter to an initial value.
(b)  Testing the loop counter to determine whether its value has
reached the number of repetitions desired.
(c)Increasing the value of loop counter each time the program
segment within the loop has been executed.

The general form of for statement is as under:

for ( initialise  counter ; test  counter ; increment  counter )
{
do this ;
and this ;
and this ; 
 } 



The do-while Loop

The do-while loop looks like this:

do
{
 this ;
 and this ;
 and this ;
 and this ;
} while ( this condition is true ) ;

This means  that  do-while  would execute its statements at least
once, even if the condition fails for the first time. The while, on
the other hand will not execute its statements if the condition fails
for the first time. do while  test condition at the end whereas
while test condition at the start.

Multiple Initialisations in the for Loop
The initialisation expression of the for loop can contain more than
one statement separated by a comma.
For example,
 for ( i = 1, j = 2 ; j <= 10 ; j++ )

Multiple statements can also be used in the incrementation
expression of for loop.However, only one expression is allowed
in the test expression.


Nesting of Loops
The way if statements can be nested, similarly whiles and fors can
also be nested.

The Odd Loop

/* Execution of a loop an unknown number of times */
main( )
{
 char  another ;
 int  num ;
 do
 {
  printf ( "Enter a number " ) ;
  scanf ( "%d", &num ) ;
  printf ( "square of %d is %d", num, num * num ) ;
  printf ( "\nWant to enter another number y/n " ) ;
  scanf ( " %c", &another ) ;
 } while ( another == 'y' ) ;
}

/* odd loop using a for loop */
main( )
{
 char  another = 'y' ;
 int  num ;
 for ( ; another == 'y' ; )
 {
  printf ( "Enter a number " ) ;
  scanf ( "%d", &num ) ;
  printf ( "square of %d is %d", num, num * num ) ;
  printf ( "\nWant to enter another number y/n " ) ;
  scanf ( " %c", &another ) ;
 }
}

/* odd loop using a while loop */
main( )
{
 char  another = 'y' ;
 int  num ;

 while ( another == 'y' )
 {
  printf ( "Enter a number " ) ;
  scanf ( "%d", &num ) ;
  printf ( "square of %d is %d", num, num * num ) ;
  printf ( "\nWant to enter another number y/n " ) ;
  scanf ( " %c", &another ) ;
 }
}


The break Statement 
When  break  is encountered inside any loop, control automatically passes to the
first statement after the loop. A break is usually associated with an
if.
The keyword  break, breaks the control only from the  while  in
which it is placed.

The continue Statement

When  continue  is encountered inside any loop,
control automatically passes to the beginning of the loop.
A  continue  is usually associated with an  if.



Decisions Using switch

The control statement that allows us  to make a decision from the
number of choices is called a switch, or more correctly a switch-
case-default, since these three keywords go together to make up
the control statement. They most often appear as follows:

switch ( integer expression )
{
  case constant 1 :
  do this ;
  case constant 2 :
  do this ;
  case constant 3 :
  do this ;
 default :
  do this ;  
}

The integer  expression following the keyword  switch is any C
expression that will yield an integer value. It could be an integer
constant like 1, 2 or 3, or an expression that evaluates to an
integer. The keyword case is followed by an integer or a character
constant. Each constant in each case must be different from all the
others. The “do this” lines in the above form of  switch represent
any valid C statement.


What happens when we run a program containing a switch? First,
the integer expression following the keyword switch is evaluated.
The value it gives is then matched, one by one, against the
constant values that follow the case statements. When a match is
found, the program executes the statements following that  case,
and all subsequent  case and  default statements as well. If no
match is found with any of the  case statements, only the
statements following the  default are executed.

The goto Keyword
the general form of goto is:
goto label;


label:


Avoid goto keyword! They make a C programmer’s life miserable.
There is seldom a legitimate reason for using goto, and its use is
one of the reasons that programs become unreliable, unreadable,
and hard to debug. And yet many programmers find  goto
seductive.
In a difficult programming situation it seems so easy to use a goto
to take the control where you want. However, almost always, there
is a more elegant way of writing the same program using  if,  for,
while and switch. These constructs are far more logical and easy
to understand.

Function
A function is a self-contained block of statements that perform  a
coherent task of some kind. Every C program can be thought of as
a collection of these functions.

the general form of the function is
return-type function_name(type varname1,type varname2,..............,type varnameN)
{
body of the function
}



return type specifies the type of data that the function returns.
example of return type are int,float,double,void,char

Note on return type:-
if you specified void
then function return nothing.
if you specified int
then function return integer value

if you specified float
then function return float value
if you specified char
then function return char value
and so on like this.
that which specified it return that value.


Passing Values between Functions

/* Sending and receiving values between functions */ 
main( )
{
 int  a, b, c, sum ;

 printf ( "\nEnter any three numbers " ) ;
 scanf ( "%d %d %d", &a, &b, &c ) ;

 sum = calsum ( a, b, c ) ;
 printf ( "\nSum = %d", sum ) ;
}

calsum ( x, y, z )
int  x, y, z ;
{
 int  d ;

 d = x + y + z ;
 return ( d ) ;
}

And here is the output...

Enter any three numbers 1 2 3
Sum = 6


the values of a, b and  c are passed on to the function  calsum( ),
by making a call to the function calsum( ) and mentioning a, b and c
in the parentheses:

sum = calsum ( a, b, c ) ;

In the  calsum( ) function these values get collected in three
variables x, y and z:
calsum ( x, y, z )
int  x, y, z ;

The variables  a,  b and  c are called ‘actual arguments’,
whereas the variables  x,  y and  z are called ‘formal
arguments’. Any number of arguments can be passed to a
function being called. However, the type, order and number of
the actual and formal arguments must always be same.

There are two methods of declaring the formal arguments.

The one that we have used in our program is known as
Kernighan and Ritchie (or just K & R) method. 
calsum ( x, y, z )
int  x, y, z ;

Another method is,
calsum ( int  x, int  y, int  z )
This method is called ANSI method and is more commonly
used these days.


Return statement

The return statement serves two purposes:
(1)  On executing the  return statement it immediately
transfers the control back to the calling program.
(2)  It returns the value present in the parentheses after
return, to th3e calling program. In the above program
the value of sum of three numbers is being returned.


There is no restriction on the number of  return statements
that may be present in a function. Also, the return statement
need not always be present at the end of the called function.

The following program illustrates these facts.
fun( )

 char  ch ; 

 printf ( "\nEnter any alphabet " ) ;
 scanf ( "%c", &ch ) ; 

 if ( ch >= 65 && ch <= 90 )
  return ( ch ) ;
 else 
  return ( ch + 32 ) ;
}

In this function different  return statements will be executed
depending on whether ch is capital or not.
Whenever the control returns from a function some value is
definitely returned. If a meaningful value is returned then it
should be accepted in the calling program by equating the
called function to some variable. For example,
sum = calsum ( a, b, c ) ;

All the following are valid return statements.
return ( a ) ;
return ( 23 ) ;
return ( 12.34 ) ;
return ;

Scope Rule of Functions
 by default the scope of a variable is local to the
function in which it is defined.

 

Arrays

Array is a collection of same type elements under the same variable identifier referenced by index number. Arrays are widely used within programming for different purposes such as sorting, searching and etc. Arrays allow you to store a group of data of a single type.  Arrays are efficient and useful for performing operations . You can use them to store a set of high scores in a video game, a 2 dimensional map layout, or store the coordinates of a multi-dimensional matrix for linear algebra calculations.


Arrays are of two types single dimension array and multi-dimension array.  Each of these array type can be of either static array or dynamic array. Static arrays have their sizes declared from the start and the size cannot be changed after declaration. Dynamic arrays that allow you to dynamically change their size at runtime, but they require more advanced techniques such as pointers and memory allocation. 

It helps to visualize an array as a spreadsheet. A single dimension array is represented be a single column, whereas a multiple dimensional array would span out n columns by n rows.

single dimensional array
 Arrays can be declared using any of the data types available in C. Array size must be declared using constant value before initialization.  A single dimensional array will be useful for simple grouping of data that is relatively small in size

datatype array_name[size_of_array]; . 
example are
int x[4];
float z[5];
char c[6];


Note: In C language the end of string is marked by the null character '\0'. Hence to store a group of 3 possible string data. the array as char ch[4]; This applies for char type array.
 ch[0]=s;
ch[1]=t;
ch[2]=d;
ch[3]='\0';

 Array can be initialized in two ways, initializing on declaration or initialized by assignment.

 initializing on declaration
 If you know the values you want in the array at declaration time, you can initialize an array as follows:
 datatype array_name[size]={element1,element2,....,elementN};

initialized by assignment
ch[0]=s;
ch[1]=t;
ch[2]=d;
ch[3]='\0';


Multidimensional arrays
Arrays can have more than one dimension. Two dimensional arrays are widely used for tables, neural networks and etc. You can have as many dimensions as you would like but you have to consider the complexity of the code as you add new dimension. Multidimensional arrays allow you to store data in a spreadsheet or matrix like format.

 To declare a multidimensional array:
datatype array_name[size_of_first_dimension][size_of_second_dimension] ...

example
int i[3][3];

To access elements in a multidimensional array, all you have to do is provide a combination of index values for each dimension to reach the desired element, just like a bunch of cells in a spreadsheet or coordinates in a n dimensional matrix or graph.

array_name[index1][index2] ... [indexM];

Setting values in a multidimensional array is just as simple as in a basic array, you need to add another index per dimension.
 array_name[index1][index2] ... [indexM] = value;

 example
i[0][1]=43;



An Introduction to Pointers 

A Pointer is a variable that holds memory address
.this address is the location of another object.

Pointer Operators:-
there are two special operator * and &.
The unary or monadic operator & gives the ``address of a variable''.
The indirection or dereference operator * gives the ``contents of an
object pointed to by a pointer''.

Declaration

Datatype *variable_name;


example
int *i,**j

here i is pointer to integer.j is pointer to pointer to integer.j is double pointer.

float *i;
here i is pointer to float;


consider statement
int *ptr,*p,a=56;
as you see in figure the value of a is stored at memory address 5000.



















to access its address value.
ptr=&a;
ptr hold the memory address of a.


printf("%d",ptr);
it will print 5000

printf("%d",*ptr);

it will print 56

pointer can be assign to another pointer
p=ptr;


pointer arithmetic


We can basically add and subtract pointer variables.
 If p1 and p2 are pointers to same data type then
p1- p2, p1+p2 , p1++ , p2-- , --p1 , p1+3 (multiplication, division not allowed)
p1<=p2, p1==p2 etc               (and other relational operations)

assume integer are two bytes long.
ptr++;
it wll point to next integer.
similarly
ptr--;
it wll point to previos integer.

ptr=ptr+5;
it will point to 5th element of ptr.




to assign annother value to a structure through ptr
we use arrow operator.(<-)
ptr->a=57;


printf("%d",*ptr);
it will print 57























Structure

This program demonstrates two fundamental aspects of structures:

(a)  declaration of a structure
(b)  accessing of structure elements

The general form of a structure declaration statement is
given below: 

struct <structure name>
{
 structure element 1 ;
 structure element 2 ;
 structure element 3 ;
 ......
 ......

} ;

Once the new structure data type has been defined one or more
variables can be declared to be of that type. For example the


struct book

 char  name ;
 float  price ;
 int  pages ;
} ;

struct book  b1, b2, *b3 ;

struct book
{
 char  name ;
 float  price ;
 int  pages ;
} b1, b2, *b3 ;
or even...

struct
{
 char  name ;
 float  price ;
 int  pages ;
} b1, b2, *b3 ;

struct book  b1 = { "x", 13 } ;
struct book  b2 = { "x", 31 } ;

Accessing Structure Elements

They use a dot (.) operator. So to refer to  pages
of the structure defined in our sample program
we have to use,
b1.pages

b3=&b1;
b3->name="xx";

b3 a pointer to a structure
C provides an operator ->, called an arrow operator
to refer to the structure elements.

printf ( "\n%s ", b3->name) ;







WAP TO FIND LARGEST AND SECOND LARGEST AMONG N NUMBER.

#include<stdio.h>
#define SIZE 100

int main(void)
{
int a[SIZE];
int i,j,n;
printf("\nHOW MANY ELEMENTS TO BE ENTERED IN AN ARRAY : \n");
scanf("%d", &n);

printf("\nENTER THE ELEMENTS IN AN ARRAY : \n");
for(i = 0; i < n; i++)
{
 scanf("\n%d",&a[i]);
}

printf("\nTHE GIVEN ARRAY IS : ");
for(i = 0; i < n; i++)
{
 display(a[i]);
}

  large(a, n);

}
display(int f)
{
printf("\n\n%d", f);
}

large(int w[], int x)
{
int i,j;
for(i = 0 ; i < x ; i++)
{
if(w[i] < w[i+1])
{
 j = i+1;
}
}
printf("\n\nTHE LARGEST VALUE IN ARRAY IS\n%s%d", " ", w[j]);
printf("\n\nTHE SECOND LARGEST VALUE IN ARRAY IS\n%s%d", " ", w[j]);

}








C LANGUAGE BOOK

QUES4 BASIC CONTINUE AND PRIME NO


C LANGUAGE BOOK


WAP TO PRINT PRIME NOS UPTO N NUMBERS.
#include<stdio.h>
main()
{
int n;

printf("\nHOW MANY PRIME NUMBER WILL BE PRINTED : \n");
scanf("%d", &n);

PrimeAll(n);
return;
}

PrimeAll(int n)
{
int i, j, w = 0;

if(n == 0 || n == 1)
{
printf("\nNO PRIME NOS UPTO %d NUMBERS \n", n);
}
else
{
printf("\nTHE PRIME NOS UPTO %d NUMBERS ARE : \n" ,n);
for(i = 2 ; i <= n ; i++)
{
for(j = 2 ; j <= i ; j++)
{
 if(i%j == 0)
  w++;
}

if(w == 1)
printf("%d%s", i," ");
w = 0;

}
}
}









C LANGUAGE BOOK

QUES5 SWAP NO


C LANGUAGE BOOK









/*
WAP TO PERFORM SWAPPING OF TWO NUMBER.
*/
#include<stdio.h>

int main(void)
{
int a,i,j;


printf("\n\nEnter The  Two Value For Swapping :\n");
scanf("\n%d%d", &i, &j);
printf("\n\nThe Two Value Are\n%d%s%d", i, " ", j);
swap(i, j);

}
swap(int i, int j)
{
int a;
a = i;
i = j;
j = a;
printf("\n\nThe Swap Value Are\n%d%s%d", i, " ", j);
}










C LANGUAGE BOOK

QUES6 GCD OF TWO NO


C LANGUAGE BOOK





/*
WAP TO FIND THE GCD OF TWO NUMBERS.
*/
#include<stdio.h>

int main(void)
{
int a;

int i,j;

printf("\nENTER TWO NUMBERS TO FIND \nTHE GCD OF THESE NUMBER");
printf("\n\nENTER THE FIRST NUMBER : \n");
scanf("%d", &i);
printf("\nENTER THE SECOND NUMBER : \n");
scanf("%d", &j);
printf("\nTHE TWO NUMBERS ARE : \n%d%s%d", i, " ", j);

if(i > j)
  GCD(i, j);
else
if(j > i)
  GCD(j, i);
}

GCD(int i, int j)
{
int a;
a = i % j;
if(a == 0)
  printf("\n\nTHE GCD OF TWO NUMBER IS\n%s%d%s", " ", j, " ");
else
  GCD(j, a);
}













C LANGUAGE BOOK

QUES7 MATRIX


C LANGUAGE BOOK




lets take an example.
In matrix.multiplication.

we have the matrix

first matrix have m row and n column
second matrix have n row and p column
when we multiply
third matrix should have m row and p column
((first matrix)m*n )*((second matrix)n*p )
n gets cancel out.

transpose

1 2 3
4 2 1
5 2 1

the transpose of this matrix is
1 4 5
2 2 2
3 1 1      

/*
WAP TO PERFORM OPERATION LIKE ADDITION,SUBTRACTION,MULTIPLICATION,TRANSPOSE. ON MATRIX
*/
#include<stdio.h>

int main()
{
int i,j,q;
int a[2][2], b[2][2], c[2][2];


/* Enter the contents of first matrix */

printf("\n\nEnter The Element For First Matrix:\n");

for(i = 0; i < 2; i++)
{
for(j = 0; j < 2; j++)
   scanf("\n%d", &a[i][j]);
}


/* Enter the contents of second matrix */

printf("\n\n\nEnter The Element For Second Matrix:\n");

for(i = 0; i < 2; i++)
{
for(j = 0; j < 2; j++)

  scanf("\n%d", &b[i][j]);
}

/* Display the contents of first matrix */

printf("\n\nThe First Matrix Are\n");

for(i = 0; i < 2; i++)
{
for(j = 0; j < 2; j++)
{  disp(a[i][j]);
}
printf("\n");
}

/* display the contents of second matrix */

printf("\n\nThe Second Matrix Are\n");

for(i = 0;i < 2;i++)
{
for(j = 0; j < 2; j++)
{
   disp(b[i][j]);
}
printf("\n");
}
/* Addition of two matrix */
printf("\nThe Addition Of Two Matrix Are\n");
for(i = 0; i < 2; i++)
{
for(j = 0; j < 2; j++)
{
   add(a[i][j], b[i][j], c[i][j]);
}
printf("\n");
}

printf("\nThe Subtraction Of Two Matrix Are\n");
for(i = 0; i < 2; i++)
{
for(j = 0; j < 2; j++)
{
   sub(a[i][j], b[i][j], c[i][j]);
}
printf("\n");
}

printf("\nThe Multiplication Of Two Matrix Are\n");
for(i = 0; i < 2; i++)
{
for(j = 0; j < 2; j++)
{
  
c[i][j]=0;
for(k = 0; k < 2; k++)
{
 mul(a[i][k], b[k][j], c[i][j]);
}
printf("\n");
}
}

printf ( "\n\nThe Transpose Of First Matrix Are :\n" ) ;

for ( q = 0 ; q < 2 ; q++ )
{
for ( i = 0 ; i < 2 ; i++ )
   c[q][i] = a[i][q];
}
  printf ( "\n" ) ;

for ( q = 0 ; q < 2 ; q++ )
{
for ( i = 0 ; i < 2 ; i++ )
{   disp ( c[q][i] ) ;
}
printf ( "\n" ) ;
}

printf ( "\n\nThe Transpose Of Second Matrix Are :\n" ) ;

for ( q = 0 ; q < 2 ; q++ )
{
for ( i = 0 ; i < 2 ; i++ )
   c[q][i] = b[i][q];
}
  printf ( "\n" ) ;

for ( q = 0 ; q < 2 ; q++ )
{
for ( i = 0 ; i < 2 ; i++ )
{   disp ( c[q][i] ) ;
}
printf ( "\n" ) ;
}

getch();
}


disp(int h)
 {
 printf("%d%s", h, " ");
 }


add(int r, int g, int f)
 {
  f = r + g;
 printf("%d%s", f, " ");
 }


sub(int r, int g, int f)
 {
  f = r - g;
 printf("%d%s", f, " ");
 }


mul(int r, int g, int f)
 {
  f = f+ (r * g);
 printf("%d%s", f, " ");
 }














C LANGUAGE BOOK

QUES 8 STRING REVERSE,POSITION OF CHARACTER AND LENGTH


C LANGUAGE BOOK








WAP TO COMPUTE THE STRING REVERSE,LENGTH OF THE STRING AND
FIND THE POSITION OF CHARACTER ENTERED BY THE USER.



#include<stdio.h>
#include<conio.h>
void main()
{
  int xstrlen(char *);
  int len1,len2,pos;
  char a[30],ch;

  clrscr(); //CLEARS THE OUTPUT SCREEN
  printf("enter the string: ");
  gets(a); //TAKING THE STRING
  len1= strlen (a);         //THE ORIGINAL FUNCTION
  len2= xstrlen (a);           //FUNCTION MADE BY USER
  printf("\n the length of the string is");
  printf("\t%d" ,len1); //PRINT THE LENGTH OF STRING



  printf("enter the character ");
  scanf("%c",&ch);
  pos=xstrchr(len,ch,a);
  printf("position of character is %d",pos);

 xstrev(a);
 printf("reverse string is %s",a);

  getch();
}

//FUNCTION TO FIND LENGTH OF THE STRING.
int xstrlen(char *s) //FUNC. TAKING CHAR POINTER
{
  int length=0;  //LENGTH IS INITIALISED WITH ZERO TO REMOVE GARBAGE VALUE
  while(*s!='\0')
  {
    length++; //INCREMENT THE LENGTH
    s++;     //INCREMENT THE S
  }
  return(length);  //RETURNS THE LENGTH
}




//FUNCTION TO FIND THE POSITION OF CHARACTER IN STRING.
int xstrchr(int len,char ch, char*s)
{
int t,i;

for(i=0;i<len;i++)
{
if(ch==(*s))
{
t=i;
break;
}
else
  s++;
}

return t;
}




//FUNCTION TO REVERSE THE STRING.
void xstrev(char*s)
{
char *t,temp;
int l,i;
l=strlen(s);
t=s+l-1;
for(i=1;i<=l/2;i++)
{
temp=*s;
*s=*t;
*t=temp;
s++;
t--;
}
}






C LANGUAGE BOOK

QUES 9 STRING COMPARE AND COPY AND CONCATENATE


C LANGUAGE BOOK














WAP TO PERFORM STRING COMPARISON,COPY ONE STRING TO ANOTHER
STRING AND CONCATENATE ONE STRING AT THE END OF OTHER.

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
void xstrcmp(char *s1,char *s2);
char str1[20];
char str2[20];
char target[20];


int n;

clrscr(); //CLEARS THE OUTPUT SCREEN
printf("enter the first string");
gets(str1); //FIRST STRING ENTERED BY THE USER.
printf("enter the second string");
gets(str2); //SECOND STRING ENTERED BY THE USER.

xstrcmp(str1,str2);

xstrcpy(target,str1);
printf("\ntarget string=%s",target); //PRINT THE TARGET STRING

printf("enter the number of characters u want to concatinate");


scanf("%d",&n);
xstrncat(str1,str2,n);

 printf("concatination string is:%s",str1);     //printing the concatinated string
 printf("\n \n \n \n press any key to exit");


getch();
}


 //function for comparing two string
 void xstrcmp(char *s1,char *s2) //FUNC. TAKING TWO CHAR POINTERS
    {
      int check;
     while((*s2!='\0')||(*s1!='\0'))
       {
         if(*s1==*s2) //CHECK FOR THE EQUALITY OF S1 AND S2
          {
         check=0;
         s1++; //INCREMENT S1
         s2++; //INCREMENT S2
           }
         else
         {
          check=1; //INITIALISE CHECK TO ONE
          break;
          }
         }
          if(check==0) //CHECKS FOR THE VALUE OF CHECK
          printf("Two strings are same");
          else
         {
           printf("Two strings are not same");
          }

         }

 //function for copy the string to another string.
void xstrcpy(char *t,char *s) //FUNC. TAKING TWO CHAR POINTERS
  {
    while(*s!='\0') //CHECKING S TO NULL ZERO
    {
      *t=*s;
      s++;  //INCREMENT THE S
      t++; //INCREMENT THE T
      }
       *t='\0';
    }


//function for concatinating the string
void   xstrncat(char*s1,char*s2,int n)   //declaring the function
 {
    int c=0;
    s1=s1+strlen(s1);
    while(c!=n)
   {
     *s1=*s2;
     s1++;
     s2++;
     c++;
   }
    *s1='\0';                         //2nd string terminated by null
 }











C LANGUAGE BOOK

QUES 10 REVERSE NO AND PRINT ITS SUM


C LANGUAGE BOOK





 WAP  TO PROGRAM TO REVERSE THE NUMBER AND TO CALCULATE THE SUM

#include<stdio.h>
#include<conio.h>
void main()
{
int num,x;
float sum=0;
clrscr(); //CLEARS THE OUTPUT SCREEN
printf("enter a number");
scanf("%d",&num);
while(num!=0)
    {
        x=num%10;   //CALCULATES THE REVERSE OF NUMBER
        num=num/10;
        printf("%d",x);
        sum=sum+x;  //CALCULATES THE SUM
    }
printf("sum=%f",sum);
getch();












C LANGUAGE BOOK

QUES 11 ARMSTRONG NO


C LANGUAGE BOOK






Example 153

 here first digit =1,
        second digit =5
        third digit=3
if  (1*1*1)+(5*5*5)+(3*3*3) == 153
then number is armstrong no
else
number is not armstrong no

1+125+27=153 hence 153 is armstong no.

 WAP TO PRINT ARMSTRONG NO. FROM 1 TO 500

 #include<stdio.h>                      
 #include<conio.h>                      
 void main()                             
 {
   int i,p1,p2,p3,s;
   clrscr();
   for(i=1;i<=500;i++)
   {
    p1= (i/100);                       //calculating first digit
    p2= ((i/10)-(10*p1));              //calculating second digit
    p3= ((i)-((100*p1)+(10*p2)));      //calculating third digit
    s=((p1*p1*p1)+(p2*p2*p2)+(p3*p3*p3));

   if(s==i)
    printf("\n armstrong no.is %d" ,s);

   }
   getch();
 }                                        






C LANGUAGE BOOK

QUES 12 FIBONACCI SERIES


C LANGUAGE BOOK









 In a Fibonacci sequence the sum of two
successive terms gives the third  term. Following are the first
few terms of the Fibonacci sequence:
1   1   2   3   5   8   13   21   34   55   89...  

Example
1+1=2 third no
 2+1=3 Fourth no
3+2=5 Fifth no

WAP A PROGRAM TO PRINT FIBONACCI SERIES UPTO NUMBER ENTERED BY THE THE USER.

#include<stdio.h>

void main()
{
int x, i;
printf(" ENTER NUMBER UPTO WHICH FIBONACI SERIES HAS TO BE GENERATED___ ");
scanf("%d", &x);
printf(" \n FIBONNACI IS = ");

for(i = 1; i < x; i++)
      fibb(i);


getch();
}
 fibb(int n)
 {
   int a;

  {
   if(n == 0)
      a = 0;
   if(n == 1)
      a = 1;
   else
      a = fibb(n-1) + fibb(n-2);

  }
  printf("%d", a);
return;
}









C LANGUAGE BOOK

QUES 13 STRING CONV(UP to LO and LO to UP)(CmdLineARG)


C LANGUAGE BOOK






 Command Line Argument:-

 A Command Line Argument is the information that follow the program name on the command line of the operating system.each command line argument is separated by space or tab.
Note:-commas,semicolons,and the like are not considered separators.

.when you compile a program you type at the command prompt something like
cc programname

in this program suppose you save abc.c.it take two command line argument.
argv[0] is  programname
argv[1] is argument.

you type at the command prompt to compile..
cc programname hello

here argv[1]=hello



WAP TO PERFORM STRING CONVERSION UPPER CASE TO LOWER CASE (OR LOWER CASE TO UPPER CASE) USING COMMAND LINE ARGUMENT.

#include<stdio.h>

main(int argc,char *argv[1])
{
int i;
if(argc != 2)
{
  printf("YOU FORGET TO TYPE YOUR ARGUMENTS");
  exit(1);
}
for(i=0;i<argc;i++)
 convers_(argv[i]);


return 0;
}
convers_(char *chh)
{

char ch;
 printf("TYPE YOUR ARGUMENTS");

while (*chh)
{
ch=*chh;

if(islower(ch))
 {
  ch = toupper(ch);
  printf("\nTHE CHARACTER IN UPPERCASE IS : %c",ch);
 }
 else
 if(isupper(ch))
 {
  ch = tolower(ch);
  printf("\nTHE CHARACTER IN LOWERCASE IS : %c",ch);

chh++;
 }

}


}






C LANGUAGE BOOK

QUES 14 AlphaBet Pattern


C LANGUAGE BOOK


















WAP TO PRINT THE ALPHABET IN FOLLOWING PATTERN.
















#include<stdio.h>

int main()
{
int p = 6,c = 2,i,q;
clrscr();

for(i = 65 ; i < 65+7; i++)
{
printf("%c%s", i," ");
}
for(i = 70 ; i > 64; i--)
{
printf("%c%s", i, " ");
}
printf("\n");

sort(p, c);
printf("\n");

return;
}

sort(int q, int c)
{
int i;
for(i = 65 ; i < 65+q; i++)
{
printf("%c%s", i," ");
}

if(q != 0)
{
for(i = 0; i < c; i++)
{
printf("%s"," ");
}
 order(q-1, c);
}
}

order(int p, int c)
{
int i;

for(i = 65+p ; i > 64; i--)
{
printf("%c%s", i, " ");
}
 if(p!=0)
  {
  printf("\n");
  c = c+4;
  sort(p, c);
  }
}


WAP TO PRINT THE ALPHABET IN FOLLOWING PATTERN.
 





















 
 #include<stdio.h>
int main()
{
int i=0,j=0;

clrscr();
for(i=0;i<6;i++)
{
printf("\n");

for(j=0;j<9;j++)
{
if(i==2||i==3)
{
  printf("*");
}
else
{
if(j==3 || j==4 || j==5)
 printf("_");
else
 printf("*");
}
}
}
}

WAP TO PRINT THE ALPHABET IN FOLLOWING PATTERN.


















 #include<stdio.h>
int main()
{
int i=0,j=0;

clrscr();
for(i=0;i<5;i++)
{
printf("\n");

for(j=0;j<9;j++)
{
if(i==3)
{
if(j==0)
{
printf("_");
}
else
{
printf("*");
}
}
else
if(i==4)
{
if(j==0 || j==1)
{
 printf("_");
}
else
if(j==2 || j==8)
{
 printf(" ");
}
else
{
printf("*");
}
}
else
{
if(j==3 || j==4 || j==5)
 printf("_");
else
 printf("*");
}
}
}
}

WAP TO PRINT THE ALPHABET IN FOLLOWING PATTERN.







#include<stdio.h>
int main()
{
int i=0,j=0;

clrscr();
for(i=0;i<6;i++)
{
printf("\n");
for(j=0;j<53;j++)
{
if(i==0)
{
if(j==0 || j==1 || j==2 || j==3 || j==4 || j==10 || j==17 || j==23 || j==25 || j==28 || j==36 || j==46 || j==47 || j==48 || j==49 || j==50)
 printf("*");
else
 printf(" ");
}

if(i==1)
{
if(j==0 || j==4 || j==9 || j==11 || j==17 || j==19 || j==23 || j==25 || j==27 || j==35 || j==37 || j==48)
 printf("*");
else
 printf(" ");

}
if(i==2)
{
if(j==0 || j==1 || j==2 || j==3 || j==4 || j==8 || j==12 || j==17 || j==20 || j==23 || j==25 || j==26 || j==34 || j==38 || j==48)
 printf("*");
else
 printf(" ");
}
if(i==3)
{
if(j==0 || j==7 || j==8 || j==9 || j==10 || j==11 || j==12 || j==13 || j==17 || j==21 || j==23 || j==25 || j==26 || j==33 || j==34 || j==35 || j==36 || j==37 || j==38 || j==39 || j==48)
 printf("*");
else
 printf(" ");
}
if(i==4)
{
if(j==0 || j==6 || j==14 || j==17 || j==17 || j==22 || j==23 || j==25 || j==27 || j==32 || j==40 || j==43 || j==48)
 printf("*");
else
 printf(" ");
}
if(i==5)
{
if(j==0 || j==5 || j==15 || j==17 || j==23  || j==25 || j==28 || j==31 || j==41 || j==43 || j==44 || j==45 || j==46 || j==47 ||  j==48)
 printf("*");
else
 printf(" ");
}



}
}
}









C LANGUAGE BOOK


QUES 15 Selection Sort


C LANGUAGE BOOK








WAP TO SORT THE NUMBER USING SELECTION SORT ENTER BY THE USER.



















#include<stdio.h>

#define SIZE 100

void Sel_Sort(int q, int a[]);

int main()
{
int i,q;
int a[SIZE];


/* how many number will be entered */

printf("\n\nHow Many Number Will Be Entered :");
  scanf("%d", &q);

for(i = 0; i < q; i++)
{
 printf("\ni = %d \t a = ", i + 1);
 scanf("%d", &a[i]);
}


/* Sort the elment using SelectionSort */
printf("\nSort The Number Using SelectionSort\n");
 Sel_Sort(q, a);
/* Displya the Sorted numbers */
printf("\nThe Sorted Number Are:");
for(i = 0;i < q;i++)
    printf("\ni = %d \t a = %d", i + 1, a[i]);

}

void Sel_Sort(int q,int a[])
{
 int position,j,i,k,temp;

 for(j=0;j < q;j++)
 {
   temp=9999;

 for(k = j;k < q; k++)
 {
   if(temp>a[k])
   {
   temp=a[k];
   position=k;
   }
 }
a[position] = a[j];
a[j] = temp;


printf("\nIteration j = %d ", j + 1);
for(i = 0; i < q; i++)
  printf("\ni = %d \t a = %d", i + 1,a[i]);
 }
 return;
 }









C LANGUAGE BOOK

QUES 16 Insertion Sort


C LANGUAGE BOOK











WAP TO SORT THE NUMBER USING INSERTION SORT ENTER BY THE USER.







#include<stdio.h>

#define SIZE 100

void Inser_Sort(int q, int a[]);

int main()
{
int i,q;
int a[SIZE];

/* how many number will be entered */

printf("\n\nHow Many Number Will Be Entered :");
  scanf("%d", &q);

for(i = 0; i < q; i++)
{
 printf("\ni = %d \t a = ", i + 1);
 scanf("%d", &a[i]);
}

for(i = 0; i < q; i++)
{
 printf("\ni = %d \t a = %d ", i+1,a[i]);
}
clrscr();
/* Sort the elment using InsertionSort */

printf("\nSort The Number Using InsertionSort\n");
 Inser_Sort(q, a);
/* Display the Sorted numbers */
printf("\nThe Sorted Number Are:");
for(i = 0;i < q;i++)
    printf("\ni = %d \t a = %d", i + 1, a[i]);

}

void Inser_Sort(int q,int a[])
{
 int i,j,k,temp;

for(j = 1; j < q; j++)
{
  temp = a[j];
for(k = j-1; k >= 0 && temp < a[k]; k--)
{
  a[k+1] = a[k];
}

a[k+1] = temp;

printf("\nIteration j = %d ", j);
for(i = 0; i < q; i++)
  printf("\ni = %d \t a = %d ", i+1,a[i]);
  printf("\n");
}
return;
}









C LANGUAGE BOOK

QUES 17 Bubble Sort


C LANGUAGE BOOK













WAP TO PERFORM SORT THE NUMBER USING BUBBLE SORT ENTERED BY THE USER.




















#include<stdio.h>

#define SIZE 100

void Bub1_Sort(int q, int a[]);

int main()
{
int i,q;
int a[SIZE];

/* how many number will be entered */

printf("\n\nHow Many Number Will Be Entered :");
  scanf("%d", &q);

for(i = 0; i < q; i++)
{
 printf("\ni = %d \t a = ", i + 1);
 scanf("%d", &a[i]);
}

for(i = 0; i < q; i++)
{
 printf("\ni = %d \t a = %d ", i+1,a[i]);
}
clrscr();
/* Sort the elment using BubbleSort */

printf("\nSort The Number Using BubbleSort\n");
 Bub1_Sort(q, a);
/* Display the Sorted numbers */

}

void Bub1_Sort(int q,int a[])
{
 int i,j,k,temp;

 for(j = q-1; j > 0; j--)
 {
 for(k = q-1; k > 0 ; k--)
 {
   if(a[k] < a[k-1])
   {
   temp = a[k-1];
   a[k-1] = a[k];
   a[k] = temp;
   }

 }
  if(j == 1)
   {
    printf("THE SORTED NUMBER ARE:\n");
      for(i = 0;i < q;i++)
       printf("\ni = %d \t a = %d ", i+1,a[i]);
       printf("\n");
   }
   else
   {
    for(i = 0;i < q;i++)
     printf("\ni = %d \t a = %d ", i+1,a[i]);
     printf("\n");
   }

}
 return;
}









C LANGUAGE BOOK

QUES 18 Merge Sort


C LANGUAGE BOOK








WAP TO MERGE TWO ARRAY.THE ELEMENT AND SIZE OF ARRAYS SHOULD BE ENTERED BY THE USER.

















#include<stdio.h>

#define SIZE1 100
#define SIZE2 100
#define SIZE3 200

void Bub1_Sort(int q, int a[]);
void MERGARR(int x,int a[],int y,int b[],int r, int c[]);

int main()
{
int i,q1,q2,q3;
int a1[SIZE1],a2[SIZE2];
int r[SIZE3];

/* how many number will be entered */

printf("\n\nHow Many Number Will Be Entered For The First Array:");
  scanf("%d", &q1);

for(i = 0; i < q1; i++)
{
 printf("\ni = %d \t a = ", i + 1);
 scanf("%d", &a1[i]);
}

for(i = 0; i < q1; i++)
{
 printf("\ni = %d \t a = %d ", i+1,a1[i]);
}

printf("\n\nHow Many Number Will Be Entered For The Second Array:");
  scanf("%d", &q2);

for(i = 0; i < q2; i++)
{
 printf("\ni = %d \t a = ", i + 1);
 scanf("%d", &a2[i]);
}
for(i = 0; i < q2; i++)
{
 printf("\ni = %d \t a = %d ", i+1,a2[i]);
}

/* Sort the elment using BubbleSort */

printf("\nSort The First Array Using BubbleSort\n");
 Bub1_Sort(q1, a1);

printf("\nSort The Second Array Using BubbleSort\n");
 Bub1_Sort(q2, a2);

/* Display the Sorted numbers */
clrscr();

     printf("THE FIRST ARRAY IS:\n");
      for(i = 0;i < q1;i++)
       printf("\ni = %d \t a = %d ", i+1,a1[i]);
       printf("\n");
     printf("THE SECOND ARRAY IS:\n");
      for(i = 0;i < q2;i++)
       printf("\ni = %d \t a = %d ", i+1,a2[i]);
       printf("\n");

q3 = q1+q2;

 MERGARR(q1, a1, q2, a2, q3, r);

      printf("THE MERGING ARRAY IS:\n");
      for(i = 0;i < q3;i++)
       printf("\ni = %d \t a = %d ", i+1,r[i]);
       printf("\n");
}

void Bub1_Sort(int q,int a[])
{
 int i,j,k,temp;

 for(j = q-1; j > 0; j--)
 {
 for(k = q-1; k > 0 ; k--)
 {
   if(a[k] < a[k-1])
   {
   temp = a[k-1];
   a[k-1] = a[k];
   a[k] = temp;
   }

 }


}
 return;
}


void MERGARR(int x,int a[],int y,int b[],int r, int c[])
{
 int i,j,k,temp;
 int alimit,blimit,climit;
 alimit = x;
 blimit = y;
 climit = r;

 printf("\nx = %d", x);
 printf("\ny = %d", y);
 printf("\nr = %d\n", r);

 if((x+y)!=r)
 {
 printf("\nArray not compatible");
 exit(1);
 }
 i=0;
 j=0;

 for(k=0; i<=alimit && j<=blimit;k++)
     if(a[i] < b[j])
       c[k] = a[i++];
     else
      c[k] = b[j++];


 return;
}






C LANGUAGE BOOK

QUES 19 FACTORIAL Recursive and Non Recursive.


C LANGUAGE BOOK



Factorial of 2 = 2 *1=2
Factorial of 3 = 3*2 *1=6
Factorial of 4 = 4*3*2 *1=24
Factorial of 5 = 5*4*3*2 *1=120

WAP TO PRINT THE  FACTORIAL UPTO N NUMBER USING RECURSION AND NON RECURSION.
THE N SHOULD BE ENTER BY THE USER.


#include<stdio.h>

void main()
{
int x, i,j=0;
printf(" ENTER NUMBER UPTO WHICH FACTORIAL HAS TO BE GENERATED___ ");
scanf("%d", &x);
printf(" \n FACTORIAL ARE USING RECURSION");

for(i = 1; i < x; i++)
{
j=FACTR(i);
printf(" \n %d ",j);
}



j=0;


printf(" \n FACTORIAL ARE USING NON-RECURSION");

for(i = 1; i < x; i++)
{
j=FACTNR(i);
printf(" \n %d ",j);
}
getch();
}

/*Recursive*/ 
FACTR(int n)
{
   int t;

   if(n == 1)
     return(1);
  
    t = FACTR(n-1)*n;      /*Recursive call*/
     return (t);
}


/*Non-Recursive*/ 

FACTNR(int n)
{

int i,t;
t=1;
for(i=1;i<=n;i++)

    t = t*i;      /*Non-Recursive call*/
  }
return (t);
}








C LANGUAGE BOOK