C Igate Bhilai
C Igate Bhilai
Study Material
C-Programming
for
By
Siddharth S. Shukla
Website: www.igate.guru
1
Page
1
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
Sememster Classes:
1. C-Programming 3rd Semster
2. C++ with OOP’s 4th Semester
3. Java(Core+adv) 5th Semester
4. .Net(VB,C#,ASP)
5. Data Structure 4th Semster
6. Analysis and Design of Algorithm 5th Semester
7. Theory of Computation 5th Semester
8. Compiler Design 6th Semster
Gate Classes:
1. One Year Gate Class Room Program for 6th and 7th Semester
2. Two Year Gate Class Room Program for 5th Semester
Features:
1. Best Gate Result in C.G. in CS/IT
2. Study material
3. Question Bank(Subject wise)
4. Test Series(Subject Wise+Mock Test)
5. Cover all technical and non technical topics(Apti+Maths)
2
Page
2
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
Copyright©,i-gate publication
First edition 2021
No part of this book or parts thereof may be reproduced, stored in a retrieval system or
transmitted in any language or by any means, electronic, mechanical, photocopying, recording or
otherwise without the prior written permission of the publisher.
3
Page
3
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
Answer:
C99
C99 introduced several new features, including inline functions, several new data types
(including long long int and a complex type to represent complex numbers), variable-
length arrays, support for variadic macros (macros of variable arity) and support for one-
line comments beginning with //, as in BCPL or C++. Many of these had already been
implemented as extensions in several C compilers.
C99 is for the most part backward compatible with C90, but is stricter in some ways; in
particular, a declaration that lacks a type specifier no longer has int implicitly assumed. A
standard macro STDC VERSION is defined with value 199901L to indicate that C99
support is available. GCC, Sun Studio and other C compilers now support many or all of
the new features of C99.
4
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
Characteristics of c language:-
5
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
Answer: C Compiler
Visual C++ 2010 includes a C compiler that you can use to create everything from
basic C programs to Windows API applications.
This walkthrough shows how to create a basic C program by using a text editor, and
then compile it on the command line.
You can use your own C programs instead of typing the sample programs shown in
this walkthrough. You can also use any C code sample programs that are incle studied
in the Help topics.
By default, the Visual C++ compiler treats all files that end in .c as C source code,
and all files that end in .cpp as C++ source code. To force the compiler to treat all
files as C regardless of file name extension, use the /Tc compiler option.
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…
start the compiler at C> prompt. The compiler (TC.EXEis usually present in
c:\TC\BIN directory).
Select New from the File menu.
6
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
In C the characters that can be used to form words, numbers and expressions are grouped
into the following categories.
1.LETTERS :
Uppercase A……….Z
Lowercase a…………z
2. DIGITS :
3. SPECIAL CHARACTERS :
4. WHITE SPACES :
Blank space, Horizontal tab, Carriage sign, New line, Form feed.
7
Page
7
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
Keywords:
Keywords are the words whose meaning has already been defined by the C compiler. All
keywords have fixed meanings and these meanings cannot be changed. Keywords serve
as basic building blocks for program statements. All keywords must be written in
lowercase.
For Example:
Char, const, int, float, for, struct, union, switch, goto, while
These are the user-defined names and consist of sequence of letters and digits, with a
letter as a first character.
Answer: Constant
The alphabets, numeric and special symbols when properly combined form constant &
variables.
Types of Constant:-
Numeric constant
Character constant
1.Numeric constants:-
8
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
a .Decimal integer:
Embedded spaces commas and non digit characters are not permitted
b.Octal integer :
c. Hexadecimal integer:
(ii).Real constants:- Numbers containing fractional parts are calledreal (floating point)
constants.
a.Decimal Notation:
eg: 3.45
b. Scientific Notation :
2.Character constants:-
Single character constant:- Single character enclosed within a pair of single quotes.
9
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
Pointer Constant:
These include the name of array without subscript and the name of a function without
parenthesis. A string literal actually is an instance of a constant pointer, since its value is
apointer to the string.
Answer:
Unlike other high-level languages, C does not have any built-in input/output statements
as part of its syntax. All input/output operations are carried out through function calls
such as printf and scanf. There exist several functions that have more or less become
standard for input and output operations in C. Those functions are collectively known as
the standard I/O library.
Each program that uses a standard input/output function must contain the statement :
#include<stdio.h>
The file name stdio.h is an abbreviation for standard input output header file. This
instruction tells the compiler to search for a file named stdio.h and place its contents at
this point in program. The contents of the header file become part of the source code
when it is compile.
Answer:
EXPRESSION
In C every expression evaluates to a value i.e. every expression results in some value of a
certain type that can be assigned to a variable.
Variable=expression
10
Page
10
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
z=a-b/c+d
2. If parentheses are nested, the evaluation begins with the innermost sub-expression.
4. The associativity rule is applied when two or more operators of the same precedence
level appear in a sub-expression.
5. Arithmetic expressions are evaluated from left to right using the rules of precedence.
6. When parentheses are used, the expressions within parentheses assume highest
priority.
if we are to use the computer as a problem-solving tool, then we must have a good
analysis of the problem given. Here are some suggested steps on how to go about
analyzing a certain problem for computer application:
Review the problem carefully and understand what you are asked to do.
Determine the manner of processing that must be done on the input data to come up with
the desired output(i.e., determine what formulas are needed to manipulate the given data).
Example
Given the scores for the two departmental quizzes, two machine projects, final exam, and
teacher's evaluation, write a program that will compute for the final grade based on the
11
following computation:
Page
11
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
FLOW CHART:-
Advantages of flowchart:-
It provides an easy way of communication because any other person besides the
programmer can understand the way they are represented.
It provides a clear overview of the entire program and problem and solution.
12
Page
12
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
Algorithm Development:-
Problem-solving phase:
Analyze - understand and define the problem; ask questions, state assumptions.
Develop an algorithm -- determine the steps necessary to solve the problem; use scratch
paper.
Test - follow the exact steps to make sure that the algorithm works. Try specific cases
and see how your algorithm reacts.
Algorithm
Implementation phase:
Test -have the computer follow the steps (execute the program).
Debug - check the results and make corrections until the answers are correct.
Answer: A C program can be viewed as a group of building blocks called functions. A function
is a subroutine that may include one or more statements designed to perform a specific
task. To write a C program we first create functions and then put them together. A C
program may contain one or more sections :
13
Page
13
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
Documentation Section
Link Section
Definition Section
Declaration part
Executable part
} Subprogram Section
Documentation section :
It consist of a set of comment lines giving the name of the program, the author and other
Link section :
It provides instructions to the compiler to link functions from the system library.
Global Declaration Section : There are some variables that are used in more than one
function. Such variables are called global variables and are declared in the global
declaration section that is outside all the functions. This section also declares all the user-
14
defined functions.
Page
14
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
Every C program must have one main function section. This section contains two parts,
declaration part and executable part. The declaration part declares all the variables that
are used in executable part, these two parts must appear between the opening and closing
braces.
Subprogram section : This section contains all the user defined functions that are called in main
function section.
There may be situations where we want to change the order of execution of statements
based on certain decisions or repeat a group of statement until specified conditions are
met. This involves a kind of decision making to see whether a particular condition has
occurred or not and then direct the computer to execute certain statements accordingly.
#include <stdio.h>
main()
printf("Hello World\n");
return 0;
Begins the design with main or top-level module, and progresses downwards to the
lowest level module.
15
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
of subroutines, block structures and for and while loops - in contrast to using simple tests
and jumps such as the gotostatement which could lead to "spaghetti code" which is both
difficult to follow and to maintain.
The selection constructs allow us to follow different paths in different situations. We may
also think of them as enabling us to express decisions.
if (expression)
statement1
else
statement2
If and else are independent constructs, in that if can occur without else (but not the
reverse).Any else is paired with the most recent else-less if, unless curly braces enforce a
different scheme. Note that only curly braces, not parentheses, must be used to enforce
the pairing. Parentheses
Iteration
16
Page
16
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
Looping is a way by which we can execute any some set of statements more than one
times continuously .In C there are mainly three types of loops are used :
while Loop
do while Loop
For Loop
The control structures are easy to use because of the following reasons:
Question 12: Explain Variable and also degine Local and Global Variable.
Answer: Variable
Variable is a name of memory location where we can store any data. It can store only
single data (Latest data) at a time. In C, a variable must be declared before it can be used.
Variables can be declared at the start of any block of code, but most are found at the start
of each function.
A declaration begins with the type, followed by the name of one or more variables. For
example,
DataType Name_of_Variable_Name;
int a,b,c;
Variable Names
Every variable has a name and a value. The name identifies the variable, the value stores
data. There is a limitation on what these names can be. Every variable name in C must
start with a letter; the rest of the name can consist of letters, numbers and underscore
characters. C recognizes upper and lower case characters as being different. Finally, you
cannot use any of C's keywords like main, while, switch etc as variable names.
17
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
x1 x2 out_file best_yet
power impetus gamma hi_score
It is conventional to avoid the use of capital letters in variable names. These are used for
names of constants. Some old implementations of C only use the first 8 characters of a
variable name.
Local Variables
Local variables are declared within the body of a function, and can only be used within
that function only.
Syntex:
void main( ){
int a,b,c; }
void fun1() {
int x,y,z; }
Here a,b,c are the local variable of void main() function and it can‘t be used within fun1()
Function. And x, y and z are local variable of fun1().
Global Variable
A global variable declaration looks normal, but is located outside any of the program's
functions. This is usually done at the beginning of the program file, but after preprocessor
directives. The variable is not declared again in the body of the functions which access it.
Syntax:
#include<stdio.h>
int a,b,c;
void main()
}
18
Page
18
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
void fun1()
Here a,b,c are global variable .and these variable cab be accessed (used) within a whole
program.
Question 13: List out all the operators with their priority and associativity?
19
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
Solution:
/*---------------------------------------------------------------------------
PROGRAM TO FIND THE ROOTS OF A QUADRATIC EQUATION
---------------------------------------------------------------------------*/
#include <stdio.h>
#include <conio.h>
#include <math.h>
int main()
{
int A,B,C;
float disc,deno,x1,x2;
printf("\n-------------------------------------------------------");
printf("\n\n PROGRAM TO FIND THE ROOTS OF A QUADRATIC EQUATION ");
20
printf("\n\n-------------------------------------------------------");
Page
20
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
Progrom 2. Write a program to input distance between two cities in kilometers & convert it
into meters & centimeters, feet & inches.
Solution: #include<stdio.h>
int main()
{int km,meter,cm;
float inch,feet;
scanf("%d",&km);
//Conversion of km in meter
21
meter = km * 1000;
Page
21
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
cm = meter * 100;
inch = cm / 2.54f;
return 0;
Solution: #include<stdio.h>
#include<conio.h>
void main ()
{ float cel, faren;
clrscr ();
printf ("Enter The Temperature In Degree Celsius: ");
scanf ("%f", &cel);
faren=(9*cel/5)+32;
printf ("The Equivalent Temperature In Farenheit is : %f", faren);
getch ();
}
OUTPUT
Enter The Temperature In Degree Celsius: -40
The Equivalent Temperature In Fahrenheit is : -40
22
22
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
Solution: #include<stdio.h>
#include<conio.h>
void main()
{ int a, b, sum, diff, pro, div;
clrscr ();
printf ("Enter The Two Numbers : ");
scanf ("%d%d", &a, &b);
sum=a+b;
diff=a-b;
pro=a*b;
div=a/b;
printf ("Sum=%d\n", sum);
printf ("Difference=%d\n", diff);
printf ("Product=%d\n", pro);
printf ("Quotient=%d\n", div);
getch ();
}
OUTPUT
Enter The Two Numbers : 15 23
Sum = 38
Difference = -8
Product = 345
Quotient = 0
Progrom 5: Write a program to swap two numbers without using third variable
Solution: #include<stdio.h>
#include<conio.h>
void main()
{ int a,b;
clrscr();
printf("Enter The Value Of a and b respectively : ");
scanf ("%d%d",&a,&b);
a=a+b;
b=a-b;
a=a-b;
printf("\nThe Values Of a and b After Swapping is %d & %d",a,b);
getch();
}
23
OUTPUT
Page
23
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
Solution: #include<stdio.h>
#include<conio.h>
void main()
{ int a,b,c;
clrscr();
printf("Enter The Value Of a and b respectively : ");
scanf ("%d%d",&a,&b);
c=a+b;
b=c-b;
a=c-b;
printf("\nThe Values Of a and b After Swapping is %d %d",a,b);
getch();
}
OUTPUT
Enter The Values Of a and b respectively : 15 65
The Values Of a and b After Swapping is 65 & 15
Solution: #include<stdio.h>
#include<conio.h>
void main()
{ char a;
clrscr();
printf("Enter The Character Whose ASCII value You Want To Print \t ");
a = getchar();
printf("\n The ASCII Value of Entered Character Is %d", a);
getch();
}
OUTPUT
Enter The Character Whose ASCII value You Want To
Print a
24
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
Progrom 8:Write a programto calculate the sum & percentage of marks obtained in 5 subject
Solution: #include<stdio.h>
#include<conio.h>
void main()
{ int a,b,c,d,e;
float sum, per;
clrscr();
printf("Enter The Marks In Five Subjects Respectively\n");
scanf("%d%d%d%d%d",&a,&b,&c,&d,&e);
sum = a+b+c+d+e;
per = (sum / 5);
printf("\n\nThe Sum Of The Marks Obtained Is \t%f",sum);
printf("\n\nThe Percentage is \t%f",per);
getch();
}
OUTPUT
Enter The Marks In Five Subjects Respectively
56 65 98 78 74
The Sum Of The Marks Obtained Is 371.000000
The Percentage Is 74.20000
Solution: #include<stdio.h>
#include<conio.h>
void main ()
{ int num, a, b, c, num1;
clrscr ();
printf ("Enter The Four Digit Number : ");
scanf ("%d", &num);
a=num%10;
num=num/10;
b=num%10;
num=num/10;
c=num%10;
num=num/10;
num1=a*1000+b*100+c*10+num;
printf ("The Reversed Four Digit Number Is : %d", num1);
getch ();
}
25
OUTPUT
Page
25
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
Program 10: Write a program to compare two numbers and print the largest number without
Using if .
Solution:
#include<stdio.h>
#include<conio.h>
void main ()
{ int a, b, c;
clrscr ();
printf ("Enter The Two Numbers Respectively: ");
scanf ("%d%d", &a, &b);
c=(a>b)?a:b;
printf ("The Largest Among The Two Number Is : %d", c);
getch ();
}
OUTPUT
Enter The Two Numbers Respectively : 15 98
The Largest Among Two Number Is : 98
26
Page
26
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
Question 1: Discuss Decision making If Statement and Swich case Statement with proper
syntax..
if statement
switch statement
IF- Statement:
It is the basic form where the if statement evaluate a test condition and direct program
execution depending on the result of that evaluation.
Syntax:
If (Expression)
Statement 1;
Statement 2;
Example:
if (a>b)
27
{
Page
27
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
IF-ELSE Statement:
An if statement may also optionally contain a second statement, the ``else clause,'' which
is to be executed if the condition is not met. Here is an example:
if(n > 0)
average = sum / n;
else
{
printf("can't compute average\n");
average = 0;
}
NESTED-IF- Statement:
It's also possible to nest one if statement inside another. (For that matter, it's in general
possible to nest any kind of statement or control flow construct within another.) For
example, here is a little piece of code which decides roughly which quadrant of the
compass you're walking into, based on an x value which is positive if you're walking east,
and a y value which is positive if you're walking north:
if(x > 0)
{
if(y > 0)
printf("Northeast.\n");
else printf("Southeast.\n");
}
else {
if(y > 0)
printf("Northwest.\n");
else printf("Southwest.\n");
}
Switch Case
This is another form of the multi way decision. It is well structured, but can only be used
in certain cases where;
Only one variable is tested, all branches must depend on the value of that variable.
28
28
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
Each possible value of the variable can control a single branch. A final, catch all,
default branch may optionally be used to trap all unspecified cases.
Hopefully an example will clarify things. This is a function which converts an integer
into a vague description. It is useful where we are only concerned in measuring a quantity
when it is quite small.
Syntax:
switch(expression)
default: statement;
In each case the value of itemi must be a constant, variables are not allowed.
29
Page
29
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
int number;
/* Estimate a number as none, one, two, several, many */
{ switch(number) {
case 0 :
printf("None\n");
break;
case 1 :
printf("One\n");
break;
case 2 :
printf("Two\n");
break;
case 3 :
case 4 :
case 5 :
printf("Several\n");
break;
default :
printf("Many\n");
break;
}
}
Question 2: Write menu driven program for calculator using Switch case.
Solution: #include<stdio.h>
#include<conio.h>
void main()
{char ch='y';
int a,b,c,choice;
clrscr();
scanf("%d%d",&a,&b);
30
while(ch=='y')
Page
30
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
printf("Operations:\n1.Addition\n2.Subtraction\n3.Multiplication\n4.Division");
scanf("%d",&choice);
switch(choice)
ase 1: c=a+b;
break;
case 2: c=a-b;
break;
case 3 :c=a*b;
break;
case 4: c=(float)(a)/b;
break;
break;
scanf("%c",&ch);
31
Page
31
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
getch();
OUTPUT :
Operations:
1.Addition
2.Subtraction
3.Multiplication
4.Division
Result of addition is :7
Answer: ? : operator
The C language has an unusual operator, useful for making two-way decisions. This
operator is a combination of ? And : and takes three operands. This operator is popularly
known as conditional operator.The general form of conditional operator is :
300 for x = 40
Can be written as :
Page
32
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
Salary=(x<40) ? (4*x+100):(x>40)?(4.5x+150):300;
goto statement
C supports the goto statement to branch unconditionally from one point to another in the
program.
The goto requires a label in order to identify the place where the branch is to be made. A
label is any valid variable name, and must be followed by a colon. The label is placed
immediately before the statement where the control is to be transferred.
label:
goto label;
……………. statement;
……………. …………….
label: …………….
statement;
goto label;
The label can be anywhere in the program either before or after the goto label: statement.
If the label is before the statement goto label; a loop will be formed and some statement
will be executed repeatedly. Such a jump is known as a backward jump.
If the label: is placed after the goto label; some statements will be skipped and the jump
is known as a forward jump.
Answer: Looping
In looping a sequence of statements are executed until some conditions for the
termination of the loop are satisfied. A program loop consist of two segments , one
known as body of loop and the other known as control statement .
The control statement test certain conditions and then directs the repeated execution of
the statements contained in the body of the loop.
33
Page
33
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
It is used when we know in advance exactly how many times the loop will be executed
In this type of loop a special value called a sentinel value is used to change the loop
expression from true to false
Loops
Looping is a way by which we can execute any some set of statements more than one
times continuously .In c there are mainly three types of loops are use :
while Loop
do while Loop
For Loop
While Loop
Loops generally consist of two parts: one or more control expressions which (not
surprisingly) control the execution of the loop, and the body, which is the statement or set
of statements which is executed over and over.
while( expression )
{ Statement1
34
Statement2
Page
34
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
Statement3
}
The most basic loop in C is the while loop. A while loop has one control expression, and
executes as long as that expression is true. This example repeatedly doubles the number 2
(2, 4, 8, 16, ...) and prints the resulting numbers as long as they are less than 1000:
int x = 2;
For Loop
Our second loop, which we've seen at least one example of already, is the for loop. The
general syntax of a while loop is
for( Initialization;expression;Increments/decrements )
{
Statement1
Statement2
Statement3
Do while Loop
This is very similar to the while loop except that the test occurs at the end of the loop
35
body. This guarantees that the loop is executed at least once before continuing. Such a
Page
35
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
setup is frequently used where data is to be read. The test then verifies the data, and loops
back to read again if it was unacceptable.
do
{
printf("Enter 1 for yes, 0 for no :");
scanf("%d", &input_value);
} while (input_value != 1 && input_value != 0)
12345
1234
123
12
Answer: #include<stdio.h>
main()
int n, c, k, space;
scanf("%d", &n);
space = 0;
printf(" ");
space++;
36
36
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
printf("%d", c);
printf("\n");
return 0;
We have already met break in the discussion of the switch statement. It is used to exit
from a loop or a switch, control passing to the first statement beyond the loop or a switch.
With loops, break can be used to force an early exit from the loop, or to implement a loop
with a test to exit in the middle of the loop body. A break within a loop should always be
protected within an if statement which provides the test to control the exit condition.
This is similar to break but is encountered less frequently. It only works within loops
where its effect is to force an immediate jump to the loop control statement.
Like a break, continue should be protected by an if statement. You are unlikely to use it
very often.
int i;
for (i=0;i<10;i++)
{
if (i==5)
37
continue;
Page
37
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
printf("%d",i);
if (i==8)
break;
}
Continue means, whatever code that follows the continue statement WITHIN the loop
code block will not be exectued and the program will go to the next iteration, in this
case, when the program reaches i=5 it checks the condition in the if statement and
executes 'continue', everything after continue, which are the printf statement, the next
if statement, will not be executed.
Break statement will just stop execution of the look and go to the next statement after
the loop if any. In this case when i=8 the program will jump out of the loop. Meaning,
it wont continue till i=9, 10.
Answer:
The break statement will immediately jump to the end of the current block of code.
The continue statement will skip the rest of the code in the current loop block and will
return to the evaluation part of the loop. In a do or while loop, the condition will be tested
and the loop will keep executing or exit as necessary. In a for loop, the counting
expression (rightmost part of the for loop declaration) will be evaluated and then the
condition will be tested.
Goto allows to make an absolute jump to another point in the program. You should use
this feature with caution since its execution causes an unconditional jump ignoring any
type of nesting limitations.
The destination point is identified by a label, which is then used as an argument for the
goto statement. A label is made of a valid identifier followed by a colon (:).
38
38
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
Answer:
The difference between "do while" and "do until" is that a "do while" loops while the test
case is true, whereas "do until" loops UNTIL the test case is true (which is equivalent to
looping while the test case is false).
The difference between a "do ...while" loop and a "while {} " loop is that the while loop
tests its condition before execution of the contents of the loop begins; the "do" loop tests
its condition after it's been executed at least once. As noted above, if the test condition is
false as the while loop is entered the block of code is never executed. Since the condition
is tested at the bottom of a do loop, its block of code is always executed at least once.
Program 1: Write a program to take a number as input and calculate the sum of its digits.
Solution: #include<conio.h>
#include<stdio.h>
void main()
int n,a,sum=0;
clrscr();
scanf("%d",&n);
while(n>0)
a=n%10 ;
sum=sum+a;
n=n/10;
39
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
getch( );
OUTPUT :
Program 2: Write a program to find smallest and largest among four numbers.
Solution: #include<stdio.h>
#include<conio.h>
void main ()
{
int a, b, c, d;
clrscr ();
printf ("Enter The Four Numbers Respectively : ");
scanf ("%d %d %d %d", &a, &b,&c, &d);
if (a>b && a>c && a>d)
printf ("%d Is The Largest Number\n", a);
else if (b>a && b>c && b>d)
printf ("%d Is The Largest Number\n", b);
else if (c>a && b<c && c>d)
printf ("%d Is The Largest Number\n", c);
else
printf ("%d Is The Largest Number\n", d);
if (a<b && a<c && a<d)
printf ("%d Is The Smallest Number\n", a);
else if (a>b && b<c && b<d)
printf ("%d Is The Smallest Number\n", b);
else if (c<b && a<c && c<d)
printf ("%d Is The Smallest Number\n", c);
else
printf ("%d Is The Smallest Number\n", d);
getch ();
}
OUTPUT
Enter The Four Numbers Respectively : 12 52 65 78
78 Is The Largest Number
12 Is The Smallest Number
40
Page
40
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
Solution: #include<stdio.h>
#include<conio.h>
void main ()
{
float bs, hra, da, ta, inc, bonus, gs, sales;
clrscr ();
printf ("Enter The Sales Earned : ");
scanf ("%f", &sales);
if (sales < 100000)
{
bs = 3000;
hra = 0.2 * 3000;
da = 1.10 * 3000;
ta = 500;
inc=0.1*sales;
bonus=1000;
}
else
{
bs = 3000;
hra = 0.2 * 3000;
da = 1.10 * 3000;
ta = 500;
inc=0.1*sales;
bonus=2000;
}
gs=bs+hra+da+ta+inc+bonus;
printf ("The Gross Salary of MR is : %f \n", gs);
getch ();
}
OUTPUT
Program 4: Write a program to convert year in to months, days, hours, minutes and seconds.
Solution: #include<stdio.h>
#include<conio.h>
void main ()
41
{
Page
41
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
2.Days
3.Hours
4.Minutes
5.Seconds
Program 5: Wap to find all pythagorian triplet for all odd number in range 1 to10.
Solution: #include<stdio.h>
#include<conio.h>
42
Page
42
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
#include<math.h>
void main()
{
int i,x;
clrscr();
for(i=1;i<10;i=i+2)
{
x=(pow(i,2)-1)/2;
printf("\nThe Pythagorian triplet for %d are \t%d %d %d", i, i, x, x+1);
}
getch();
}
OUTPUT
The Pythagorean Triplet for 1 are 1 0 1
The Pythagorean Triplet for 3 are 3 4 5
The Pythagorean Triplet for 5 are 5 12 13
The Pythagorean Triplet for 7 are 7 24 25
The Pythagorean Triplet for 9 are 9 40 41
43
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
Solution: #include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
double i, n, s=0, j, fact=1;
clrscr();
printf("Enter The Value Of n : ");
scanf("%lf",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
fact=fact*j;
s=s+(i/fact);
}
printf("The Sum Of The Series Is : %lf",s);
getch();}
OUTPUT
Enter The Value Of n : 5
44
44
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
Array:
An array is a fixed sized sequenced collection of related data items of same data type.
In its simplest form an array can be used to represent a list of numbers or a list of names
using a single name.
We can use an array name salary to represent a set of salaries of a group employees in an
organization. We can refer to the individual salaries by writing a number called index or
subscript in brackets after the array name. for example salary[10] represents the salary of
10th employee.
While the complete set of values referred to as an array, individual values are called
elements.
One-dimensional arrays
Two-dimensional arrays
multidimensional arrays
One-dimensional arrays :-
A list of items can be given one variable name using only one subscript and such a
variable is called a single-subscripted variable or one-dimensional array.
Like any other variable, arrays must be declared before they are used.
type variable_name[size];
Page
45
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
type specifies the type of elements of array, such as int, float or char and the size
indicates the maximum number of elements that can be stored inside the array.
For example :
float height[10];
Multidimensional arrays
Two-dimensional arrays
One-diemensional array can store a list of values. There could be situations where a table
of values will have to be stored.
C allows to define such table of items by using two-dimensional array such as v[3][4].
type array_name[row_size][column_size];
Each dimension of array is indexed from zero to its maximum size minus one, the first
index selects the row and the second index selects the column within that row.
46
Page
46
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
Multi-dimensional array
C allows arrays of three or more dimensions. The exact limit is determined by the
complier.
type array_name[s1][s2][s3[]……….[sm];
For example :
int a[3][4][12];
float table[5][4][5][3];
Solution: #include<stdio.h>
#include<conio.h>
void main( )
int a[10],sum=0,i;
scanf(―%d‖,&n);
for(i=0;i<n;i++)
scanf(―%d‖,&a[i]);
for(i=0;i<n;i++)
sum=sum+a[i];
47
Page
47
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
getch();
OUTPUT :
Answer: Strings
A string is sequence of characters that is treated as a single data item. Any group of
characters defined between double quotation marks is a string constant.
Declaring strings :
C does not support strings as a data type. However, it allows us to represent strings as
character arrays. In C therefore, a string variable is any valid C vaiable name and is
always declared as an array of characters.
char string_name[size];
For example :
char city[10];
#include <stdio.h>
main()
48
Page
48
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
printf("%s\n", name1);
printf("%s\n", name2);
Helloxghifghjkloqw30-=kl`'
Hello
The difference between the two arrays is that name2 has a null placed at the end of the
string, ie, in name2[5], whilst name1 has not. This can often result in garbage characters
being printed on the end. To insert a null at the end of the name1 array, the initialization
can be changed to,
The familiar input function scanf can be used with %s format specification to read in a
string of characters.
For example :
char address[15];
scanf(―%s‖,address);
the problem with the scanf function is that it terminates its input on the first white space it
finds.
49
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
A single character can be read from the keyboard using getchar. We can use this function
repeatedly to read successive single characters from the input and place them into a
character array. The reading is terminated when newline character (‗\n‘) is entered and
the null character is then inserted at the end of the string.
char ch;
ch=getchar( );
Another and more convenient method of reading a string of text containing whitespaces
is to use the library function gets available in the <stdio.h> header file. This is simple
function with one string parameter and called as under:
gets(str);
str is a string variable declared properly. It reads characters into str from the keyboard
until a newline character is encountered and then appends a null character to the string.
Unlike scanf, it does not skip whitespaces.
printf function with %s format specifier can be used to print strings to the screen. The
format %s can be used to display an array of characters that is terminated by the null
character.
For example :
char name[10];
printf(―%s‖,name);
Question 4: Write a program to read and display a line of text using getchar and putchar
Answer:#include <stdio.h>
#include <conio.h>
50
void main( )
Page
50
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
char str[ 30 ], c;
int i = 0;
{ str[ i ] = c;
i++; }
str[ i ] = '\0';
putchar( str [ i ] );
getch( ); }
OUTPUT :
Question 5: Write a program to copy one string into another without using any string
functions.
Answer:#include<stdio.h>
#include<conio.h>
void main( )
{char s1[15],s2[15];
int i;
clrscr();
51
Page
51
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
gets(s1);
for(i=0;s1[i]!='\0';i++)
s2[i]=s1[i];
s2[i]='\0';
getch();
OUTPUT :
manipulation functions.
For example :
char x=‗a‘;
printf(―%d‖,x);
52
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
For example :
int x;
x=‗z‘-1;
is a valid statement. The ASCII value of z is 122 and therefore the statement will assign
121 to x.
String-Handling Functions:
C library supports a large number of string-handling functions that can be used to carry
out many of the string manipulations.
Strcat() function
strcat(string1,string2);
It does so by removing the null character at the end of string1 and placing string2 from
there.
53
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
strcmp() function
The strcmp compares two functions identified by the arguments and has a value of 0 if
they are equal.
If they are not, it has numeric difference between the first nonmatching characters in the
strings.
strcmp(string1,string2);
strcpy() function
strcpy(string1,string2);
Strlen() function
n=strlen(string);
Where n is an integer variable, which receives the value of the length of the string.
strncpy(): it copies the left-most n characters of the sourse string to the target string variable.
Page
54
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
strncpy(string1,string2,n);
strncmp – this fuction has three parameter and has the following form :
strncmp(string1,string2,n);
and returns :
strncat(): This function concatenates the left-most n characters of second string to the end of
first string.
strncat(string1,string2,n);
strstr(string1,string2);
The function strstr searches the string1 to see whether the string2 is contained in string1.
if yes function returns the position of the first occurrence of the sub-string. Otherwise, it
returns a NULL pointer.
Answer: #include<stdio.h>
main()
{
int i=0,n[5],s=0,m[5],r=0;
55
Page
55
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
n[i]=n[i]/10;
}
if(m[i]==s)
{
printf("\nPalindrome :");
}
else
{
printf("\nNot palindrome :");
}
printf("%d",s);
s=0,r=0;
}
getch();
Question 8: Write a program in c language to find greatest and smallest element in one
dimensional array.
Answer:
#include<stdio.h>
56
int main()
Page
56
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
int l.n = 0;
int s.n = 9;
int i=0;
while(a[i] != '\0')
if(a[i]>= l.n)
l.n = a[i];
else if(a[i]<=s.n)
s.n = a[i]
i++;
printf("%d %d",s.n,l.n) ;
return(0);
Question 9: WAP to find largest number and smallest number from 10 integer number in
array.
Answer:#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,b,s;
clrscr();
for(i=0;i<10;i++)
{
57
57
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
scanf("%d",&a[i]);
}
b=a[0];
s=a[0];
for(i=1;i<10;i++)
{
if(a[i]>b)
b=a[i];
if(a[i]<s)
s=a[i];
}
printf("\nLargest Value In An Array Is : %d\nSmallest Value In An Array Is : %d",b,s);
getch();
}
OUTPUT
Enter 1 Element In An Array: 15 Enter 2 Element In An Array: 36
Enter 3 Element In An Array: 95 Enter 4 Element In An Array: 40
Enter 5 Element In An Array: 12 Enter 6 Element In An Array: 85
Enter 7 Element In An Array: 74 Enter 8 Element In An Array: 65
Enter 9 Element In An Array: 92 Enter 10 Element In An Array: 57
Question 10: Write a program to enter five number using array and rearrange it in reverse
order.
Solution: #include<stdio.h>
#include<conio.h>
void main()
{
int a[5],i;
clrscr();
for(i=0;i<5;i++)
{
printf("Enter %d Element : ",i+1);
scanf("%d",&a[i]);
}
printf("The Elements In Reverse Order Is: ");
for(i=4;i>=0;i--)
printf("%d, ",a[i]);
getch();
58
}
Page
58
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
OUTPUT
Enter 1 Element : 12
Enter 2 Element : 35
Enter 3 Element : 62
Enter 4 Element : 45
Enter 5 Element : 54
The Elements In Reverse Order Is: 54, 45, 62, 35, 12,
Answer: #include<stdio.h>
#include<conio.h>
void main()
{
int matrix[4][4],i,j,temp;
clrscr();
printf("Enter Elements of the 4*4 Matrix:");
for(i=0;i<4;i++)
for(j=0;j<4;j++)
scanf("%d",&matrix[i][j]);
for(i=0;i<4;i++)
for(j=0;j<4;j++)
if(j<i)
{
temp=matrix[i][j];
matrix[i][j]=matrix[j][i];
matrix[j][i]=temp;
}
printf("\nTransposed Matrix :\n\n");
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
printf("%d ",matrix[i][j]);
printf("\n");
}
getch();
}
OUTPUT
Enter Elements of the 4*4 Matrix:15 65 87 98 87 15 32 96 74 85 50 41 65 32
145 65 98
59
Transposed Matrix :
Page
59
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
15 87 74 65
65 15 85 32
87 32 50 145
98 96 41 65
Question 12: Write a prigram to add, substarct, multiply the two 3*3 matrix.
Answer: #include<stdio.h>
#include<conio.h>
void main()
{
int matrix1[3][3],matrix2[3][3],multi[3][3],i,j,k;
clrscr();
printf("Enter Elements of The First Matrix :");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&matrix1[i][j]);
printf("Enter Elements of The Second Matrix :");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%d",&matrix2[i][j]);
printf("\n\nMatrix Formed By Addition :\n\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
printf("%d ",matrix1[i][j]+matrix2[i][j]);
printf("\n");
}
printf("\n\nMatrix Formed By Substraction :\n\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
printf("%d ",matrix1[i][j]-matrix2[i][j]);
printf("\n");
}
printf("\n\nMatrix Formed By Multiplication :\n\n");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
multi[i][j]=0;
for(k=0;k<3;k++)
60
multi[i][j]+=matrix1[i][k]*matrix2[k][j]; }
Page
60
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
printf("%d ",multi[i][j]);
printf("\n");
}
getch();
}
OUTPUT
Enter Elements of The First Matrix :15 15 32 9875 51 25 32 32
12 32
Enter Elements of The Second Matrix :15 47 89 65 45 12 32 21 10 25 35
47 47 47
9922 140 90
77 44 44
-17 -17 17
9828 -38 -40
-13 20 -20
Question 13: Write a program to perform following sorting method in array with five element
by using
(a) Selection sort
(b) Bubble sort
clrscr();
Page
61
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
Sorted Array Is : 10 45 65 78 98
Bubble Sort:
#include<stdio.h>
#include<conio.h>
void main()
{
int array[5],i,j,temp;
clrscr();
printf("Enter Elements of the Array:");
for(i=0;i<5;i++)
scanf("%d",&array[i]);
for(i=0;i<5;i++)
for(j=0;j<5-i-1;j++)
if(array[j]>array[j+1])
{
temp=array[j];
array[j]=array[j+1];
array[j+1]=temp;
}
printf("\nSorted Array Is : ");
for(i=0;i<5;i++)
printf("%d ",array[i]);
62
getch(); }
Page
62
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
OUTPUT
Enter Elements of the Array:45 65 98 78 10
Sorted Array Is : 10 45 65 78 98
Question 14: Write a program to perform following searching method in array with 5 element
Linear search & binary search.
#include<stdio.h>
#include<conio.h>
void main()
{
int array[5],i,j,element;
clrscr();
printf("Enter Elements of the Array :");
for(i=0;i<5;i++)
scanf("%d",&array[i]);
printf("Enter Element to search: ");
scanf("%d",&element);
for(i=0;i<5;i++)
if(array[i]==element)
{
printf("\nElement found at Array index %d ",i);
break;
}
if(i==5)
printf("\nElement not found");
getch();
}
OUTPUT
Enter Elements of the Array :5 9 8 2 1
Enter Element to search: 1
int x[5],y[5],s,m=0,e=4,b=0,i,mid,j,t;
Page
63
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
clrscr();
for(i=0;i<5;i++)
{
printf("Enter value %d:",i+1);
scanf("%d",&y[i]);
x[i]=y[i];
}
for(i=0;i<4;i++)
{
for(j=0;j<4-i;j++)
if(x[j]>x[j+1])
{
t=x[j];
x[j]=x[j+1];
x[j+1]=t;
}
}
printf("\nEnter number to search:");
scanf("%d",&s);
while(b<5)
{
mid=(b+e)/2;
if(x[mid]<s)
b=mid+1;
else if(x[mid]>s)
e=mid-1;
else
{
m=1;
break;
}
}
for(i=0;i<5;i++)
{if(x[mid]==y[i])
break; }
if(m==1)
printf("\nIt exists in position %d",i+1);
else
printf("\nIt does not exists");
getch();
}
OUTPUT
64
Page
64
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
It exists in position 5
Question 15: Write a program to check string is palindrome or not using loop.
Answer: #include<stdio.h>
#include<conio.h>
void main()
{
char a[100],b[100];
int i=0,k=0,j=0,len=0;
clrscr();
printf("Enter The String : ");
scanf("%[^\n]s",a);
while(a[i]!='\0')
{
len++;
i++;
}
for(k=i-1;k>=0;k--)
{
b[i-(k+1)]=a[k];
}
i=0;
while(a[i]!='\0')
{
if(a[i]==b[i])
j++;
i++;
}
if(len==j)
printf("Palindrome\n");
else
printf("Not Palindrome\n");
getch();
65
}
Page
65
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
OUTPUT
Enter The String : RADAR
Palindrome
Question 16: Write a program to compare two string. (ignore case) using loop.
Answer: #include<stdio.h>
#include<conio.h>
void main()
{
char a[25],b[25];
int i=0,j=0,len=0,len1=0,k=0;
clrscr();
printf("Enter The First String: ");
gets(a);
printf("Enter The Second String: ");
gets(b);
while(a[i]!='\0')
{
len++;
i++;
}
i=0;
while(b[i]!='\0')
{
len1++;
i++;
}
if(len==len1)
{
while(a[j]!='\0')
{
if(a[j]==b[j]||a[j]==b[j]+32||a[j]==b[j]-32)
k++;
j++;
}
if(k==len)
printf("Both Strings Are Same\n");
}
else
printf("\nBoth Strings Are Not Same");
getch();
66
}
Page
66
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
OUTPUT
Answer: #include<stdio.h>
#include<conio.h>
void main()
{
char a[25],b[25];
int i=0,j=0,len=0,len1=0,k=0;
clrscr();
printf("Enter The First String: ");
gets(a);
printf("Enter The Second String: ");
gets(b);
while(a[i]!='\0')
{
len++;
i++;
}
i=0;
while(b[i]!='\0')
{
len1++;
i++;
}
if(len==len1)
{
while(a[j]!='\0')
{
if(a[j]==b[j]&&(a[j]==b[j]+32||a[j]==b[j]-32))
k++;
j++;
}
if(k==len)
printf("Both Strings Are Same\n");
else
printf("Both Strings Are Not Same\n");
67
}
Page
67
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
else
printf("\nBoth Strings Are Not Same");
getch(); }
OUTPUT
Enter The First String: i-GATE
Enter The Second String: I-GATE
Both Strings Are Not Same
Question 18: Write a program that will read a line and delete all vowel from sentence.Assume
that sentences is not more than 80 char long.
Answer: #include<stdio.h>
#include<conio.h>
void main()
{
char a[100];
int i=0,len=0;
clrscr();
printf("Enter The String: ");
gets(a);
while(a[i]!='\0')
{
len++;
i++;
}
printf("The String After Deletion Of Vowel: ");
i=0;
while(i<=len)
{
if(a[i]!='a')
if(a[i]!='e')
if(a[i]!='i')
if(a[i]!='o')
if(a[i]!='u')
if(a[i]!='A')
if(a[i]!='E')
if(a[i]!='I')
if(a[i]!='O')
if(a[i]!='U')
printf("%c",a[i]);
i++;
68
}
Page
68
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
getch();
}
OUTPUT
Enter The String: PRESENTLY WE ARE DOING THE C LABWORK
The String After Deletion Of Vowel: PRSNTLY W R DNG TH C LBWRK
Question 19: Write a program to count frequency of all character in input string.
Answer: #include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char string[100], ch;
int c = 0, count[26] = {0};
clrscr();
printf("Enter a string\n");
gets(string);
c++;
}
getch();
}
OUTPUT
Enter a string :=> It Is My First Practical Class On C Lab
a occurs 4 times in the entered string.
b occurs 1 times in the entered string.
69
69
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
Question 20: Write a program to count no. of vowels , consonant , blank in input string.
70
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
OUTPUT
Enter a String :=> Saturday Is The Last Day For Submission Of Soft Copy Of C
Lab
Vowels = 17
Consonant = 32
Whitspace = 12
71
Page
71
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
Answer:
Once a function has been designed and packed, it can be treated as a black box that takes
some data from the main program and returns a value. The inner details of the operation
are invisible to the rest of the program. All that the program knows about a function is :
What goes in and what comes out.
Every C program can be designed using a collection of these black boxes known as
functions.
Prototypes:- are syntactically distinguished from the old style of function declaration.
The two styles can be mixed for any single function, but this is not recommended. The
following is a comparison of the old and the prototype styles of declaration:
Old style:
Prototype style:
Functions are declared explicitly with a prototype before they are called. Multiple
declarations must be compatible; parameter types must agree exactly.
72
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
The number and type of arguments are checked against the prototype and must agree with
or be convertible to the declared types. Empty parameter lists are designated using
the void keyword.
Ellipses are used in the parameter list of a prototype to indicate that a variable number of
parameters are expected.
Prototype Syntax
function-prototype-declaration:
Library functions :
This functions are built-in functions and not required to be written by user.
Used-Defined functions :
It has to be developed by the user at the time of writing a program. However it can later
become a part of
example : main( ).
In this programming style, the high level logic of the overall problem is solved first
while the details of each lower-level functions are addressed later.
The length of a source program can be reduced by using functions at appropriate places.
73
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
A function may be used by many other programs. In order to make use of a user-defined
function, we need to establish three elements that are related to functions :
Function definition
Function call
Function declaration
In order to use this function we need to invoke it at a required place in the program. This
is known as function call. The program (or a function) that calls the function is
referred to as the calling program or calling function.
The calling program should declare any function (like declaration of a variable) that is to
be used later in the program. This is known as function declaration or function prototype.
Function declaration :
Like variables, all functions in C program must be declared, before they are invoked. A
function declaration also known as function prototype consist of four parts :
Function type
Function name
Parameter list
Terminating semicolon
Definition of a function
A function definition, also known as function implementation shall include the following
elements :
Function name
Function type
74
Page
74
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
List of parameters
Function statements
A return statement
Function header
Function body
executable statement1;
executable statement2;
……………………….
………………………
return statement;
Function call
A function can be called by simply using the function name followed by a list of actual
parameters (or arguments), if any, enclosed in parentheses.
Function_name(parameter list);
Question 2: Define value return by functions and their Different categories of functions.
A function may or may not send back any value to the calling function.
75
Page
75
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
A called function can return only one value per call at the most.
return;
OR
return(expession);
The plane return does not return any value to the calling function, it acts much as the
closing brace of the function.
When a return is encountered the control is immediately passed back to the calling
function.
For example :
mul(x,y)
int x,y;
int p;
p=x*y;
return(p);
If we do not specify any return type then by default compiler assumes it as int.
Answer:
Page
76
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
(i)Nesting of functions
C permits nesting of functions freely. main can call function1, which calls function2,
which calls function3,………… and so on. There is in principle no limit as to how deeply
functions can be nested.
Fun1( )
………….
……….
Fun2( )
……….
……….
Fun2( )
…………
……….
Fun3( )
…………..
Call by value means programmer send some value coping from one function to
another.At the time of function calling aprogrammer can send a copy of variable of value.
77
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
void main()
int x=10,y=20;
printf("%d%d',x,y);
int c;
b=c;
(iii)call by referance :
it means sending sending the address of variable to the called function means a user can
send the address of variable.
swap(&a,&b)
&c=&a;
&a=&b;
&b=&c;
78
Page
78
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
Answer:
When a called function in turn calls another function a process of chaining occurs.
Recursion is a special case of this process where a function calls itself.
For example :
void main( )
main( );
When we write recursive functions, we must have an if statement to force the function to
return without the recursive call being executed. Otherwise, the function will never
return.
Question 5: Write a program to print the following Fibonacci series using recursive
function :
1 1 2 3 5 8 13 ……………………………………..
Answer:
#include<stdio.h>
#include<conio.h>
int count=0,a=1,b=1,n,c,f;
void main()
79
Page
79
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
int sum;
int febo(int,int);
clrscr();
scanf("%d",&n);
printf("%d\t%d\t",a,b);
c=febo(a,b);
getch();
febo(int a,int b)
if(count==n-2)
return c;
else {
count++;
c=a+b;
printf("%d\t",c);
febo(b,c);
}}
OUTPUT :
1 1 2 3 5 8
80
Page
80
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
Answer:
To pass a one-dimensional array to a called function, it is sufficient to list the name of the
array, without any subscripts and the size of the array as arguments.
For example :
Calling : largest(a,n)
The function must be called by passing only the name of the array.
2. In the function definition, the formal parameter must be an array type, the size of the
array does not need to be specified.
81
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
In the function definition, we must indicate that the array has two dimensions by
including two sets of brackets.
Question 7:
(i)What is structure ?
(ii) Write a program to define a structure student that will contain the roll number, name
and total marks of a student the program will ask the user to input details of 5 students
and print the details of all the students whose total marks is greater then a given value.
Answer:
(i) Structure:
Defining a structure
Structures must be defined first for their format that may be used later to declare structure
variable. The general format of a structure definition is as follows :
struct tag_name
82
Page
82
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
data_type member1;
data_type member2;
……………………
……………………};
The tag_name can be used to declare structure variables of its type, later in program.
A structure variable declaration is similar to the declaration of variables of any other data
types.
A terminating semicolon.
For example :
Struct Student{
Char name[10];
int rno;
83
char sec; };
Page
83
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
The members of a structure themselves are not variables. They do not occupy any
memory until they are associated with the structure variables such as student.
When the complier comes across a declaration statement, it reserves memory space for
the structure variables.
It is also allowed to combine both the structure definition and variables in one statement.
The members themselves are not variables, they should be linked to the structure
variables in order to make them meaningful members.
The link between a member and a variable is established using member operator ‗ . ‗,
which is also known as dot operator or period operator.
Structure initialization
Initializer lists
Example:
Assignment statements
Example:
s2=s1;
s2.rno=20;
Structure Comparison
We can compare values of members of same structure just like as ordinary variables.
For example :
84
struct student
Page
84
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
int roll_no;
char name[20];
float per;
};
if(s1.per>=s2.per)
…………………..
………………….
#include<conio.h>
void main( )
struct student
int roll;
char name[10];
float marks;
}st[5];
85
Page
85
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
int i;
for(i=0;i<=5;i++)
scanf(―%d%s%f‖,&st[i].roll,st[i].name,&st[i].marks);
for(i=0;i<=5;i++)
if(st[i].marks>65)
printf(―%d%s%f‖,st[i].roll,st[i].name,st[i].marks);
getch( );
In such cases we may declare an array of structures, each element of the array
representing a structure variable.
For example :
struct student
86
Page
86
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
int roll_no;
char name[20];
};
Answer : Union
Unions are a concept borrowed from structures and therefore follow the same syntax as
structure. However, there is a major difference between them in terms of storage. In
structures, each member has its own storage location, whereas all the members of the
union use the same location. This implies that although a union may contain many
members of different types, it can handle only one member at a time.
Union
union Number
int x; float y;
87
};
Page
87
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
The size of the structure is sum of the size of each member in the struchture.but sizeof the
union is size of Largest member in the union because union members are overlaps on
each other in memory.
struct tag_name
{
data type member1;
data type member2;
…
…
}
Example of Structure:
struct lib_booker
{
char titles[20];
char authors[15];
int page;
float prices;
88
};
Page
88
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
code;
Question 9: Discuss Method of Defining structure inside another structure and give proper
example.
Answer: This is a discussion on Defining structure inside another structure within the C
Programming forums, part of the General Programming Boards category; for example:
struct IDType
char zone;
char id[10];
struct Record
char x[30];
int y,z,f;
}
89
Page
89
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
operation.
Answer: Pointer:
Pointer is a derived data type in C. it is built from one of the fundamental data types
available in C. A pointer variable is a variable that contains an address which is the
location of another variable in memory. Since these memory addresses are the locations
in the computer memory where program instructions and data are stored, pointers can be
used to access and manipulate data stored in the memory.
In C every variable must be declared by its type. Since pointer variables contain
addresses that belong to a separate type, they must be declared as pointers before we use
them. The declaration of a pointer variable takes the following form :
data_type *pt_name;
This tells the compiler three things about the variable pt_name : The asterisk ( * ) tells
that the variable pt_name is a pointer variable. pt_name needs a memory location.
pt_name points to a variable of type data type.
Initialization of pointers
The actual location of a variable in the memory is system dependent and therefore, the
90
90
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
This can be done with the help of the operator & (address of) available in C.
The operator & immediately preceding a variable returns the address of the variable
associated with it.
P=&quantity;
Once a pointer has been assigned the address of a variable, the question remains as to
how to access the value of the variable using the pointer?
This is done by using a unary operator asterisk ( * ), usually known as the indirection
operator, another name for this operator is the dereferencing operator or it can be
remembered as value at address.
Pointer Arithmetic:-
Operations meaningless unless performed on an array Five element int array on machine
with 4 byte ints vPtr points to first element v[ 0 ] at location 3000 (vPtr = 3000)
vPtr points to v[ 2 ] (incremented by 2), but the machine has 4 byte ints, so it points to
address 3008
Subtracting pointers
vPtr2 = v[ 2 ];
vPtr = v[ 0 ];
91
Page
91
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
See which pointer points to the higher numbered array element Also, see if a pointer
points to 0
It is declare as
Example:
char ch=‘c‘;
Lets suppose we have a pointer ‗p1′ that points to yet another pointer ‗p2′ that points to a
92
92
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
p2=& c;
p1=& p2;
So we can see that in memory, pointer p1 holds the address of pointer p2. Pointer p2
holds the address of character ‗ch‘.
So ‗p2′ is pointer to character ‗ch‘, while ‗p1′ is pointer to ‗p2′ or we can also say that
‗p2′ is a pointer to pointer to character ‗ch‘.
p2=&ch;
char **p1=&p2;
So we see that ‗p1′ is a double pointer (ie pointer to a pointer to a character) and hence
the two *s in declaration.
Now,
{
Page
93
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
Answer:
<type> *<name>[<number-of-elements>];
Example:
Char *ptr[3];
#include<stdio.h>
void main()
{
94
char *p1=‖i-Gate ‖;
Page
94
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
char *p2=‖Bhilai‖;
char *p3=‖SSS ‖;
char *arr[3];
int i;
for(i=0;i<3;i++)
getch();
For example :
The above line declares a function pointer ‗fptr‘ that can point to a function whose return
type is ‗int‘ and takes two integers as arguments.
#include<stdio.h>
{
95
Page
95
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
void main()
func(2, 3);
fptr(2, 3);
getch();
Output: 2 3
2 3
Question 5: What is dynamic memory Allocation? Explain with example. Write down
Answer:
The exact size of array is unknown untill the compile time,i.e., time when a
compier compiles code written in a programming language into a executable form.
The size of array you have declared initially can be sometimes insufficient and
sometimes more than required.
Dynamic memory allocation allows a program to obtain more memory space,
while running or to release space when no space is required.
Although, C language inherently does not has any technique to allocated memory
dynamically, there are 4 library functions under "stdlib.h" for dynamic memory
96
allocation.
Page
96
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
malloc()
It is used to allocate single block of memory of specified size and return a pointer of type
void which can be type casted into pointer of any type.
Syntax of malloc()
ptr=(cast-type*) malloc(byte-sze);
Exaple:
int *p;
p=(int *)malloc(10* sizeof(int));
This statement will allocate 20 bytes respectively and the pointer points to the
address of first byte of memory.
calloc()
It is used to allocate multi block of memory of specified size and return a pointer of type
void which can be type casted into pointer of any type.
Syntax of calloc()
ptr=(cast-type*) calloc(n, element-sze);
Exaple:
int *p;
p=(int *)calloc(10, sizeof(int));
This statement will allocate 20 bytes respectively and the pointer points to the
address of first byte of memory.
free()
Dynamically allocated memory with either calloc() or malloc() does not get return
on its own. The programmer must use free() explicitly to release space.
97
syntax of free()
Page
97
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
free(ptr);
This statement cause the space in memory pointer by ptr to be deallocated.
Examples of calloc() and malloc()
Write a C program to find sum of n elements entered by user. To perform this
program, allocate memory dynamically using malloc() function.
#include <stdio.h>
#include <stdlib.h>
int main(){
int n,i,*ptr,sum=0;
printf("Enter number of elements: ");
scanf("%d",&n);
ptr=(int*)malloc(n*sizeof(int)); //memory allocated using malloc
if(ptr==NULL)
{
printf("Error! memory not allocated.");
exit(0);
}
printf("Enter elements of array: ");
for(i=0;i<n;++i)
{
scanf("%d",ptr+i);
sum+=*(ptr+i);
}
printf("Sum=%d",sum);
free(ptr);
return 0;
}
98
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
exit(0);
}
printf("Enter elements of array: ");
for(i=0;i<n;++i)
{
scanf("%d",ptr+i);
sum+=*(ptr+i);
}
printf("Sum=%d",sum);
free(ptr);
return 0;
}
realloc()
If the previously allocated memory is insufficient or more than sufficient. Then,
you can change memory size previously allocated using realloc().
Syntax of realloc()
ptr=realloc(ptr,newsize);
99
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
Question 6: What is FILE? Define various file handling function fopen(), fclose() , fprintf()
, fscanf() .
Answer:
A file is a place on disk where the group of related data is stored .Like most other
language, C supports number of functions that have the ability to perform basic file
operations, which includes:
naming a file.
Opening a file.
Reading data from a file.
Writing data to a file .
Closing a file.
There are two distinct way to perform file operation in C. The first one is known as low
level I/O and uses UNIX system calls. The second method is referred to as the high level
I/O operation and uses functions in C‘s standard I/O library.
100
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
fopen() Creates new file for use. Opens an existing file for use.
Data structure of a file is defined as FILE in the library of standard I/O function
definitions. Therefore all the file should be declared as type FILE before they are used.
When we open a file, we must specify what we want to do with the file. For example we
may write data to the file or read the already existing data.
FILE *fp;
Fp = fopen(“filename”, “mode”);
101
The first statement declares the variable fp as a ―pointer to the data type FILE‖
Page
101
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
The second statement opens the file named filename and assigns an identifier to the
r+ The existing file is opened to the beginning for both reading and writing
When trying to open a file, one of the following things may happen:
When the mode is ‗writing‘, a file with the specified name is created if the file does not
exist. The content are deleted if the file already exists.
When the purpose is ‗appending‘, the file is opened with the current contents safe. A file
with the specified name is created if the file does not exist.
If the purpose is ‗reading‘ , and if it exists, then the file is opened with the current
contents safe; otherwise an error occurs.
CLOSING A FILE:
A file must be closed as soon as all operations on it have been completed. This ensures
that all outstanding information associated with the file is flushed out from the buffers
and all links to the file are broken.
fclose(file_pointer);
This would close the file associated with the FILE pointer file_pointer.
102
102
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
…………
…………
p1 = fopen(―INPUT‖, ―w‖);
p2 = fopen(―OUTPUT‖, ―r‖);
…………
………….
fclose(p1);
fclose(p2);
………..
This program opens two files and closes them after all operations on them are completed.
Once a file is opened , reading out or writing to it is accomplished by using the standard
I/O routines.
The simplest file I/O function are getc() and putc(). These are analogous to getchar and
putchar function and handle one character at a time. Assume that a file is opened with
mode wand file pointer fp1. Then statement
putc(c,fp1);
Writes the character contained in character variable c to file associated with FILE
pointer fp1.
Similarly getc() is used to read a character from a file that has been opened in read mode.
103
The statement
Page
103
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
c = getc(fp2);
Would read a character from the file whose file pointer is fp2.
The getw and putw are integer oriented functions. They are similar to the getc and putc
functions and are used to read and write integer values. These function would be useful
when we deal with only integer data. The general forms of getw and putw are:
putw(integer, fp);
getw(fp);
Most complier support two other functions, namely fprintf and fscanf that can handle a
group of mixed data simultaneously.
The function fprintf and fscanf performsI/O operation that are similar to printf and scanf
function, except they work on files.
Where fp is the file pointer associated with the file that has been opened for writing.
The control string contains output specification for the items in the list
For example:
For example:
Page
104
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
Like scanf , fscanf also returns the number of items that are successfully read. When the
end of file is reached, it returns the value EOF.
#include <stdio.h>
void main()
int c ;
c = getc( fp ) ;
while ( c != EOF )
putchar( c );
c = getc ( fp );
fclose( fp );
105
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
Trying to perform an operation on a file, when the file is opened for another type
of information.
Opening a file with an invalid file name.
Attempting to write to a write protected file.
If we fail to check such read and write errors, a program may behave abnormally
when an error occurs.
An unchecked error may result in a premature termination of program or incorrect
output.
C has two library functions feof and ferror that can help us to detect I/O errors.
If fp is a pointer to a file that has just been opened for reading :
if(feof(fp))
printf(―End of data‖);
It returns a nonzero integer if an error has been detected upto that point, during
processing.
tf(ferror(fp)!=0)
If the file cannot be opened for some reason, then the function returns null pointer.
This facility can be used to test whether a file has been opened or not.
if(fp= =NULL)
Answer:
106
Page
106
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
ftell : Takes a file pointer and returns a number of type long, that corresponds to the
current position. This function is useful in saving the current position of the file,
which can be used later in program.
n = ftell(fp);
rewind : This function is used to take the file pointer and resets the position to the
start of the .The statement:
rewind(fp);
n = ftell(fp);
fseek: This function is used to move the file position to a desired location within the
file. It takes the following form.
fp is a file pointer to the file concerned offset is a number or variable of type long.
position is an interger no.The position can take one of the following three values.
Value Meaning
1 Current position
107
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
Answer:
It is a parameter supplied to a program when the program is invoked. This parameter may
represent filename the program should process. For example, if we want to execute a
program to copy the contents of file named X_FILE to another one named Y_FILE , then
we may use command line like-
PROGRAM is the filename where the executable code of the program is stored. This
eliminates the need for the to request the user to enter the filenames during execution.
main can take two arguments called argc and argv and the information contained in the
command line is passed on to the program through these arguments, when main is called
up by the system.
The variable argc is an argument counter that counts the number of arguments on the
command line. The argv is an argument vector and represent s an array of character
pointers that point to the command line arguments. The size of this array will be equal to
the value of argc.
argv[0] PROGRAM
argv[1] X_FILE
argv[2] Y_FILE
In order to access the command line arguments, we must declare the main function and
its parameter as follows:
main(argc, argv)
int argc;
char *argv[];
{
108
………….
Page
108
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
………….
#include <stdio.h>
void main()
int c ;
char filename[40] ;
gets( filename ) ;
c = getc( fp ) ;
while ( c != EOF )
putchar(c);
c = getc ( fp ); }
fclose( fp );
Question 10: Write a program to count the number of lines and characters in a file.
Answer: Each line of input from a file or keyboard will be terminated by the newline character
‗\n‘. Thus by counting newlines we know how many lines there are in our input.
109
109
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
#include <stdio.h>
void main()
char filename[40] ;
nlines = 0 ;
nc = 0;
gets( filename );
if ( fp == NULL )
c = getc( fp ) ;
while ( c != EOF )
if ( c == ‗\n‘ )
110
nlines++ ;
Page
110
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
nc++ ;
c = getc ( fp );
fclose( fp );
if ( nc != 0 )
else
Question 11: Write a program to display file contents 20 lines at a time. The program
pauses after displaying 20 lines until the user presses either Q to quit or Return to
#include <stdio.h>
void main()
int c , linecount;
111
111
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
gets( filename );
linecount = 1 ;
reply[0] = ‗\0‘ ;
if ( c == ‗\n‘ )
linecount = linecount+ 1 ;
if ( linecount == 20 )
linecount = 1 ;
gets( reply ) ;
}
112
c = getc ( fp );
Page
112
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
fclose( fp );
Question 12: Write a program to compare two files specified by the user, displaying a
#include <stdio.h>
void main()
gets(fname1);
gets(fname2);
113
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
ca = getc( fp1 ) ;
cb = getc( fp2 ) ;
ca = getc( fp1 ) ;
cb = getc( fp2 ) ;
if ( ca == cb )
else if ( ca != cb )
fclose ( fp1 );
fclose ( fp2 );
114
}
Page
114
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
Answer:
The first parameter in the command line is always the program name and therefore
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<ctype.h>
void main()
FILE *fs,*ft;
char ch;
fs=fopen("al.cpp","r");
if(fs==NULL)
exit(0);
ft=fopen("a2.cpp","w");
if(ft==NULL)
115
115
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
fclose(fs);
exit(0);
while(1)
ch=fgetc(fs);
if(ch==EOF)
break;
else
fputc(ch,ft);
fclose(fs);
fclose(ft);
getch();
116
Page
116
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
Question n Answer
Aim – Write a program to print value one less than & One More Than The Given Value & Print
their Sum .
Code –
#include<stdio.h>
#include<conio.h>
void main ()
{
int a,b,c,sum;
clrscr();
printf("Enter The Value \t");
scanf("%d",&a);
b= a-1;
c= a+1;
sum=a+b+c;
printf("\nThe Value One less Than The Given Value %d",b);
printf("\nThe Value One Greater Than The Given Value %d",c);
printf("\nThe Value Of Their Sum Is %d",sum);
getch();
}
OUTPUT
Enter The Value 8
The Value One Less Than The Given Value 7
The Value One Greater Than The Given Value 9
The Value Of Their Sum Is 24
#include<stdio.h>
#include<conio.h>
void main ()
{
int num, a, b, c, num1;
clrscr ();
printf ("Enter The Four Digit Number : ");
scanf ("%d", &num);
a=num%10;
117
num=num/10;
b=num%10;
Page
117
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
num=num/10;
c=num%10;
num=num/10;
num1=a*1000+b*100+c*10+num;
printf ("The Reversed Four Digit Number Is : %d", num1);
getch ();
}
OUTPUT
Enter The Four Digit Number : 1256
The Reversed Four Digit Number Is: 6521
Aim – Write a program to find the first day of the any year.
Code -
#include<stdio.h>
#include<conio.h>
void main()
{
int year, leapdays, days;
clrscr ();
printf ("Enter The Year To Find First Day (Year>1900) : ");
scanf ("%d", &year);
leapdays=(year-1900)/4;
days=leapdays+(year-1900);
days=days%7;
switch (days)
{ case 0:printf ("Monday\n");
break;
case 1:printf ("Tuesday\n");
break;
case 2:printf ("Wednesday\n");
break;
case 3:printf ("Thursday\n");
break;
case 4:printf ("Friday\n");
break;
case 5:printf ("Saturday\n");
break;
case 6:printf ("Sunday\n");
break;
}
118
getch ();
}
Page
118
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
OUTPUT
Aim – Write a program to reverse a 5-digit number and check whether the reversed and original
numbers are Equal
Code -
#include<stdio.h>
#include<conio.h>
void main ()
{
long int num, a, b, c, d, e, num1;
clrscr ();
printf ("Enter The 5 Digit Number : ");
scanf ("%ld", &num);
e=num;
a=num%10;
num=num/10;
b=num%10;
num=num/10;
c=num%10;
num=num/10;
d=num%10;
num=num/10;
num1=a*10000+b*1000+c*100+d*10+num;
printf ("The Reversed Number Is : %ld \n", num1);
if (e==num1)
printf ("The Entered and Reversed Numbers Are Equal\n");
else
printf ("The Entered and Reversed Numbers Are Not equal\n");
getch ();
}
OUTPUT
119
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
OUTPUT
120
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
#include<stdio.h>
#include<conio.h>
void main()
{
char a;
clrscr ();
printf ("Enter The Character : ");
a=getchar ();
if (a>=65 && a<=90)
printf ("Entered Character Is a Upper Case Letter\n");
else if (a>=97 && a<=122)
printf ("Entered Character Is a Lower Case Letter\n");
else if (a>=48 && a<=57)
printf ("Entered Charcter Is a Number\n");
else
printf ("Entered Character Is a Special Symbol\n");
getch ();
}
OUTPUT
Enter The Character : #
Entered Character Is a Special Symbol
#include<stdio.h>
#include<conio.h>
void main()
{
char sex, health, place;
int age;
clrscr ();
printf ("Enter Gender (M \ F) : ");
scanf("%c",&sex);
printf("Enter Health Condition (E \ P) : ");
scanf("%c",&health);
printf("Enter Place C for City or V for Village : ");
scanf ("%c", &place);
121
121
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
#include<stdio.h>
#include<conio.h>
122
void main()
{
Page
122
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
int i, n;
clrscr();
printf(―\n Enter n= ‖);
scenf(―%d‖, &n);
for(i=1;i<=50;i++)
printf("%d \t",i);
getch();
}
OUTPUT
1 2 3 4 5 6 7 8 9 10 11 12
13 14 15 16 17 18 19 20 21 22 23 24
…..
Aim - Write a program to count number between 1-100 not Divisible by 2,3,5
Code
#include<stdio.h>
#include<conio.h>
void main()
{
int i, x=0;
clrscr();
printf("The Numbers From 1 To 100 Not Divisible By 2 , 3 & 5 Are : \n");
for(i=1;i<=100;i++)
{
if(i%2!=0 && i%3!=0 && i%5!=0)
{
printf(" %d\n",i);
x++;
}
}
printf("\nTotal No Which Are Not Divisible By 2 , 3 & 5 Are : %d",x);
getch();
}
OUTPUT
123
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
void main()
{
int i, n;
clrscr();
printf("Enter The Number Upto Which The Even Numbers To Print : ");
scanf("%d",&n);
printf("The Even Numbers Upto %d Is : \n",n);
for(i=1;i<=n;i++)
{
if(i%2==0)
printf("%d\t",i);
}
getch();
}
OUTPUT
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int i,r,s,a;
clrscr();
printf("The Armstrong Number From 1 - 500 : \n");
for(i=1;i<=500;i++)
{
124
a=i;
r=a%10;
Page
124
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
a=a/10;
s=a%10;
a=a/10;
if(pow(r,3)+pow(s,3)+pow(a,3)==i)
printf("%d\t",i);
}
getch();
}
OUTPUT
The Armstrong Number From 1 – 500:
1 153 370 371 407
Aim - wap for a matchstick game being played between the computer and auser. Your
#include<stdio.h>
#include<conio.h>
void main()
{
int matchstick =21,user,computer;
clrscr();
printf("\nDont enter invalid number 1,2,3,4");
for(;matchstick>=1;)
{
printf("\n no .of avail matchstick %d ",matchstick);
printf("\n your turn pick ");
scanf("%d",&user);
if(user>4)
{
printf("invalid");
break;
}
computer = 5 - user;
printf("\ncomputer turn");
if(matchstick==1)
break;
printf("\n computer choose %d",computer);
matchstick=matchstick-user-computer;
125
continue;
}
Page
125
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
matchstick--;
printf("computer win");
getch();
}
OUTPUT
computer turn
computer choose 3
no .of avail matchstick 16
your turn pick 1
computer turn
computer choose 4
no .of avail matchstick 11
your turn pick 3
computer turn
computer choose 2
no .of avail matchstick 6
your turn pick 1
computer turn
computer choose 4
no .of avail matchstick 1
your turn pick 1
computer turn
computer win
Aim - Write a program to find all pythagorian triplet for all odd number in range 1 to10.
Code –
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
126
int i,x;
clrscr();
Page
126
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
for(i=1;i<10;i=i+2)
{
x=(pow(i,2)-1)/2;
printf("\nThe Pythagorian triplet for %d are \t%d %d %d",i,i,x,x+1);
}
getch();
}
OUTPUT
{
h=0;
Page
127
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
m=0;
s=0;
}
delay(1000);
s=s+1;
clrscr();
printf("\n DIGITAL CLOCK");
printf("\n HOUR:MINUTE:SECOND");
printf("\n%d:%d:%d",h,m,s);
}
getch();
}
OUTPUT
DIGITAL CLOCK
HOUR:MINUTE:SECOND
0:05:35
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j;
clrscr();
printf("Enter The Length Of The Series : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
printf("*");
printf("\n");
}
getch();
128
}
Page
128
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
OUTPUT
Enter The Length Of The Series : 6
*
**
***
****
*****
*******
Aim- To Print Series
1
12
123
1 2 3 4( up to n)
Code-
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j, n;
clrscr();
printf("Enter The Length Of The Series : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
printf("%d ",j);
printf("\n");
}
getch();
}
OUTPUT
129
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j, n,x=1;
char a='a';
clrscr();
printf("Enter The Length Of The Series : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(i%2==0)
{
for(j=1;j<=i;j++)
{
printf("%c ",a);
a++;
if(a>122)
a=97;
}
}
else
{
for(j=1;j<=i;j++)
{
printf("%d ",x);
x++;
}
}
printf("\n");
}
getch();
}
130
OUTPUT
Page
130
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
1
ab
234
abcd
56789
ef g hi j
Code-
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n;
clrscr();
printf("Enter The Length Of The Series : ");
scanf("%d",&n);
for(i=n;i>=1;i--)
{
for(j=i;j>=1;j--)
printf("* ");
printf("\n");
}
getch();
}
OUTPUT Enter The Length Of The Series : 6
******
*****
****
***
131
**
*
Page
131
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
#include<conio.h>
void main()
Page
132
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
{
int i, j, n;
clrscr();
printf("Enter The Length Of The Series : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
printf("%d ",j);
printf("\n");
}
for(i=n;i>=1;i--)
{
for(j=i;j>=1;j--)
printf("%d ",j);
printf("\n");
}
getch();
}
OUTPUT
1
12
123
1234
4321
321
32
1
Code -
#include<stdio.h>
#include<conio.h>
133
void main()
{
Page
133
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
int i, j, k, l, n;
clrscr();
printf("Enter The Length Of The Series : ");
scanf("%d",&n);
for(i=0;i<=n;i++)
{
for(j=n-1;j>=i;j--)
printf(" ");
for(k=i;k>=0;k--)
printf("%d ",k);
for(l=i-1;l>=0;l--)
printf("%d ",i-l);
printf("\n");
}
getch();
}
Code –
#include<stdio.h>
#include<conio.h>
void main ()
{
int i, j, a=1, k;
clrscr();
for(i=1;i<=4;i++)
{
for(j=4;j>=i;j--)
printf(" ");
for(k=1;k<=i;k++)
{
printf("%d ",a);
a++;
134
}
printf("\n");
Page
134
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
}
getch();
}
Aim- WAP to take 10 integer number in array and display it with their address.
Code-
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i;
clrscr();
for(i=0;i<10;i++)
{
printf("Enter %d Element In An Array:",i+1);
scanf("%d",&a[i]);
}
for(i=0;i<10;i++)
printf("\nElement=%d Address=%u",a[i],&a[i]);
getch();
}
OUTPUT
Enter 1 Element In An Array:2
Enter 2 Element In An Array:4
Enter 3 Element In An Array:5
Enter 4 Element In An Array:6
Enter 5 Element In An Array:7
Enter 6 Element In An Array:8
Enter 7 Element In An Array:9
Enter 8 Element In An Array:10
Enter 9 Element In An Array:15
Enter 10 Element In An Array:12
Element=2 Address=65506
Element=4 Address=65508
Element=5 Address=65510
Element=6 Address=65512
Element=7 Address=65514
Element=8 Address=65516
Element=9 Address=65518
Element=10 Address=65520
135
Element=15 Address=65522
Element=12 Address=65524
Page
135
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
Aim- Write a program to find largest number and smallest number from 10 integer
number in array.
Code-
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,b,s;
clrscr();
for(i=0;i<10;i++)
{
printf("Enter %d Element In An Array: ",i+1);
scanf("%d",&a[i]);
}
b=a[0];
s=a[0];
for(i=1;i<10;i++)
{
if(a[i]>b)
b=a[i];
if(a[i]<s)
s=a[i];
}
printf("\nLargest Value In An Array Is : %d\nSmallest Value In An Array Is : %d",b,s);
getch();
}
OUTPUT
136
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
Aim – Write a program to merge two array ,with 5 element each, in to third array.
Code-
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5],b[5],i,c[10];
clrscr();
printf("Enter Element In First Array: \n");
for(i=0;i<5;i++)
{
printf("%d Elements Is :",i+1);
scanf("%d",&a[i]);
}
printf("Enter Element In Second Array: \n");
for(i=0;i<5;i++)
{
printf("%d Element Is :",i+1);
scanf("%d",&b[i]);
}
for(i=0;i<10;i=i+2)
{
c[i]=a[i/2];
c[i+1]=b[i/2];
}
printf("\nElements of Merged Array Are: ");
for(i=0;i<10;i++)
printf("%d ",c[i]);
getch();
}
OUTPUT
2 Element Is :12
3 Element Is :10
Page
137
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
4 Element Is :6
5 Element Is :8
Aim- Write a program to read three digit number and generate its possible permutation
Code-
#include<stdio.h>
#include<conio.h>
void main()
{
int a[3],x,i,m=1,c,j;
clrscr();
printf("Enter Three Digit Number : ");
scanf("%d",&x);
for(i=0;i<3;i++)
{
a[i]=x%10;
x/=10;
}
if(a[0]==a[1]&&a[1]==a[2])
m=6;
if(a[0]==a[1]||a[1]==a[2]||a[0]==a[2])
m=2;
printf("Possible Permutations Are: ");
for(i=0;i<6/m;i++)
{
for(j=0;j<3;j++)
{
if(i>2)
c=i-j;
else
c=i+j;
if(c<0)
c=-1*c;
printf("%d",a[c%3]);
}
printf(" ");
}
getch();
}
138
OUTPUT
Page
138
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
Aim- Write a program to enter five number using array and rearrange it in reverse order.
Code-
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5],i;
clrscr();
for(i=0;i<5;i++)
{
printf("Enter %d Element : ",i+1);
scanf("%d",&a[i]);
}
printf("The Elements In Reverse Order Is: ");
for(i=4;i>=0;i--)
printf("%d, ",a[i]);
getch();
}
OUTPUT
Enter 1 Element : 12
Enter 2 Element : 35
Enter 3 Element : 62
Enter 4 Element : 45
Enter 5 Element : 54
The Elements In Reverse Order Is: 54, 45, 62, 35, 12,
Aim – Write a program to count the number of student belonging to each of the following
group of marks: obtained by 50 students
0-9, 10-19, 20-29,………….,80-89,90-99,10
Code-
#include<stdio.h>
#include<conio.h>
void main()
{
int m[10],i,j,c[10],k=0,d=0;
clrscr();
for(i=0;i<10;i++)
{
139
printf("Enter Marks:");
scanf("%d",&m[i]);
Page
139
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
if(m[i]>99)
d++;
}
for(j=0;j<=90;j=j+10)
{
c[k]=0;
for(i=0;i<10;i++)
{
if(m[i]>=j&&m[i]<=j+9)
c[k]++;
}
k++;
}
for(j=0,k=0;k<10;j=j+10,k++)
printf("\nNo Of Students Belonging To %d to %d Is : %d",j,j+9,c[k]);
printf("\nNo of Students Belonging To 100 Is : %d",d);
getch();
}
OUTPUT
Enter Marks:95
Enter Marks:65
Enter Marks:12
Enter Marks:32
Enter Marks:15
Enter Marks:32
Enter Marks:65
Enter Marks:02
Enter Marks:32
Enter Marks:36
No Of Students Belonging To 0 to 9 Is : 1
No Of Students Belonging To 10 to 19 Is : 2
No Of Students Belonging To 20 to 29 Is : 0
No Of Students Belonging To 30 to 39 Is : 4
No Of Students Belonging To 40 to 49 Is : 0
No Of Students Belonging To 50 to 59 Is : 0
No Of Students Belonging To 60 to 69 Is : 2
No Of Students Belonging To 70 to 79 Is : 0
No Of Students Belonging To 80 to 89 Is : 0
No Of Students Belonging To 90 to 99 Is : 1
140
140
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
Aim- WAP to input 20 number and separate it in even and odd array.
Code-
#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],e[20],o[20],i,j=0,k=0;
clrscr();
for(i=0;i<20;i++)
{
printf("Enter %d Element : ",i+1);
scanf("%d",&a[i]);
}
for(i=0;i<20;i++)
{
if(a[i]%2==0)
{
e[k]=a[i];
k++;
}
else
{
o[j]=a[i];
j++;
}
}
printf("\nEven Array Element Is: ");
for(i=0;i<k;i++)
printf("%d ",e[i]);
printf("\nOdd Array Element Is: ");
for(i=0;i<j;i++)
printf("%d ",o[i]);
getch();
}
OUTPUT
Enter 1 Element : 15
Enter 2 Element : 32
Enter 3 Element : 65
Enter 4 Element : 48
Enter 5 Element : 958
Enter 6 Element : 75
141
Enter 7 Element : 52
Enter 8 Element : 51
Page
141
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
Enter 9 Element : 36
Enter 10 Element : 39
Enter 11 Element : 82
Enter 12 Element : 10
Enter 13 Element : 32
Enter 14 Element : 02
Enter 15 Element : 01
Enter 16 Element : 30
Enter 17 Element : 37
Enter 18 Element : 39
Enter 19 Element : 34
Enter 20 Element : 36
Aim - WAP a program to count occurrence of digit 0 to 9 digit between 1 and given decimal
number
Code-
#include<stdio.h>
#include<conio.h>
void main()
{
int a,i,j,c[10];
clrscr();
printf("Enter Number (<100): ");
scanf("%d",&a);
for(i=0;i<=a;i++)
{
for(j=0;j<10;j++)
{
c[j]=0;
if((i/10)%10==j||i%10==j)
c[j]++;
}
}
for(i=0;i<10;i++)
printf("\nNumber of %d Is : %d",i,c[i]);
getch();
}
142
OUTPUT
Page
142
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
Number of 0 Is : 0
Number of 1 Is : 0
Number of 2 Is : 1
Number of 3 Is : 0
Number of 4 Is : 0
Number of 5 Is : 1
Number of 6 Is : 0
Number of 7 Is : 0
Number of 8 Is : 0
Number of 9 Is : 0
Aim- Write a program to find largest number from 5 rows and 5 column matrix
Code-
#include<stdio.h>
#include<conio.h>
void main()
{
int matrix[5][5],i,j,big;
clrscr();
printf("Enter Elements of the 5*5 Matrix: ");
for(i=0;i<5;i++)
for(j=0;j<5;j++)
scanf("%d",&matrix[i][j]);
big=matrix[0][0];
for(i=0;i<5;i++)
for(j=0;j<5;j++)
{
if(matrix[i][j]>big)
big=matrix[i][j];
}
printf("\nBiggest Element in 5*5 Matrix Is : %d",big);
getch();
}
OUTPUT
Enter Elements of the 5*5 Matrix: 15
51 65 98 45 87 98 65
45 65 98 41 20 32 32 65
654 456 45 465 68
143
143
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
OUTPUT
#include<stdio.h>
#include<conio.h>
void main()
{
char a[100], t;
int l=0,k=0;
clrscr();
printf("Enter The String : ");
scanf("%[^\n]s",a);
while(a[l]!='\0')
{
l++;
144
}
Page
144
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
for(int i=1;i<l/2;i++)
{
t=a[i];
a[i]=a[l-i-1];
a[l-i-1]=t;
}
printf ("The reverse Of The Entered String Is: %s", a);
getch();
}
OUTPUT
Enter The String : KAUSHAL
The reverse Of The Entered String Is: LAHSUAK
Code-
#include<stdio.h>
#include<conio.h>
void main()
{
char a[100],b[100];
int i=0,k=0,j=0,len=0;
clrscr();
printf("Enter The String : ");
scanf("%[^\n]s",a);
while(a[i]!='\0')
{
len++;
i++;
}
for(k=i-1;k>=0;k--)
{
b[i-(k+1)]=a[k];
}
i=0;
while(a[i]!='\0')
{
if(a[i]==b[i])
j++;
145
i++;
}
Page
145
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
if(len==j)
printf("Palindrome\n");
else
printf("Not Palindrome\n");
getch();
}
OUTPUT
k++;
j++;
Page
146
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
}
if(k==len)
printf("Both Strings Are Same\n");
}
else
printf("\nBoth Strings Are Not Same");
getch();
}
OUTPUT
while(a[j]!='\0')
{
Page
147
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
if(a[j]==b[j]&&(a[j]==b[j]+32||a[j]==b[j]-32))
k++;
j++;
}
if(k==len)
printf("Both Strings Are Same\n");
else
printf("Both Strings Are Not Same\n");
}
else
printf("\nBoth Strings Are Not Same");
getch();
}
OUTPUT
OUTPUT
Page
148
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
Aim - WAP to append the first n character of string in to another string, and print.
Code-
#include<stdio.h>
#include<conio.h>
void main()
{
char a[100],b[100];
int i=0,len=0,n,k=1;
clrscr();
printf("Enter The String: ");
gets(a);
while(a[i]!='\0')
{
len++;
i++;
}
printf("The Length Of The String Is : %d\n\n",len);
printf("Enter The Length Of Character To Append less Than %d: ",len);
scanf("%d",&n);
while(k<n)
{
b[k-1]=a[k-1];
k++;
}
b[k]='\0';
printf("The String Appended is: ");
puts(b);
getch();
}
OUTPUT
149
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
Aim – WAP that will read a line and delete all vowel from sentence. Assume that sentences
is not more than 80 char long.
Code –
#include<stdio.h>
#include<conio.h>
void main()
{
char a[100];
int i=0,len=0;
clrscr();
printf("Enter The String: ");
gets(a);
while(a[i]!='\0')
{
len++;
i++;
}
printf("The String After Deletion Of Vowel: ");
i=0;
while(i<=len)
{
if(a[i]!='a')
if(a[i]!='e')
if(a[i]!='i')
if(a[i]!='o')
if(a[i]!='u')
if(a[i]!='A')
if(a[i]!='E')
if(a[i]!='I')
if(a[i]!='O')
if(a[i]!='U')
printf("%c",a[i]);
i++;
}
getch();
}
OUTPUT
Aim - Write a program that convert all character in string in to capital letter.
Page
150
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
Code -
#include<stdio.h>
#include<conio.h>
void main()
{
char a[100];
int i=0;
clrscr();
printf("Enter The String: ");
gets(a);
while(a[i]!='\0')
{
if(a[i]>=97 && a[i]<=122)
a[i]=a[i]-32;
i++;
}
a[i]='\0';
printf("The String In Capital Is: ");
puts(a);
getch();
}
OUTPUT
Aim – Write a program to take set of individual full name and print abbreviate the first,
middle except the last name.
Code –
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
char name[40];
char abname[30];
int i,len,m=1,j=0;
clrscr();
puts("Enter Name: ");
151
gets(name);
len=strlen(name);
Page
151
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
abname[j++]=name[0];
abname[j++]='.';
abname[j++]=' ';
for (i=0;i<len;i++)
{
if(name[i]==' ')
if(m==1)
{
abname[j++]=name[i+1];
abname[j++]='.';
abname[j++]=' ';
m=0;
}
else
{
while(name[i]!='\0')
{
abname[j++]=name[++i];
}
}
}
abname[j++]='\0';
puts("Abbreviated Name: ");
puts(abname);
getch();
}
OUTPUT
Aim – Write a program to take set of 5 name, and find Employee “abc” is there or not.
Code-
#include<stdio.h>
#include<conio.h>
void main()
{
char name[5][25],sa[4]={"ABC"};
int i , c=1,j;
152
clrscr();
Page
152
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
OUTPUT
ASD
AFG
ABC
QWE
RST
Employee ABC Is Found
Aim – Write a program to take set of 5 name, and find largest name.
Code-
#include<stdio.h>
#include<conio.h>
void main()
153
{
char name[5][25];
Page
153
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
int i , c[5]={0,0,0,0,0},j,k,max;
clrscr();
puts("Enter The Names : ");
for(i=0;i<=4;i++)
scanf("%s",name[i]);
for(i=0;i<=4;i++)
{
j=0;
while(name[i][j]!='\0')
{
c[i]++;
j++;
}
}
max=c[0];
for(i=0;i<5;i++)
{
if(c[i]>max && c[i]!=max)
{
max = c[i];
k=i;
}
k=0;
}
printf("The Largest Name Is : %s",name[k]);
getch();
}
OUTPUT
Enter The Names :
VIMAL
MRINAL
BHUDEV
ABHILASH
LOKESH
The Largest Name Is : ABHILASH
154
Page
154
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
printf("Enter a string\n");
gets(string);
c++;
}
getch();
}
OUTPUT
Enter a string :=> It Is My First Practical Class On C Lab
a occurs 4 times in the entered string.
b occurs 1 times in the entered string.
c occurs 2 times in the entered string.
i occurs 2 times in the entered string.
l occurs 2 times in the entered string.
n occurs 1 times in the entered string.
r occurs 2 times in the entered string.
s occurs 4 times in the entered string.
155
155
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
Aim – Write a program to count no. of vowels , consonant , blank in input string.
Code –
#include <stdio.h>
#include <string.h>
#include<conio.h>
void main()
{
char a[100];
int VowelCount=0, ConsonantCount=0;
int whitespace=0, digit;
int i;
clrscr();
printf( "Enter a String :=> ");
gets(a);
digit=strlen(a);
156
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
OUTPUT
Enter a String :=> Saturday Is The Last Day For Submission Of Soft Copy Of C
Lab
Vowels = 17
Consonant = 32
Whitspace = 12
157
Page
157
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
1. main() 2. main()
{ {
int i = –1, j = –1, k = 0, l = 2, m; printf("%x",–1 << 4);
m = i++&&j++&&k++||l++; }
printf("%d %d %d %d %d",i,j,k,l,m);
} (a) 3ffc (b) FFF0
(a) –1 –1 0 2 (b) 0 0 1 3 1 (c) fff0 (d) 3FFC
(c) 0 0 1 3 0 (d) 1113
3. #define int char 4. main()
main() {
{ int i=10;
int i = 65; i=!i>14;
printf("sizeof(i)=%d", sizeof(i)); Printf ("i=%d",i);
} }
(a) 8 (b) 2 (a) 0 (b) 1
(c) 4 (d)1 (c) 2 (d) 0 & 1
5. main() 6. main()
{ {
printf("\nab"); int i=5;
printf("\bsi"); printf("%d%d%d%d%d",i++,i--
printf("\rha"); ,++i,--i,i);
} }
(a) ash (b) absiha (a) 56665 (b) 564544
(c) hai (d) has (c) 45545 (d) 55545
} }
(a) 400..300 (b) 300..400 (a) 13 (b) 11
Page
158
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
(c) 1 (d) 5 }
(a) 10 10 (b) 20 10
Page
159
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
(c) 20 20 (d) 10 20
{ {
int ii = 10; printf(―%d‖,printf(―computer‖));
Page
160
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
ii<<=1; printf(―%d‖,ii); } }
(a) 10 (b) 15 (c) 20 (d) error (a) computer 8 (b) computer8
(c) 8computer (d) 8 computer
(*functable[0])("%d", &i); }
Page
161
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
}
,++main)); } (a) Hihello (b) hi hello
Page
162
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
163
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
{ printf(―%d‖, {
strcat(((!0)|6|0xf)>=20?0x10:2,8)); } float i=1.5;
Page
164
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
(b) 10 9 8…………2 1 0
(c) 9 8 7…………..2 1 0
Page
165
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
(d) garbage
73. void main() 74. wahat is the data type used in switch
{ statement?
int i , j; (a) int char float (b) char float
for(i=0,j=0;i<5,j<25;i++,j++); (c) int char (d) char string
printf(―i=%d,j=%d‖,i,j);
}
(a) i=25 j=25 (b) i= 5 j=5
(c) i=4 j=24 (d) Error
75. void main() 76. void main()
{ int i=1; {
for(;i;) i++;printf(―%d‖,i); } int var=0;
(a) 1 (b) 0 for(;++var;printf(―%d‖,var));
(c) +inf (d) none printf(―%d‖,var);
}
(a) 0 (b) 1 to 0
(c) 1 to +INF (d) 0 to +INF
166
166
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
83. What is the value assigned to the 84. What is the value of the expression
variable X if b is 7? (3^6) + (a^a)?
X = b>8? b <<3: b>4? b>>1: b; (a)3 (b) 5
(a) 5 (b) 3 (c) 6
(c) 7 (d) 0
(d) a+18 ( e) None
167
Page
167
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
printf(―%d‖,i++); }
if(I>10) (a) Error (b) 10
Page
168
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
169
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
{ {
char p[ ]="%d\n"; char str1[] = {‗s‘,‘o‘,‘m‘,‘e‘};
Page
170
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
171
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
} s3=s1+s2;
(a) science (b) computer printf(―%s‖,s3);
science }
(c) computer (d) Error (a) HelloWelcome (b) Hello
(c) Welcome (d) Error
172
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
}
(a) 789 , 987 (b) 987 , 789
(c) 789 , 789 (d) 987 ,987
125. void main() 126. void main()
{ {
char s[]={{‗H‘},{‗E‘}}; char str[]=‖RAVI‖;
printf(―%s‖,s); int i=0,n=strlen(str);
printf(―%c‖,*s-1); while(n)
printf(―%c‖,*s); { n--;
printf(―%c‖,*s+1); str[i]=str[n]; i++;
printf(―%c‖,*s+2); }
} printf(―%s‖,str); }
(a) H,E,I,J,K (b) HE,G,H,I,J (a) RAVI (b) IVI
(c) HE,J,I,H (d) HE (c) IVAR (d) IVVI
129. void main() 130. what is the allowed data type for
{ subscript?
int a[]={0,0x4,4,9}; (a) int (b) float
int I=2; (c) double (d) all
printf(―%d,%d\n‖,a[I],I[a]); }
(a) 4 (b) 04 04
(c) 0x4 0x4 (d) o4 o4
173
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
174
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
printf(―%d‖,*(a+strlen(a)));} printf(―i-GateAcademy‖+6);}
(a) 0 (b) 1 (a) cademy (b) Academy
(c) 2 (d) 3 (c) i-Gate (d) Error
149. Identify the wrong expression given int 150. void main()
a[10]? {int a[3]={1,2,3,4,5};
175
175
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
(c) 3 (d) 0
176
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
int a=2,b=4;
junk(a,&b); (A) 2
printf(―\n\t %d,%d‖,a,b); (B) 0
} (C)Compiler error
(a) 2, 16 (b) 2, 2 (D) Runtime error
(c) 4, 4 (d) 4,16
177
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
(b) hi TechPreparation.com
(c) hi hi TechPreparation.com (d) Give runtime error
Page
178
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
174. The default return data type in function 175. Recursive call results when
definition is : (a) a function calls itself
(a) void (b) int (b) a function1 call another function,
(c) float (d) char which in turn calls the function1
(c) options a and b
(d) a function calls another function
{ math.h
printf(―sizeof (void *) = %d \n―, sizeof( void (a) return the value rounded down to
Page
179
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
180. The function floor(x) defined in math.h 181. The parameter passing mechanism
(a) return the value rounded down to used in C is
the next lower integer (a) call by reference
(b) return the value rounded up to the (b) call by value
next lower integer (c) option a & b
(c) the next lower value (d) call by name
(d) the next higher value
}
Page
180
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
190. void fun(int x) 191. if j=0, k=2 and m=14, consider the
{ expression,
if(x>0) func(--x); N=(k&&m)+(j<k/m)+(j||(!m))+(m/k);
printf(―%d,‖,x);
} What value is stored in N?
int main()
{ fun(5); (a) 0 (b) 8 (c) 7 (d) 9
181
return 0;
}
Page
181
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
182
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
200. Let A be 2D array declared as follows 201. What does the following algorithm
A: array[1..10][1..15] approximates?(Assume m>1, e>0)
Assuming that each integer takes one x=m;
memory locations the array is stored I y=1
row-major order and the first element of while(x-y> e)
the array is stored at location 100, what is {
the address of the element a[i][j]? x=(x+y)/2;
y=m/x;}
(a) 15i+j+84 (b) 15j+i+84 print(x);
(c) 10i+j+89 (d) 10j+i+89 (a) logm (b) m2 (c) m1/2(d) m1/3
202. float f(float x,float y) Common Data Questions 203 & 204
{
float p,s;int i; Consider the following code
for(s=1,p=1,i=1;i<y;i++) void main(){
{ enum color{
183
183
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
} x=YELLOW;
return s; printf("%d",x);
} }
For large values of y, the return value Q203- What will be output if you will
of the function f best approximates compile and execute the above c code?
(a)Xy (b) ex (c) ln(1+x) (d) Xx
(a) -22 (b) -18 (c) 1 (d)
Compiler error
Answer Key
1-B 2-C 3-D 4-A 5-C 6-C 7-D 8-A 9-A 10-D
11-A 12-B 13-C 14-A 15-C 16-A 17-A 18-A 19-C 20-A
21-A 22-D 23-A 24-C 25-B 26-B 27-D 28-B 29-C 30-B
31-B 32-A 33-D 34-A 35-B 36-A 37-A 38-C 39-B 40-C
41-D 42-B 43-B 44-D 45-B 46-B 47-D 48-B 49-B 50-A
51-B 52-A 53-B 54-A 55-A 56-C 57-A 58-A 59-C 60-B
61-C 62-B 63-A 64-C 65-A 66-A 67-A 68-A 69-D 70-A
71-A 72-C 73-A 74-C 75-B 76-B 77-B 78-B 79-C 80-B
81-B 82-C 83-B 84-B 85-A 86-A 87-B 88-B 89-A 90-B
91-D 92-C 93-D 94-D 95-D 96-D 97-D 98-A 99-D 100-B
101-D 102-C 103-A 104-C 105-B 106-D 107-A 108-B 109-C 110-D
111-A 112-B 113-C 114-A 115-B 116-D 117-B 118-C 119-A 120-B
121-A 122-A 123-D 124-A 125-B 126-D 127-B 128-A 129-D 130-A
131-B 132-B 133-A 134-A 135-A 136-B 137-C 138-A 139-A 140-B
141-C 142-C 143-A 144-C 145-A 146-B 147-A 148-C 149-D 150-A
151-A 152-A 153-B 154-D 155-D 156-D 157-B 158-A 159-C 160-B
161-B 162-B 163-C 164-C 165-A 166-B 167-A 168-A 169-C 170-A
171-C 172-C 173-C 174-B 175-C 176-C 177-A 178-C 179-B 180-A
181-B 182-D 183-C 184-D 185-C 186-D 187-B 188-B 189-C 190-B
191-B 192-A 193-A 194-C 195-C 196-C 197-C 198-C 199-C 200-A
201-C 202-B 203-B 204-D
184
Page
184
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
Test Paper 1:
1. #include <stdio.h>
2. int main()
3. {
4. int c = 2 ^ 3;
5. printf("%d\n", c);
6. }
1. #include <stdio.h>
2. int main()
3. {
4. unsigned int a = 10;
5. a = ~a;
6. printf("%d\n", a);
7. }
1. #include <stdio.h>
2. int main()
3. {
4. if (7 & 8)
5. printf("Honesty");
6. if ((~7 & 0x000f) == 8)
7. printf("is the best policy\n");
8.
9. }
1. #include <stdio.h>
2. int main()
3. {
4. int a = 2;
5. if (a >> 1)
6. printf("%d\n", a);
185
7. }
Page
185
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
1. #include <stdio.h>
2. int main()
3. {
4. int i, n, a = 4;
5. scanf("%d", &n);
6. for (i = 0; i < n; i++)
7. a = a * 2;
8. }
1. #include <stdio.h>
2. void main()
3. {
4. int x = 97;
5. int y = sizeof(x++);
6. printf("x is %d", x);
7. }
1. #include <stdio.h>
2. void main()
3. {
4. int x = 4, y, z;
5. y = --x;
6. z = x--;
7. printf("%d%d%d", x, y, z);
8.
9. }
1. #include <stdio.h>
2. void main()
3. {
4. int x = 4;
5. int *p = &x;
6. int *k = p++;
186
7. int r = p - k;
8. printf("%d", r);
Page
186
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
9.
10. }
1. #include <stdio.h>
2. int main()
3. {
4. int i = -3;
5. int k = i % 2;
6. printf("%d\n", k);
7. }
1. #include <stdio.h>
2. int main()
3. {
4. int i = 3;
5. int l = i / -2;
6. int k = i % -2;
7. printf("%d %d\n", l, k);
8. return 0;
9. }
1. #include <stdio.h>
2. int main()
3. {
4. int i = 5;
5. i = i / 3;
6. printf("%d\n", i);
7. return 0;
8. }
1. #include <stdio.h>
2. int main()
3. {
187
4. int i = -5;
5. i = i / 3;
Page
187
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
6. printf("%d\n", i);
7. return 0;
8. }
1. #include <stdio.h>
2. void main()
3. {
4. int x = 5 * 9 / 3 + 9;
5. }
1. #include <stdio.h>
2. void main()
3. {
4. int x = 5.3 % 2;
5. printf("Value of x is %d", x);
6. }
1. #include <stdio.h>
2. void main()
3. {
4. int y = 3;
5. int x = 5 % 2 * 3 / 2;
6. printf("Value of x is %d", x);
7. }
1. #include <stdio.h>
2. void main()
3. {
4. char *s= "hello";
5. char *p = s;
6. printf("%c\t%c", p[0], s[1]);
7. }
188
188
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.
C Programming
1. #include <stdio.h>
2. void main()
3. {
4. char *s= "hello";
5. char *p = s;
6. printf("%c\t%c", 1[p], s[1]);
7. }
1. #include <stdio.h>
2. void foo( int[] );
3. int main()
4. {
5. int ary[4] = {1, 2, 3, 4};
6. foo(ary);
7. printf("%d ", ary[0]);
8. }
9. void foo(int p[4])
10. {
11. int i = 10;
12. p = &i;
13. printf("%d ", p[0]);
14. }
1. #include <stdio.h>
2. int main()
3. {
4. int ary[4] = {1, 2, 3, 4};
5. int *p = ary + 3;
6. printf("%d\n", p[-2]);
7. }
1. int main()
2. {
3. int ary[4] = {1, 2, 3, 4};
4. int *p = ary + 3;
189
189
By Siddharth S. Shukla (BE, ME, PhD* )
i-Gate , B-713, Street 22, Smriti Nagar, Bhilai- 490020, Contact Mobile 98271-62352
No part of this booklet may be reproduced or utilized in any form without the written permission. All right are reserved.