Sunteți pe pagina 1din 65

Practical No: 1

Aim: Assignment on Flow Chart.

Theory:
Flow Chart: Flowchart is a diagrammatic representation of an algorithm. Flowcharts are
very helpful in writing program and explaining program to others. Flowchart are the graphical
representation of a solution to a particular problem, which comes under the category of
Programming Practices and Techniques, in simple words flowchart is a graphical representation of
a program. Flowchart are symbolic diagrams which shows type of data(numeric, character etc.),
data flow, control flow and programming logics and algorithms.
The first flowchart was created by Von Neumann in 1945. Flowcharts are important for
planning and working of a program. Flowcharts are important for planning and working of a
program.
Conclusion: Hence, we have studied about flow charts and how to draw flow charts by using
standard flow chart symbols with examples.
Practical No: 2

Aim: A Simple program to display a message “Hello World” on screen.

Software Requirement: Operating System: Windows / Linux, TURBO C++ 3.2 / Eclipse CDT Luna

Hardware Requirement: Desktop PC with Dual Core Processor 2.0 GHz, RAM - 1GB, HDD -20GB

Theory:
In this experiment we have to study about different operating systems as follows:

Operating System (OS):


An operating system (OS) is system software that manages computer hardware and
software resources and provides common services for computer programs.
UNIX, BSD, Windows (XP, Vista, 7) and Mac OS, are all examples of operating systems
No operating system
•Machines run from a console with
Display lights, toggle switches, input device, and printer
•Schedule time, e.g. sign up
•Setup included loading the compiler and source program, saving compiled program, loading
and linking.
An operating system has three main functions:
(1)Manage the computer's resources, such as the central processing unit, memory, disk
drives, and printers.
(2) Establish a user interface.
(3) Execute and provide services for applications software.

Introduction to C language:

C is a general-purpose high level language that was originally developed by Dennis


Ritchie for the Unix operating system. It was first implemented on the Digital Eqquipment
Corporation PDP-11 computer in 1972.

The Unix operating system and virtually all Unix applications are written in the C language.
C has now become a widely used professional language for various reasons.

 Easy to learn
 Structured language
 It produces efficient programs.
 It can handle low-level activities.
 It can be compiled on a variety of computers.

Facts about C

 C was invented to write an operating system called UNIX.


  C is a successor of B language which was introduced around 1970
 The language was formalized in 1988 by the American National Standard Institue
 (ANSI).
 By 1973 UNIX OS almost totally written in C.
 Today C is the most widely used System Programming Language.
 Most of the state of the art software have been implemented using C
Why use C?
C was initially used for system development work, particularly the programs that make-up the
operating system. C was adopted as a system development language because it produces code
that runs nearly as fast as the code written in assembly language. Some examples of the use of
C might be −

 Operating Systems
 Language Compilers
 Assemblers
 Text Editors
 Print Spoolers
 Network Drivers
 Modern Programs
 Databases
 Language Interpreters
 Utilities
The C Compiler
The source code written in source file is the human readable source for your program. It needs
to be "compiled", into machine language so that your CPU can actually execute the program as
per the instructions given.

The compiler compiles the source codes into final executable programs. The most frequently
used and free available compiler is the GNU C/C++ compiler, otherwise you can have compilers
either from HP or Solaris if you have the respective operating systems.

Hello World Example


A C program basically consists of the following parts

 Preprocessor Commands
 Functions
 Variables
 Statements & Expressions
 Comments

#include <stdio.h>
#include <conio.h>
void main()
{
printf("Hello, World! \n"); /* my first program in C */
getch();
}

Let us take a look at the various parts of the above program −


The first line of the program #include <stdio.h> is a preprocessor command, which tells a C
compiler to include stdio.h file before going to actual compilation. The next line void main() is
the main function where the program execution begins.

 The next line /*...*/ will be ignored by the compiler and it has been put to add
additional comments in the program. So such lines are called comments in the
program.

 The next line printf(...) is another function available in C which causes the message
"Hello, World!" to be displayed on the screen.

Semicolons:
In a C program, the semicolon is a statement terminator. That is, each individual statement
must be ended with a semicolon. It indicates the end of one logical entity.
Given below are two different statements −
printf("Hello, World! \n");
return 0;

C programming has several in-built library functions to perform input and output tasks. Two
commonly used functions for I/O (Input/Output) are printf() and scanf(). The scanf() function
reads formatted input from standard input (keyboard) whereas the printf() function sends
formatted output to the standard output (screen).
C language is case sensitive. For example, printf() and scanf() are different from Printf() and
Scanf(). All characters in printf() and scanf() functions must be in lower case.
Escape Sequence:
Escape sequences are special single character constants starts with backslash (\) followed by
one or more characters.

Escape Character Meaning Escape Character Meaning


\n newline \t Horizontal tab
\a Audible alert \b backspace
\f Form feed \r Carriage return
\v Vertical tab \\ Back slash

Conclusion:
Hence, we have studied a Simple program to display a message “Hello World” on
screen.
Practical No: 3
Aim: A Program to take input from user and display value entered by user on screen.

Software Requirement: Operating System: Windows / Linux, TURBO C++ 3.2 / Eclipse CDT Luna

Hardware Requirement: Desktop PC with Dual Core Processor 2.0 GHz, RAM - 1GB, HDD -20GB

Theory:
In this experiment we have to study about variables, in C program, different primitive data types available
in C and different placeholders i.e. format specifiers.
Variables:
Definition: Variable is the name given to memory location to store a value at the time of program in the
execution.
a) A program can consist of more than one variables.
b) Data type and variable name is associated with each variable.
c) Variable name, function name, constant is known as an Identifier.
d) C is case sensitive language so each variable name with combination of uppercase and lowercase,
numbers (0-9) and underscore(_) is treat as distinct.
e) Variable name should not same as keywords in C.
Variable Declaration & Initialization:
Variable Declaration:
a) In C program, we must have to declare variable before it used.
b) At the time of declaration, one can declare one or more Variables.
e.g. data type variable_name1, variable_name2, .... ;
int age, marks;
float average, radius, perimeter;
Variable Initialization:
a) Variable initialization means assigning a value to particular variable.
b) User can initialize more than one variable in declaration Statement.
e.g. data type variable1 = value, variable2 = value, ....... ;
int age = 21, mark = 45;
Data Types:
Why do we use Data Types in Programming Language?
- Programmer gives input i.e data (value) to program, so compiler gets idea which type of data user
wants to process.
- A Value can be numeric, decimal or alphanumeric.
- Most of programming language provides integer (int) for whole numbers, float for decimal numbers, and
characters (char) which takes a alphabet as a data types.
- C also supports int, float, double, char and void as primary data types; struct, enum, union, pointer as an
advanced data types.
Constants:
- Sometimes user need to use same value again and again in program. In such case C, a variable initialize
with the fixed value.
Definition: A value assign to variable or identifier which can not modified when program is in execution.
- It is also known as literals.
- C supports integer constants, float constant, character constant and enumeration constant.
Placeholder:
The printf() function is a powerful function, and is probably the most-used function in C programs. For
example, let us look at a problem. Say we don't know what 19 + 31 is. Let's use C to get the answer.We start
writing

#include "stdio.h" // this is important, since printf


// can't be used without this header

int main(void)
{
printf("19+31 is");

But here we are stuck! printf() only prints strings! Thankfully, printf has methods for printing numbers. What
we do is put a placeholder format code in the string. We write:

printf("19+31 is '''%d'''", 19+31);

The placeholder %d literally "holds the place" for the actual number that is the result of adding 19 to 31.

These placeholders are called format specifiers. Many other format specifiers work with printf() . If we have
a floating-point number, we can use %f to print out a floating-point number, decimal point and all. Other format
specifiers are:

 %d - int (same as %i)


 %ld - long int (same as %li)
 %f - float
 %lf - double[1]
 %c - char
 %s - string
 %x – hexadecimal

scanf()and use of address of operator & in it :

The scanf() function is the input method equivalent to the printf() output function - simple yet
powerful. In its simplest invocation, the scanf format string holds a single placeholder representing the type
of value that will be entered by the user. These placeholders are mostly the same as the printf() function
- %d for ints, %f for floats, and %lf for doubles.

There is, however, one variation to scanf() as compared to printf() . The scanf() function requires
the memory address of the variable to which you want to save the input value. While pointers (variables
storing memory addresses) can be used here, this is a concept that won't be approached until later in the
text. Instead, the simple technique is to use the address-of operator, &. For now it may be best to consider
this "magic" before we discuss pointers.
A typical application might be like this:

#include "stdio.h"

int main(void)
{
int a;

printf("Please input an integer value: ");


scanf("%d", &a);
printf("You entered: %d\n", a);

return 0;
}

If you were to describe the effect of the scanf() function call above, it might read as: "Read in an integer
from the user and store it at the address of variable a ".
Program:
#include<stdio.h>
void main()
{
int a, d =1130; //variable declaration and initialization
float b;
char c;
printf("\n Enter a Number:");
scanf("%d", &a); // %d format string is used in case of integer
printf("\n Entered number = %d and assign value to d = %d", a, d);
printf("\n Enter a number: ");
scanf("%f", &b); // %f format string is used in case of floats
printf("\n Value of b = %f", b);
printf("\n Floating point number rounded to 2 digits: %.2f\n", b);
printf("\n Enter a character: ");
scanf("%c", &c); // %c format string is used in case of character
printf("\n You entered a character = %c", c);
printf("\n ASCII value of a character %c is %d.", c, c);
}
Program Output:
Enter a Number: 5
Entered number = 5 and assign value to d = 1130
4 digit integer right justified to 6 column: 1130
Enter a number: 4.952
Value of b = 4.952000
Floating point number rounded to 2 digits: 4.95
Enter a character: f
You entered a character = f
ASCII value of a character f is 102.

Conclusion: Hence, we have studied A Program to take input from user and display value entered by user
on screen successfully.
Algorithm and Flowchart: Practical No 4
Algorithm for arithmetic operations:
1. Start
2. Accept two numbers a,b from user
3. Add a to b and store result in c
4. Display the addition of two numbers i.e c.
5. Multiply a to b and store result in c
6. Display the multiplication of two numbers i.e c
7. Divide a by b and store result in c
8. Display the Division of two numbers i.e c.
9. Subtract a from b and store result in c
10. Display the subtraction of two numbers i.e c
11. Stop.
Flowchart for arithmetic operation
Practical No: 4
Aim: Basic Examples for performing different C operations Using operator (with & without
using scanf( )).

Software Requirement: Operating System: Windows / Linux, TURBO C++ 3.2 / Eclipse CDT Luna

Hardware Requirement: Desktop PC with Dual Core Processor 2.0 GHz, RAM - 1GB, HDD -20GB

Theory:
With scanf( ) function:
SCANF() Function In C Language:

In C programming language, scanf() function is used to read character, string, numeric data
from keyboard
scanf("%d", &number);

The format specifier %d is used in scanf() statement. So that, the value entered is received as an
integer
1. printf() is used to display the output and scanf() is used to read the inputs.
2. printf() and scanf() functions are declared in “stdio.h” header file in C library.
3. All syntax in C language including printf() and scanf() functions are case sensitive.
Program: take input from user and display value entered by user on screen.
#include <stdio.h>
void main()
{
int n;

printf("Enter the number: ");

scanf("%d",&n);

printf("\n Number = %d",n);

Output:
Enter the number: 7
Number =7
Without scanf( ) function:
An operator is a symbol that tells the compiler to perform specific mathematical or logical
functions. C language is rich in built-in operators and provides the following types of operators

 Arithmetic Operators
 Relational Operators
 Logical Operators
 Bitwise Operators
 Assignment Operators

Arithmetic Operators

The following table shows all the arithmetic operators supported by the C language. Assume
variable A holds 10 and variable B holds 20 then,

+ Adds two operands. A + B = 30

− Subtracts second operand from the first. A − B = -10

* Multiplies both operands. A * B = 200

/ Divides numerator by de-numerator. B/A=2

Modulus Operator and remainder of after an integer


% B%A=0
division.

++ Increment operator increases the integer value by one. A++ = 11

-- Decrement operator decreases the integer value by one. A-- = 9


Following example to understand all the arithmetic operators available in C

#include <stdio.h>
void main()
{
int a = 21;
int b = 10;
int c ;

c = a + b;
printf(" Value of c is %d\n", c );

c = a - b;
printf("Value of c is %d\n", c );

c = a * b;
printf("Value of c is %d\n", c );

c = a / b;
printf("Value of c is %d\n", c );

c = a % b;
printf("Value of c is %d\n", c );
}

Output
Value of c is 31
Value of c is 11
Value of c is 210
Value of c is 2
Value of c is 1
Conclusion: Hence, we have studied a Program Basic Examples for performing different C
operations Using operator (with & without using scanf( )) successfully.
Pr. No. 05 Algorithm & Flowchart to find Area, diameter and circumference of a circle.

Algorithm:
Step 1: Start.
Step 2: Input & read radius r.
Step 3: Area= Pi*r*r, Diameter=2*r, Circumference= 2&Pi*r.
Step 4: Print Area, Diameter and Circumference.
Step 5: Stop.
Flowchart:
Practical No: 5
Aim: Basic Program on Operator (Using scanf()).
Program to find and print area, perimeter and volume of geometric objects.

Software Requirement: Operating System: Windows / Linux, TURBO C++ 3.2 / Eclipse CDT Luna

Hardware Requirement: Desktop PC with Dual Core Processor 2.0 GHz, RAM - 1GB, HDD -20GB

Theory:

Basic Program on Operator. (Using scanf()).


An operator is a symbol that tells the compiler to perform specific mathematical or logical
functions.

C Operator Precedence Table


This page lists C operators in order of precedence (highest to lowest).
Their associativity indicates in what order operators of equal precedence in an expression are
applied.

Precedenc Associativit
Operator Description
e y
++ -- Suffix/postfix increment and decrement Left-to-right
() Function call
[] Array subscripting
1 . Structure and union member access
Structure and union member access through
->
pointer
(type){list
Compound literal(C99)
}
++ -- Prefix increment and decrement Right-to-left
+- Unary plus and minus
!~ Logical NOT and bitwise NOT
(type) Type cast
2
* Indirection (dereference)
& Address-of
sizeof Size-of
_Alignof Alignment requirement(C11)
3 */% Multiplication, division, and remainder Left-to-right
4 +- Addition and subtraction
5 << >> Bitwise left shift and right shift
< <= For relational operators < and ≤ respectively
6
> >= For relational operators > and ≥ respectively
7 == != For relational = and ≠ respectively
8 & Bitwise AND
9 ^ Bitwise XOR (exclusive or)
10 | Bitwise OR (inclusive or)
11 && Logical AND
12 || Logical OR
13 ?: Ternary conditional Right-to-
= Simple assignment Left

+= -= Assignment by sum and difference


Assignment by product, quotient, and
14 *= /= %=
remainder
<<= >>= Assignment by bitwise left shift and right shift
&= ^= |= Assignment by bitwise AND, XOR, and OR
15 , Comma Left-to-right
Program: Program to find and print area, perimeter and volume of geometric objects.

#include<stdio.h>
void main()
{
float r,b,h,s,area,vol,perimeter;
float pie=3.14;
printf("\n enter radius r=");
scanf("%f",&r);
area=pie*r*r;
printf("\n area of circle=%f",area);
vol=1.34*pie*r*r*r;
printf("\n volume of sphere=%f", vol);
//for triangle
printf("\n enter b=");
scanf("%f",&b);
printf("\n enter h=");
scanf("%f",&h);
area=0.5*b*h;
printf("\n area of triangle=%f",area);
perimeter=b+b+b;
printf("\n perimeter of triangle=%f",perimeter);
//for square
printf("\n enter s=");
scanf("%f",&s);
area=s*s;
printf("\n area of square=%f",area);
perimeter=4*s;
printf("\n perimeter of square=%f",perimeter);
}

Output:

Conclusion: Hence, we have studied Basic Program on Operator (Using scanf()) successfully.
Pr. No. 06 Algorithm & Flowchart to find maximum and minimum between two nos.
Algorithm:
Step 1: Start.
Step 2: Read the values of A and B.
Step 3: If A is greater than B then print A.
Step 4: If B is greater than A then print B.
Step 5: Stop.
Flowchart:
Practical No: 6
Aim: Program to find maximum and minimum between two numbers given by user using if else
and conditional operators.

Software Requirement: Operating System: Windows / Linux, TURBO C++ 3.2 / Eclipse CDT Luna

Hardware Requirement: Desktop PC with Dual Core Processor 2.0 GHz, RAM - 1GB, HDD -20GB

Theory:
Decision Making:
Decision making structures require that the programmer specifies one or more conditions to
be evaluated or tested by the program, along with a statement or statements to be executed if
the condition is determined to be true, and optionally, other statements to be executed if the
condition is determined to be false. Show below is the general form of a typical decision
making structure found in most of the programming languages −

C programming language assumes any non-zero and non-null values as true, and if it is
either zero or null, then it is assumed as false value. C programming language provides the
following types of decision making statements.
S.N. Statement & Description

1 if statement

An if statement consists of a boolean expression followed by one or more


statements.

2 if...else statement
An if statement can be followed by an optional else statement, which executes
when the Boolean expression is false.

3 nested if statements
You can use one if or else if statement inside another if or else ifstatement(s).

4 switch statement
A switch statement allows a variable to be tested for equality against a list of values.

5 nested switch statements


You can use one switch statement inside another switchstatement(s).

The ? : Operator
We have covered conditional operator ? : in the previous chapter which can be used to
replace if...else statements. It has the following general form −

Exp1 ? Exp2 : Exp3;


Where Exp1, Exp2, and Exp3 are expressions. Notice the use and placement of the colon. The
value of a ? expression is determined like this −

 Exp1 is evaluated. If it is true, then Exp2 is evaluated and becomes the value of the
entire ? expression.

 If Exp1 is false, then Exp3 is evaluated and its value becomes the value of the
expression.
Program 1:
#include <stdio.h>
void main()
{
int a, b; /* Input two numbers from user */
printf("Enter two numbers: ");
scanf("%d%d", &a, &b);
/* If a is maximum */
if(a >b)
{
printf("%d is maximum/n", a);
printf("%d is minimum", b);
}

/* If b is maximum */
else
{
printf("%d is maximum/n", b);
printf("%d is minimum", a);
}

}
Output:
Enter two numbers: 50 20

50 is maximum

20 is minimum

Enter two numbers: 300 700

700 is maximum

300 is minimum
Program 2:
/* Program using conditional operator*/
#include <stdio.h>
void main()
{
int a, b;
/* Input two numbers from user */
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
a > b ? printf(“%d is maximum \n %d is minimum”, a, b) :
printf(“%d is maximum \n %d is minimum”, b, a);
getch();
}
Output:

Enter two numbers: 50 20

50 is maximum

20 is minimum

Conclusion: Hence, we have studied Program to find maximum and minimum between two
numbers given by user using if else and conditional operators successfully.
Pr. No. 07 Algorithm & Flowchart to swap two nos.

 Algorithm : a. using a third variable


o Step 1 : Start
o Start 2 : READ num1, num2
o Start 3 : temp = num1
o Start 4 : num1 = num2
o Start 5 : num2 = temp
o Start 6 : PRINT num1, num2
o Start 7 : Stop

 Algorithm : b. without using a third variable


o Step 1 : Start
o Start 2 : READ num1, num2
o Start 3 : num1 = num1 + num2
o Start 4 : num2 = num1 - num2
o Start 5 : num1 = num1 - num2
o Start 6 : PRINT num1, num2
o Start 7 : Stop
Practical No: 7
Aim: Program to swap two numbers.

Software Requirement: Operating System: Windows / Linux, TURBO C++ 3.2 / Eclipse CDT Luna

Hardware Requirement: Desktop PC with Dual Core Processor 2.0 GHz, RAM - 1GB, HDD -20GB

Theory:
Swapping of two numbers using third variable.
Swapping the value of variable ‘a’ and ‘b’ by using a third variable ‘temp’. While by using some
tricks you can make c program to swap two numbers without using third variable. Let’s look on
this c program to swap two numbers using third variable.
#include<stdio.h>
void main()
{
int a,b,c;
printf("enter two values\n");
scanf("%d%d",&a,&b);
printf(“Before Swapping a=%d and b=%d\n”,a,b);
c=a;
a=b;
b=c;
printf("After Swapping a=%d and b=%d\n ", a, b);
}

Output :
Enter two values
10 20
Before swapping a = 20 and b = 10
After swapping a=10 and b=20

In the above program, the temporary Variable c is assigned the value of first Number a.
Then, the value of first Number a is assigned to second Number b. Finally, the temporary
Variable c (which holds the initial value of first Number a) is assigned to second Number
b which completes the swapping process. Here, temporary Variable c is used to hold the value
of first Number and doesn't have any other use except that. You can also write the swapping
program without using temporary Variable c.
Swapping of two numbers without using third variable.
#include <stdio.h>
void main()
{
int a, b;
printf("Enter two values\n");
scanf("%d%d", &a, &b);
printf(“Before Swapping a=%d and b=%d\n”,a,b);
b = a + b;
a = b - a;
b = b - a;
printf("After Swapping a=%d and b=%d\n ", a, b);
}

Output:
Enter two values
10 20
Before swapping a = 20 and b = 10
After swapping a=10 and b=20

Conclusion: Hence, we have studied Program swapping of two numbers with and without using
third variable successfully.
Pr. No. 08 Algorithm & Flowchart to find factorial of given no.

 Algorithm :
o Step 1 : Start
o Start 2 : Read n
o Start 3 : Initialize counter variable i to 1 and fact to 1
o Start 4 : if i <= n go to step 5 otherwise goto step 7
o Start 5 : calculate fact = fact * i
o Start 6 : increment counter variable i and goto step 4
o Start 7 : Write fact.
o Start 8 : Stop

Flowchart :
Practical No: 8
Aim: Program to print square and factorial of an entered number using while loop.

Software Requirement: Operating System: Windows / Linux, TURBO C++ 3.2 / Eclipse CDT Luna

Hardware Requirement: Desktop PC with Dual Core Processor 2.0 GHz, RAM - 1GB, HDD -20GB

Theory:
C - Loops
You may encounter situations, when a block of code needs to be executed several number of
times. In general, statements are executed sequentially: The first statement in a function is
executed first, followed by the second, and so on. Programming languages provide various
control structures that allow for more complicated execution paths. A loop statement allows
us to execute a statement or group of statements multiple times. Given below is the general
form of a loop statement in most of the programming languages −

C programming language provides the following types of loops to handle looping


requirements.
S.N. Loop Type & Description

1 while loop

Repeats a statement or group of statements while a given condition is true. It tests


the condition before executing the loop body.

2 for loop
Executes a sequence of statements multiple times and abbreviates the code that
manages the loop variable.

3 do...while loop
It is more like a while statement, except that it tests the condition at the end of
the loop body.

4 nested loops
You can use one or more loops inside any other while, for, or do..while loop.

The While loop:


A while loop in C programming repeatedly executes a target statement as long as a
given condition is true.
Syntax:
The syntax of a while loop in C programming language is −

while(condition) {
statement(s);
}

Here, statement(s) may be a single statement or a block of statements. The condition may be
any expression, and true is any nonzero value. The loop iterates while the condition is true.
When the condition becomes false, the program control passes to the line immediately
following the loop.
Flow Diagram

Program using while loop:


#include<stdio.h>
void main ( )
{
int n, sq, fact = 1;
printf("Enter a number:\n ");
scanf("%d", &n);
sq = n * n;
while (n>1)
{
fact = fact* n;
n--;
}
printf("Square is=%d", sq);
printf("Factorial is=%d", fact);
}

Output:
Enter a number: 5
Square is = 25
Factorial is = 120
Conclusion: Hence, we have studied Program to find square and factorial of a given number
using while loop successfully.
Pr. No. 09 Algorithm & Flowchart to check given no is palindrome or not.
Algorithm:
Step 1: Start.
Step 2: Input one number num.
Step 3: print input number and assign it to temp variable.
Step 4: repeat following step while num > 0 then go to step 6.
Step 5: remainder(r)=num%10, sum=sum*10+r, num=num/10.
Step 6: if temp=s then print number is palindrome.
Step 7: if temp != s then print number is not palindrome.
Step 8: Stop.
Flowchart:
Practical No: 9
Aim: Program to check a number is palindrome number or not.

Software Requirement: Operating System: Windows / Linux, TURBO C++ 3.2 / Eclipse CDT Luna

Hardware Requirement: Desktop PC with Dual Core Processor 2.0 GHz, RAM - 1GB, HDD -20GB

Theory:
Palindrome Number: A palindrome number is a number such that we reverse it, it will not
change.
e.g. 121,212,323,111,222.
To check whether the number is palindrome or not first we reverse it and then compare the
number obtained with original, if both are same then number is palindrome otherwise not a
palindrome.
We had already studied conditional statements and looping statements like if-else statement
and while loop in previous practical. Now apply that concepts here to perform this practical.

Program to check a number is palindrome number or not.


#include<stdio.h>
void main()
{
int n,s=0,r,temp;
printf(“Enter a Number\n”);
scanf(“%d”,&n);
temp=n;
while(n>0)
{
r=n%10;
s=s*10+r;
n=n/10;
}
if(temp==s)
{
printf(“Number is Palindrome\n”);
}
else
{
printf(“Number is not Palindrome\n”);
}
}

OUTPUT:
Enetr a Number
212
Number is Palindrome
Conclusion: Hence, we have studied Program to find number is palindrome number or not
palindrome successfully.
Pr. No. 10 Algorithm & Flowchart to check given no is Armstrong or not.
Algorithm:
Step 1: Start.
Step 2: Input one number num.
Step 3: print input number and assign it to temp variable.
Step 4: repeat following step while num > 0 then go to step 6.
Step 5: remainder(r)=num%10, sum=sum+(r*r*r), num=num/10.
Step 6: if temp=s then print number is Armstrong.
Step 7: if temp != s then print number is not Armstrong.
Step 8: Stop.
Flowchart:
Practical No: 10
Aim: Program to check Armstrong Number.

Software Requirement: Operating System: Windows / Linux, TURBO C++ 3.2 / Eclipse CDT Luna

Hardware Requirement: Desktop PC with Dual Core Processor 2.0 GHz, RAM - 1GB, HDD -20GB

Theory:
Armstrong Number: In this program we ask the user to enter a number and then we used a
loop from one to the entered number and check it is an Armstrong number and if it is then the
number is printed on screen. Remember the number is Armstrong if the sum of cubes of
individual digits a number is equal to the number itself.
e.g. 371 is an Armstrong number as 33 +73 +13 =371
Some other Armstrong numbers are 0, 1, 153, 370, 407 etc.
Program to check Armstrong number.
#include<stdio.h>
void main()
{
int n,s=0,r,temp;
printf(“Enter a Number\n”);
scanf(“%d”,&n);
temp=n;
while(n>0)
{
r=n%10;
s=s+(r*r*r);
n=n/10;
}
if(temp==s)
{
printf(“Number is Armstrong\n”);
}
else
{
printf(“Number is not Armstrong\n”);
}
}

OUTPUT:
Enetr a Number
153
Number is Armstrong

Conclusion: Hence, we have studied Program to find Armstrong number successfully.


Practical No: 11
Aim: Program to check and generate Prime Numbers up to n.

Software Requirement: Operating System: Windows / Linux, TURBO C++ 3.2 / Eclipse CDT Luna

Hardware Requirement: Desktop PC with Dual Core Processor 2.0 GHz, RAM - 1GB, HDD -20GB

Theory:
Prime Number: A Prime Number can be divided evenly only by 1, or itself. And it must be a
whole number greater than 1. Example: 5 can only be divided evenly by 1 or 5, so it is a prime
number. But 6 can be divided evenly by 1, 2, 3 and 6 so it is NOT a prime number (it is a
composite number).

Flag: A 'flag' is a variable that is supposed to signalize something. Think about the word itself -
flags are used in sending signals. In our world of programming a basic boolean flag can either
be "up" or "down", either true or false. ... You can use flag = 2 but it's not much in line with the
concept.

Let’s consider the explanation before going to discuss program to check and generate Prime
numbers up to n.

If the for loop terminates when the test expression of loop i < n is false, the entered number is a
prime number. The value of flag is equal to 1 in this case.
If the loop terminates because of break statement inside the if statement, the entered number
is a non prime number. The value of flag is 2 in this case.

Program:
#include<stdio.h>
void main()
{
int i,n,flag,m;
printf(“Enter a Number\n”);
scanf(“%d”,&m);
n=1;
while(n<=m)
{
flag=1;
for(i=2;i<=n;i++)
{
if(n%i==0)
{
flag=2;
break;
}
}
if(flag==1)
{
printf(“%d\t”,n);
}
}
n++;
}
}

Output:
Enter a Number
10
1 2 3 5 7

Conclusion: Hence, we have studied Program to check and generate prime numbers up to n
successfully.
Pr. No. 12 Algorithm & Flowchart to find GCD of two numbers..
Algorithm:
Step 1: Start.
Step 2: Read the values of n1 & n2.
Step 3: While the value of n2 is not equal to n2 repeat step 4.
Step 4: if (n1>n2) n1=n1-n2 other wise n2=n2-n1.
Step 5: print the value of n1.
Step 6: Stop.
Flowchart:
Practical No: 12
Aim: Program to find GCD of two entered numbers.

Software Requirement: Operating System: Windows / Linux, TURBO C++ 3.2 / Eclipse CDT Luna

Hardware Requirement: Desktop PC with Dual Core Processor 2.0 GHz, RAM - 1GB, HDD -20GB

Theory:
Definition of GCD (Greatest common divisor) or HCF (Highest common factor):

HFC is also called greatest common divisor (GCD). HCF of two numbers is a largest positive
numbers which can divide both numbers without any remainder. For example HCF of two
numbers 4 and 8 is 2 since 2 is the largest positive number which can dived 4 as well as 8
without a remainder.
Logic of HCF or GCD of any two numbers:

In HCF we try to find any largest number which can divide both the number.
For example: HCF or GCD of 20 and 30
Both number 20 and 30 are divisible by 1, 2,5,10.
HCF=max (1, 2, 3, 4, 10) =10

Program:
#include<stdio.h>
void main()
{
int i,n1,n2,t;
printf(“Enter Two Numbers\n”);
scanf(“%d%d”,&n1,&n2);
t=n1>n2?n1:n2;
for(i=t;i>=1;i--)
{
if(n1%i==0 && n2%i==0)
{
printf(“GCD is : %d”,i);
break;
}
}
}

Output:
Enter Two Numbers
10 20
GCD is 10

Conclusion: Hence, we have studied Program to find GCD of entered numbers successfully.
Pr. No. 13 Algorithm & Flowchart to find maximum and minimum from n entered nos.
Algorithm:
Step1: Start
Step2: Declare an array a [10].
Step3: Enter the elements of an array.
Step4: Set max to first element of array i.e. Max=a[0].
Step5: For i=0 to i<=10
Step6: If Max<a[i] then
Max = a[i]
Step7: i++
Step8: goto step 5
Step9: Display the Maximum element in an array.
Step10: Stop.
Flowchart:
Practical No: 13
Aim: Program to find Maximum and Minimum from n entered numbers

Software Requirement: Operating System: Windows / Linux, TURBO C++ 3.2 / Eclipse CDT Luna

Hardware Requirement: Desktop PC with Dual Core Processor 2.0 GHz, RAM - 1GB, HDD -20GB

Theory:
Array: "Array is a group of the released data items that share a common name."
"Individual elements of array are known as the elements of an array." "The number specified
within the square bracket during the declaration variable is known as the array size,"
Properties of an array:
(4) The size of an array is the number of elements in each dimension.
(5) The number of dimensions.
(6) The type of an array is the data type of it's elements.
Declaration of an array:
Syntax:
data type name of array[size];
e.g. int sample[10];
Here, int=data type of all individual data items be variable
Sample= name of the array
10= 10 different integer values can be stored in this array.
element name[] The bracket tells the compiler the we are dealing with
array.
Sameple*0+sample*1+………………………………………………………………………………………………………………….
sample[8]
100 101 102 103 104 105 106 107 108

0Element value
The data elements are referred to as sample[0],sample[1],sample[2],sequentially upto

sample[8].
i.e.
sample[0]=100;
sample[1]=101;
sample[8]=108;
One-dimensional Array:

This array can be represent as a row or column or in other words it can be represent as in a
single dimensional array unable the user to design a set of similar data type. Syntax:

Data type array name[size];


e.g. int marks[12];
Advantages of array:
Easy to use.
Random Access: Any element in an array can be accessed immediately it it’s exact position in
the array is known.
Limitations of array:
Elements cannot be inserted into an array.
Arrays do not support copy assignment.
Constant data type.
Constant size.
Large free sequential block to accommodate large arrays.
Program:
#include<stdio.h>
void main()
{
int a[50],n,i,min,max;
printf(“How many elements you want to enter\n”);
scanf(“%d\n”,&n);
printf(“Enter elments %d”,n);
for(i=0;i<n;i++)
{
scanf(“%d”,&a[i]);
}
max = a[0];
min = a[0];
for (i=1;i<n;i++)
{
if(a[i]>max)
{
max=a[i];
}
if(a[i]<min)
{
min=a[i];
}
}
printf(“maximum =%d\n”,max);
printf(“minimum =%d\n”,min);
}

Output:
How many elements you want to enter
10
100 20 30 45 89 56 21 35 87 54
Maximum= 100
Minimum= 20

Conclusion: Hence, we have studied Program to find maximum and minimum from n entered
numbers successfully.
Practical No: 14
Aim: Program to print alternate numbers from n entered numbers

Software Requirement: Operating System: Windows / Linux, TURBO C++ 3.2 / Eclipse CDT Luna

Hardware Requirement: Desktop PC with Dual Core Processor 2.0 GHz, RAM - 1GB, HDD -20GB

Theory:
For performing this program we must have a knowledge of an array and for loop. Which
we already discussed in previous practical. Logic behind to print series of alternate numbers we
are just incrementing value of variable i by 2 instead of by 1 i.e. we are using I =i+2 in second for
loop instead of i++ as follows.

Program:
#include<stdio.h>
void main()
{
int a[10];
int i;
printf(“Enter 10 Elements\n”);
for(i=0;i<=9;i++)
{
scanf(“%d”,&a[i])’
}
printf(“Alternate Elements are\n”);
for(i=0;i<=9;i=i+2)
{
printf(“%d\n”,a[i]);
}
}

Output:
Enter 10 Elements
100 20 30 45 89 56 21 35 87 54
Alternate Elements are
100
30
89
21
87

Conclusion: Hence, we have studied Program to print alternate numbers from n entered numbers
successfully.
Practical No: 15
Aim: Program to search an element in an array using linear search.

Software Requirement: Operating System: Windows / Linux, TURBO C++ 3.2 / Eclipse CDT Luna

Hardware Requirement: Desktop PC with Dual Core Processor 2.0 GHz, RAM - 1GB, HDD -20GB

Theory:
Linear Search: Linear search is also called as sequential search. Linear search is a
method for finding a particular value in a list that consists of checking every one of its elements,
one at a time and in sequence, until the desired one is found.
e.g. if an array containing 5 elements as 20 30 35 21 56 and we want to find 35 is present in an
array or not this particular program returns us weather it is present or not? If present output
will be Element found if not output will be Element not found.

Program:
#include<stdio.h>
void main()
{
int a[10];
int i,n,flag=1;
printf(“Enter 10 Elements\n”);
for(i=0;i<=9;i++)
{
scanf(“%d”,&a[i]);
}
printf(“Enter Element to Search\n”);
scanf(“%d”,&n);
for(i=0;i<=9;i++)
{
if(a[i]==n)
{
flag=2;
break;
}
}
if(flag==1)
{
printf(“Element not Found\n”);
}
if(flag==2)
{
printf(“Element Found\n”);
}
}
Output:
Enter 10 Elements
100 20 30 45 89 56 21 35 87 54
Enter Element to Search
56
Element Found

Conclusion: Hence, we have studied Program to search an element in an array using linear
search method successfully.
Pr. No. 16 Algorithm & Flowchart for bubble sort technique.
Algorithm:
Step1: Start
Step2: Declare and initialize an array.
Step3: Declare the variables.
Step4: Bubble sort an elements from an array.
Step5: Display the result.
Step6: Stop.

Flowchart:
Practical No: 16
Aim: Program to print entered numbers in ascending order using Bubble sort technique.

Software Requirement: Operating System: Windows / Linux, TURBO C++ 3.2 / Eclipse CDT Luna

Hardware Requirement: Desktop PC with Dual Core Processor 2.0 GHz, RAM - 1GB, HDD -20GB

Theory:
Bubble Sort is an algorithm which is used to sort N elements that are given in a
memory for eg: an Array with N number of elements. Bubble Sort compares all the element
one by one and sort them based on their values.
It is called Bubble sort, because with each iteration the smaller element in the list bubbles
up towards the first place, just like a water bubble rises up to the water surface.
Sorting takes place by stepping through all the data items one-by-one in pairs and
comparing adjacent data items and swapping each pair that is out of order.

Let's consider an array with values {5, 1, 6, 2, 4, 3}


int a[6] = {5, 1, 6, 2, 4, 3};
int i, j, temp;
for(i=0;i<6;i++)
{
for(j=0;j<6-i-1;j++)
{
if( a[j] > a[j+1])
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
Bubble Sort Algorithm in Programming

Bubble sort algorithm starts by comparing the first two elements of an array and
swapping if necessary, i.e., if you want to sort the elements of array in ascending order and if
the first element is greater than second then, you need to swap the elements but, if the first
element is smaller than second, you mustn't swap the element. Then, again second and third
elements are compared and swapped if it is necessary and this process go on until last and
second last element is compared and swapped. This completes the first step of bubble sort.

If there are n elements to be sorted then, the process mentioned above should be
repeated n-1 times to get required result. But, for better performance, in second step, last and
second last elements are not compared becuase, the proper element is automatically placed
at last after first step. Similarly, in third step, last and second last and second last and third last
elements are not compared and so on.

Program:
#include<stdio.h>
void main()
{
int a[10];
int i,j,temp;
printf(“Enter 10 Numbers”);
for(i=0;i<=9;i++)
{
scanf(“%d”,&a[i]);
}
for(i=0;i<=9;i++)
{
for(j=0;j<=8-i;j++)
{
if(a[ j ]>a[ j+1])
{
temp=a[ j ];
a[ j ]=a[ j+1];
a[ j+1]=temp;
}
}
}
printf(“List in ascending Order\n”);
for(i=0;i<=9;i++)
{
printf(“%d\n”,a[i]);
}
}

Output:
Enter 10 numbers
100 20 30 45 89 56 21 35 87 54
List in ascending Order
20
21
30
35
45
54
56
87
89
100

Conclusion: Hence, we have studied Program to print entered numbers in ascending order
using bubble sort technique successfully.
Practical No: 17
Aim: Program to print entered numbers in ascending order using Selection sort technique.

Software Requirement: Operating System: Windows / Linux, TURBO C++ 3.2 / Eclipse CDT Luna

Hardware Requirement: Desktop PC with Dual Core Processor 2.0 GHz, RAM - 1GB, HDD -20GB

Theory:
Selection sort algorithm starts by compairing first two elements of an array and
swapping if necessary, i.e., if you want to sort the elements of array in ascending order and if
the first element is greater than second then, you need to swap the elements but, if the first
element is smaller than second, leave the elements as it is. Then, again first element and third
element are compared and swapped if necessary. This process goes on until first and last
element of an array is compared. This completes the first step of selection sort.

If there are n elements to be sorted then, the process mentioned above should be repeated n-
1 times to get required result. But, for better performance, in second step, comparison starts
from second element because after first step, the required number is automatically placed at
the first (i.e, In case of sorting in ascending order, smallest element will be at first and in case of
sorting in descending order, largest element will be at first.). Similarly, in third step, comparison
starts from third element and so on.

A figure is worth 1000 words. This figure below clearly explains the working of selection sort

algorithm.
Program:

#include<stdio.h>
void main()
{
int a[10];
int i,j,temp;
printf(“Enter 10 Numbers”);
for(i=0;i<=9;i++)
{
scanf(“%d”,&a[i]);
}
for(i=0;i<=9;i++)
{
for(j=i+1;j<=9;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf(“List in ascending Order\n”);
for(i=0;i<=9;i++)
{
printf(“%d\n”,a[i]);
}
}
Output:
Enter 10 numbers
100 20 30 45 89 56 21 35 87 54
List in ascending Order
20
21
30
35
45
54
56
87
89
100
Conclusion: Hence, we have studied Program to print entered numbers in ascending order
using selection sort technique successfully.
Pr. No. 18 Algorithm & Flowchart for addition of two matrices.
Algorithm:
Step1:Start
Step2: Accept the elements of 1st matrix i.e 2-D array.
Step3: Accept the elements of 2nd matrix i.e 2-D array.
Step4: Perform addition of 1st matrix and 2nd matrix element by element and store result in
3rd matrix
Step5: Display the 3rd matrix which gives you the addition of first two matrix
Step6: Stop.
Practical No: 18
Aim: Program to print addition, subtraction and multiplication of Matrices.

Software Requirement: Operating System: Windows / Linux, TURBO C++ 3.2 / Eclipse CDT Luna

Hardware Requirement: Desktop PC with Dual Core Processor 2.0 GHz, RAM - 1GB, HDD -20GB

Theory:
TWO DIMENSIONAL ARRAY:
The simplest form of multidimensional array is the two-dimensional array. A two-
dimensional array is, in essence, a list of one-dimensional arrays. To declare a two-dimensional
integer array of size *x+*y+, you would write something as follows −

type arrayName [ x ][ y ];

Where type can be any valid C data type and arrayName will be a valid C identifier. A
two-dimensional array can be considered as a table which will have x number of rows and y
number of columns. A two-dimensional array a, which contains three rows and four columns
can be shown as follows –

Thus, every element in the array a is identified by an element name of the form a[ i ][ j ],
where 'a' is the name of the array, and 'i' and 'j' are the subscripts that uniquely identify each
element in 'a'.
Initializing Two-Dimensional Arrays
Multidimensional arrays may be initialized by specifying bracketed values for each row.
Following is an array with 3 rows and each row has 4 columns.

int a[3][4] = {

{0, 1, 2, 3} , /* initializers for row indexed by 0 */

{4, 5, 6, 7} , /* initializers for row indexed by 1 */


{8, 9, 10, 11} /* initializers for row indexed by 2 */
};
Accessing Two-Dimensional Array Elements
An element in a two-dimensional array is accessed by using the subscripts, i.e., row index and
column index of the array. For example −

int val = a[2][3];

The above statement will take the 4th element from the 3rd row of the array. You can verify it
in the above figure. Let us check the following program where we have used a nested loop to
handle a two-dimensional array −

#include <stdio.h>

int main () {

/* an array with 5 rows and 2 columns*/

int a[5][2] = { {0,0}, {1,2}, {2,4}, {3,6},{4,8}};


int i, j;

/* output each array element's value */

for ( i = 0; i < 5; i++ ) {

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

printf("a[%d][%d] = %d\n", i,j, a[i][j] );


}
}

return 0;
}

When the above code is compiled and executed, it produces the following result –

a[0][0]: 0
a[0][1]: 0
a[1][0]: 1
a[1][1]: 2
a[2][0]: 2
a[2][1]: 4
a[3][0]: 3
a[3][1]: 6
a[4][0]: 4
a[4][1]: 8

As explained above, you can have arrays with any number of dimensions, although it is likely
that most of the arrays you create will be of one or two dimensions.
Program:
#include<stdio.h>
void main()
{
int a[3][3], b[3][3], add[3][3], sub[3][3], mul[3][3];
int i,j,k,s;
printf(“Enter Elements for First Matrix\n”);
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
scanf(“%d”,&a[i][j]);
}
}
printf(“Enter Elements for Second Matrix\n”);
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
scanf(“%d”,&b[i][j]);
}
}
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
add[i][j]=a[i][j]+b[i][j];
}
}
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
s=0;
for(k=0;k<=2;k++)
{
s=s+(a[ i ][ k ]*b[ k ][ j ]);
}
mul[i][ j]=s;
}
}
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
sub[i][j]=a[i][j]-b[i][j];
}
}

printf(“The Addition of the matrix is\n”);


for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
printf(“%d\t”,add[i][j]);
}
printf(“\n”);
}
printf(“The Multiplication of the matrix is\n”);
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
printf(“%d\t”,mul[i][j]);
}
printf(“\n”);
}
printf(“The Subtraction of the matrix is\n”);
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
printf(“%d\t”,sub[i][j]);
}
printf(“\n”);
}
}
Output:
Enter Elements for First Matrix
123456789
Enter Elements for Second Matrix
123456789
The Addition of the matrix is
2 4 6
8 10 12
14 16 18
The Multiplication of the matrix is
30 36 42
66 81 96
102 126 150
The Subtraction of the matrix is
0 0 0
0 0 0
0 0 0

Conclusion: Hence, we have studied Program to print addition, subtraction and multiplication
of matrices successfully.
Practical No: 19
Aim: Program to find length of string.(With and without using library function).

Software Requirement: Operating System: Windows / Linux, TURBO C++ 3.2 / Eclipse CDT Luna

Hardware Requirement: Desktop PC with Dual Core Processor 2.0 GHz, RAM - 1GB, HDD -20GB

Theory:
Strings: Strings are actually one-dimensional array of characters terminated by
a nullcharacter '\0'. Thus a null-terminated string contains the characters that comprise the
string followed by a null.

The following declaration and initialization create a string consisting of the word "Hello". To
hold the null character at the end of the array, the size of the character array containing the
string is one more than the number of characters in the word "Hello."

char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};

If you follow the rule of array initialization then you can write the above statement as follows −

char greeting[] = "Hello";

Following is the memory presentation of the above defined string in C

Actually, you do not place the null character at the end of a string constant. The C compiler
automatically places the '\0' at the end of the string when it initializes the array. Let us try to
print the above mentioned string −

#include <stdio.h>

int main () {

char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};

printf("Greeting message: %s\n", greeting );

return 0;

}
When the above code is compiled and executed, it produces the following result −

Greeting message: Hello

C supports a wide range of functions that manipulate null-terminated strings

Sr.No. Function & Purpose

1 strcpy(s1, s2);

Copies string s2 into string s1.

2 strcat(s1, s2);

Concatenates string s2 onto the end of string s1.

3 strlen(s1);

Returns the length of string s1.

4 strcmp(s1, s2);

Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0
if s1>s2.

5 strchr(s1, ch);

Returns a pointer to the first occurrence of character ch in string s1.

6 strstr(s1, s2);

Returns a pointer to the first occurrence of string s2 in string s1.


Program to find length of string with using library function.
#include<stdio.h>
void main()
{
char str[25];
int len;
printf(“Enter a String\n”);
gets(str);
len=strlen(str);
printf(“length of string is: %d”,len);
}

Output:
Enter a String
Mynameis
length of string is:8

Program to find length of string without using library function.


#include<stdio.h>
void main()
{
char str[25];
int i=o;
printf(“Enter a String\n”);
gets(str);
while(str[i]!= ‘\0’)
{
i++;
}
printf(“length of string is: %d”,i);
}

Output:
Enter a String
Mynameis
length of string is:8

Conclusion: Hence, we have studied Program to find length of string with and without using
library function successfully.
Practical No: 20
Aim: Program to demonstrating use of structures.

Software Requirement: Operating System: Windows / Linux, TURBO C++ 3.2 / Eclipse CDT Luna

Hardware Requirement: Desktop PC with Dual Core Processor 2.0 GHz, RAM - 1GB, HDD -20GB

Theory:
Structure:
Arrays allow to define type of variables that can hold several data items of the same kind.
Similarly structure is another user defined data type available in C that allows to combine data
items of different kinds. Structures are used to represent a record. Suppose you want to keep
track of your books in a library. You might want to track the following attributes about each
book −

 Title
 Author
 Subject
 Book ID
Defining a Structure
To define a structure, you must use the struct statement. The struct statement defines a new
data type, with more than one member. The format of the struct statement is as follows −

struct [structure tag] {


member definition;
member definition;
...
member definition;
} [one or more structure variables];

The structure tag is optional and each member definition is a normal variable definition, such
as int i; or float f; or any other valid variable definition. At the end of the structure's definition,
before the final semicolon, you can specify one or more structure variables but it is optional.
Here is the way you would declare the Book structure −

struct Books {
char title[50];
char author[50];
char subject[100];
int book_id;
} book;

Accessing Structure Members


To access any member of a structure, we use the member access operator (.). The member
access operator is coded as a period between the structure variable name and the structure
member that we wish to access. You would use the keyword struct to define variables of
structure type.

Array of Structure :

Structure is used to store the information of One particular object but if we need to store such
100 objects then Array of Structure is used.

Example :

struct Bookinfo
{
char[20] bname;
int pages;
int price;
}Book[100];

Explanation :

1. Here Book structure is used to Store the information of one Book.

2. In case if we need to store the Information of 100 books then Array of Structure is used.
3. b1[0] stores the Information of 1st Book , b1[1] stores the information of 2nd Book and
So on We can store the information of 100 books.
Accessing Pages field of Second Book :

Book[1].pages

Program:
#include<stdio.h>
struct person
{
char name[25];
int age;
float weight;
};
void main()
struct person p1;
int i;
printf(“Enter Name, Age and Weight of Person:\n”);
gets(p1.name);
scanf(“%d%f”,&p1.age,&p1.weight);
printf(“Person Name: %s\n”);
printf(“Person Age: %d\n”,p1.age);
printf(“Person Weight: %f\n”,p1.weight);
}

Output:
Enter Name, Age and Weight of Person:
xyz
35
65
Person Name:xyz
Person Age:35
Person Weight:65

Conclusion: Hence, we have studied Program to demonstrating use of structures successfully.

S-ar putea să vă placă și