FOC Lab Manual
FOC Lab Manual
FOC Lab Manual
INTRODUCTION TO MS-WORD
OBJECTIVES:
1. To expose the students about the Graphical User Interface in Windows Operating Systems.
2. Understanding about word processors and learn, how to prepare documents by using
Microsoft Word.
PRELIMINARY EXPLANATIONS:
Nowadays, the Windows X Operating System is the most popular operating systems
for PCs. Example: Windows 95, Windows 98, Windows 2000 and Windows XP. One of the
most important reasons of this popularity is they are having rich in Graphical User Interface
(GUI) support. GUI makes the people easy to interact with the system. GUI has menus, dialog
boxes, icons, etc, which allows the users to work easy with the system because people
recognize and respond to graphic representations of concepts much more readily than if they
have to read and write word.
As the Windows Operation Systems have GUI, applications written for them have
very good graphical interfaces, compared with the older applications written in the time of
non-GUI operating systems like MS-DOS. Nowadays any person in a modern society, without
having to have job related with computers, is, and should be, able to use many different
applications on PCs. These applications include word processors, spreadsheets, etc.
Word Processors
Word processor is an application program which is used for creating, editing, printing
and saving documents. By using a word processor, it is possible to prepare reports,
announcements, journals, books, etc. Nowadays, Microsoft Word is the most popular word
processor for office applications. This product has so many features and we will discuss some
of the features in this experiment. We can learn more features by ourselves by using Help
Menu.
Typing Text:
When you start Word, a new, empty document file is automatically created, so you
face an empty page. You can start typing anything as you like. They will be seen on the
screen. Use the Enter key only at the end of Paragraphs! (Not at the end of each sentence or
line.)
Using the Menus:
On the top of the screen there is a bar that contains many drop-down menus (File,
Edit, View, etc). If you want to reach the menu bar with your keyboard, you can use the Alt
key. A lot of operations are offered in these menus, and you can select any of them to do the
corresponding operation, without having to write any commands. It is not possible to explore
all the menu items in one experiment; however you can learn them in time, easily, by yourself.
For this purpose, you may find the Help menu very helpful. Just use it. It is for you!
Department of CSE, PSNACET, Dindigul
By default, the font type is usually Times New Roman. You can select any other type.
Ex:
Department of CSE, PSNACET, Dindigul
Paragraph Alignment:
You can arrange the paragraphs as left aligned, right aligned, centered and justified
(i.e. aligned from both left and right margins) by clicking one of the corresponding buttons
shown above.
Ex:
The first paragraph in the Preliminary Explanations part is left aligned. The second
one is right aligned, and the third is centered. All the other paragraphs in the rest of the
lab sheet are justified.
Selecting Text:
With mouse
1. Move your mouse pointer to the beginning of the text you want to select
2. Click and hold the left mouse button
3. Move the mouse to the end of the text you want to select.
4. Release the mouse button.
With keyboard
1. Move the blinking cursor, by using the arrow keys, to the beginning of the text you
want to select
2. Press and hold the shift button
3. Move the cursor to the end of the text that you want to select
4. Release the shift button
When a text is selected, you see it highlighted. Now, you can do many operations on the
selected text. (ex: move (cut~paste), copy (copy~paste), underline, change size, type, color,
etc)
Copying or Moving a selected text:
To copy or move a text the first thing you have to do is to select it. Then click the
Copy or Cut item from the Edit menu depending on what you want to do. (Cut is used to
move). Move the cursor to the position that you want to insert the selected text. Click Paste in
the Edit menu.
Using Shortcuts and Access Keys:
Department of CSE, PSNACET, Dindigul
Many items in the menus have shortcuts that can be accessed by keyboard. Instead of
clicking these items with your mouse, sometimes you may prefer to use the shortcuts. For
example the shortcut for Save is Ctrl + S. Instead of clicking File and then Save, you can
hold down Ctrl key and press S. This will do the same thing.
Each menu item has one letter underlined. This is the access key of the item. If you get
bored of using mouse, just hold down the Alt key and press the corresponding letters to
travel in the menus.
Ex:
(a+b)2 = a2 + b2+ 2 * a * b
sin2 + cos2 = 1
1.0E-6 or 1.0^-6 or 0.000001 sec.
2.34 * 10-5 + 5.67 * 10-9
Constructing Tables
Table creation
Select from menu bar as Table Insert Table. And select no of rows and columns
required.
Split cells:
Select the cell to split, right click that cell and select the split cell option and give the
no of rows and/or columns.
Merge cells:
Select the number of rows or columns and right click it, select the merge cell option.
Insert Rows:
Place cursor in particular cell, Select from menu bar as
Table insert Rows Above/Rows Below.
Insert Columns:
Place cursor in particular cell, Select from menu bar as
Table insert Columns To The left / Columns to the right.
EX. NO.: 1
Aim
To create an advertisement using various drawing tools in MS-Word.
Procedure
1.
2.
3.
4.
5.
6.
7.
8.
Result
Thus the advertisement is created in MS-Word using various drawing tools.
EX. NO.: 2
Aim
To demonstrate the concept of mail merging in MS-Word.
Procedure
Creation of Mail Merge
1. Make the main document active, where we want to produce the letter.
2. Choose Mail Merge option from the Tools menu. Now the Mail Merge helper
dialog box appears.
3. In that box, click on the create button and choose the option Form Letters.
4. Click on the Form Letters because we are merging data source to document in
active window.
Creation of Data Source
5. Click on the Get data button and choose Create Data Source option.
6. Select the required fields for the data source ( Eg. Address fields) and then click ok
and then click OK and then save the data source with file name.
7. Now give data to that fields and Click OK, now your Data Source File is created.
8. The Mail Merge toolbar is automatically shown in your word document
Insertion of field names in the main document by using the mail merge toolbar
9. Place the cursor where we want to insert, and then click on the Insert Merge Field
button.
10. Choose First Field from the list box to tell Word to place the information from the
first row of the data source file in the form letter. The First merge Field is inserted
in the document.
11. Similarly we can insert other fields at the place we want.
12. If we want to merge data source in form letters then click on the Merge button on
Mail Merge helper dialog box. The resultant merged document will be displayed.
Ordering Books
From
Mr. Ragul Singh,
IFCI Towers, Nehru Place,
New Delhi - 19
011-2556890
To
<Address Field>
Dear sir,
I shall be thankful to you send me the following books by RMS at an early
date. Also please give the nominal discounts as mentioned in your booklist.
Book Title
Mastering c++
The Java2
Windows
Programming
Programming
Visual c++
Author
No.of copies
K.R.Venugpopal
50
RajkumarBuyya
T.Ravishankar
Complete Refernce
30
Herbert Schildt
Price
Rs. 250
Charles Petzold
40
Rs. 300
David J.Kruglinski
George Sherherd
Scot Wingo
20
Rs. 400
Rs.350
Dear sir,
Author
No.of copies
K.R.Venugpopal
50
RajkumarBuyya
T.Ravishankar
Complete Refernce
30
Herbert Schildt
Price
Rs. 250
Charles Petzold
40
Rs. 300
David J.Kruglinski
George Sherherd
Scot Wingo
20
Rs. 400
Rs.350
Author
No.of copies
K.R.Venugpopal
50
RajkumarBuyya
T.Ravishankar
Complete Refernce
30
Herbert Schildt
Price
Rs. 250
Charles Petzold
Rs. 300
40
Rs.350
David J.Kruglinski
George Sherherd
Scot Wingo
20
Rs. 400
10
EX. NO. :3
Aim:
To draw a flow chart to find factorial of a number using MS-Word.
Algorithm:
1. Read a number to find the factorial of it
2. Assign value 1 to variable M, F
3. Multiply the vlaues of varible, F with variable, M
4. Check whether value of variable, M is equal to value of variable, N
4.a. Display the Factorial of the given number, N
4.b. Otherwise, increment the value of the varible, M by 1
Sample Output:
Result:
Thus, the flow chart to compute Factorial of a given number was constructed
successfully.
Preliminary Explanations:
In the following experiment you will learn about Excel, a very popular
spreadsheet. This is also application software. Spread sheets can be used for evaluating
results of surveys and elections, preparing statistical summaries, taking financial
decisions, making accounting calculations, etc
When you start Excel, we will find a worksheet (tables) in which you can write
text, numbers or formulas. Texts can be used to create titles or labels, numbers are the
data that we obtain from somewhere, and formulas are used to do some operations or
calculations on these numbers.
Tables are formed of columns and rows. Each box in a table (the intersection of a
row and a column) is called a cell. Columns are named as A, B, C, D, etc, and rows are
named as 1, 2, 3, 4, etc Cells are named as A1, A2,, B1, B2,,, Z1, Z2,
depending on the location they are found.
Ranges:
In an Excel Worksheet there are 16384 rows and 256 columns. Range is a group
(rectangular block) of cells. A column or a row, for example, is a range, part of a column
or a row is also a range. Ranges are represented with two cells: The first and the last.
Ex:
A1:A5 is a range of 5 cells in the A column. More strictly, A1, A2, A3, A4, A5
B2:E4 is a range of 12 cells. The cells are B2, B3, B4, C2, C3, C4, D2, D3, D4, E2,
E3, E4.
Formulas:
In Excel, you can write formulas to do some calculations on cells or ranges of
cells. For example, you can add the values in F1 and F2, and write the result into F3, by
writing =F1+F2 into F3.
Functions:
In these formulas you can also use some functions like SUM(), AVERAGE(),
SIN(), etc. For example, instead of =F1+F2, you may use = SUM(F1:F2). Of course for
adding only two numbers it may not be a very bright idea to use the function SUM(), but
if we have many numbers to add, it will certainly be very useful.
If we want to see the complete list of these functions, just click the Function item in the
Insert menu.
Writing Formulas:
To write a formula into a cell, you should first put an equal sign (=) and then write
the corresponding formula.
Ex: Write 1, 2, 3, 4, 5 into A1, A2, A3, A4 and A5 respectively. Then write
=SUM(A1:A5) into the cell A6.
Do you see the result 15 in A6? Now change the number in any of the cells in range
A1:A5 and see that the value of A6 is automatically recalculated.
Copying a formula to other cells:
Suppose you want to add the numbers in B1:B5, also. Instead of writing
=SUM(B1:B5) into B6, you can just copy A6 to B6. You know that the formula in A6 was
=SUM(A1:A5) but it will be automatically turned to =SUM(B1:B5) during the copy
operation.
Constant Addressing vs Relative Addressing:
As you see in the section above, when you copy some formula to another cell, the
range in it automatically changes. This is because there is relative addressing. To prevent
this, you can use the $ sign in front of the cell addresses. Suppose that you write
=SUM($A$1:$A$5) in A6. When you copy it to B6, the formula will remain exactly the
same, and you will see 15 in both A6 and B6.
Using the Help Menu:
Sometimes, you may forget how to do something, or you may want to learn
something new which would be very useful for what you are intending to do. In these
cases, you can just refer to the Help menu. Try it! It is like a teacher who is always ready
to answer your questions and solve your problems.
Sorting a column:
You can sort some rows (or some range of cells) in the table, by any column, in
ascending or descending order. To do this, just select a range of cells, click the Sort item
in the Data menu, and select the columns by which you want to do the sort. It is so
simple!
Inserting Charts into your worksheet:
Charts (graphs) are visual representations of numbers. Instead of seeing many
numbers all together, we usually prefer to see a chart that represents everything visually.
Whenever there is an election, for example, all the TV channels show the results by using
some kind of graphs. It is more easily understandable to human beings. To insert a chart,
just select the corresponding columns (or ranges), and click Chart item in the Insert
menu. Then select the chart type that you want to use and click Next two times. Now
enter the Chart title, and select a data label style, if you want, and click Next again. Be
sure that As object in: Sheet 1 is selected and click Finish.
Now the chart is inserted into your worksheet. Change the values in some of the
cells of the range that was used to prepare the chart. Realize that the chart is
automatically updated to reflect these changes.
Experimental Work:
1. Prepare the following table: (Course should be in A1!)
Course
Credit
Grade Worth Credit X
Worth
CMPE101
4A
4
PHYS101
4B
3
ELT101
3 B2.7
MATH151
4 B+
3.3
MATH163
3 A3.7
Total Credit X
Worth:
Total Credit:
Average (GPA):
2.
3.
4.
5.
6.
7.
8.
CMPE101
%25
MATH151
%20
MATH163
%17
EX. NO. :4
CMPE101
PHYS101
MATH163
PHYS101
%25
MATH151
ELT101
Aim:
To create a spread sheet to analyze the marks of the students of a class and
also to create appropriate charts
Algorithm
Step 1:
Step 2:
Step 3:
Step 4:
"THIRD
`
PowerPoint is a presentation tool helps you create eye catching and effective
presentation in manner of minutes. A presentation comprises of individual slides arranged
in a sequential manner. Normally, each slide would cover a brief topic.
GETTING STARTED
Open PowerPoint and you will be prompted by a dialog box with four choices.
Each of these options are explained on this page. If Powerpoint is already open or this
box does not appear, select File\New menu bar
AUTOCONTENT WIZARD
The Auto content wizard provides templates and ideas for a variety of presentation
types. Page through the wizard by clicking the Next button on the bottom of each page
after making necessary choices
DESIGN TEMPLATE
Select Blank Presentation to build the presentation from scratch with no preset
graphics
VIEWS
PowerPoint gives you four screen layouts for constructing your presentation in
addition to the slideshow. You can select the page view by clicking the buttons just
above formatting toolbar and the bottom of the page
NORMAL VIEW
This screen is split into three sections showing the presentation outline on the left,
the slide in the main window, and notes at the bottom.
SLIDEVIEW
The slide view displays each slide on the screen and is helpful for adding images,
Formatting text and adding background styles.
OUTLINEVIEW
The presentation outline is displayed on the majority of the screen with small
windows for the slide and notes. This view is recommended for editing text.
EX. NO. :5
Aim
To create the presentation for the department using Powerpoint using animation,
design templates and Effective presentation
Algorithm
Step 1: Open a Blank Presentation
Step 2: Choose WordArt option from the picture in insert menu or click on WordArt
button from the drawing tool bar.
Step 3: From the WordArt gallery choose the appropriate style then click OK.
Edit WordArt Text dialog box appears, type your text here, then click OK.
Step 4: Choose Clipart option from the picture in Insert Menu selection or click
on Clipart button from the Drawing Tool Bar
Step 5: Insert clipart dialog box appears, from there choose required clipart then
ok. Follow the above steps to complete below slides
PSNA COLLEGE OF
ENGINEERING AND
TECHNOLGY
Result
Thus, the presentation for the department using power point using animation,
design templates and Effective presentation is created
EX. NO. :6 COMPUTATION OF AGE IN TERMS OF YEARS,
MONTHS AND DAYS
Aim
To write a C program to find the age in terms of years, months and days.
Algorithm
Step 1: Read the date of birth through bd,bm,by.
Step 2: Read the current date through cd, cm,cy.
Step 3: btotal <-(bd+(bm*30)+(by*366)).
Step 4: ctotal <-(cd+(cm*30)+(cy*366)).
Step 5: total <- ctotal- btotal.
Step 6: days<- total % 30.
Step 7: months<- total / 30.
Step 8: years<- months % 12.
START
Step 9: Print total, years, months, days.
Flowchart
READ bd, bm, by
cd,cm,cy
Compute:
btotal <-(bd+(bm*30)+(by*366))
ctotal <-(cd+(cm*30)+(cy*366))
total <- ctotal- btotal
Compute:
Years<-total/365;
Months<-(total%365)/30;
Days<-(total%365) %30;
Display years,
months, days,
total
STOP
Program
#include<stdio.h>
#include<conio.h>
void main()
{
int bd,bm,by,cd,cm,cy;
int btotal,ctotal,total,days,month,months,years;
clrscr();
printf("enter your date of birth in the order of date,month and years\n");
scanf("%d%d%d",&bd,&bm,&by);
printf("enter current date in the order of date,month and year\n");
scanf("%d%d%d",&cd,&cm,&cy);
btotal=(bd+(bm*30)+(by*365));
ctotal=(cd+(cm*30)+(cy*365));
total=(ctotal-btotal);
printf("total days are: %d \n",total);
years=total/365;
months=(total%365)/30;
days=(total%365)%30;
printf("\n%d your age in \n %d years \n %d months \n %d days
\n",total,years,months,days);
getch();
}
Sample Input and Output:
Enter your date of birth in order of date, month and years
01 01 1983
Enter current date in order of date, month and years
11 08 2009
Total days are: 9710
Your age is
26 years
7 months
10 days
Result:
Thus the C program to find the age in terms of years, months and day was written
and executed successfully.
DECISION MAKING
C program is a set of statements which are normally executed sequentially in the
order in which they appear. In some situations ,change the order of execution of
statements based on certain conditions This involves a kind of decision making to
see whether a particular condition has occurred or not and then direct the computer to
execute statements.
C language possesses such decision making capabilities and supports the
following statements known as control or decision making statements.
1. if statement
2. switch statement
3. conditional operator statement
4. goto statement
if Statement:
The, if statement is a powerful decision making statement and is used to control
the flow of execution of statements. It allows the computer to evaluate the expression first
and then depending on whether the value of the expression is true or false. The, if
statement may be implemented in different forms depending on the complexity of
conditions to be tested.
1. Simple if statement
2. if.else statement
3. Nested ifelse statement
4. else if ladder
Simple if statement:
General form:
if (test expression)
{
statement-block)
}
statement-x;
The ifElse statement:
The if..else statement is an extension of the simple if statement.
General form:
if(test expression)
{
True-block statements(s)
}
else
{
False-block statements(s)
}
statement-x
Nesting of if else statements
When a series of decisions are involved, more than one ifelse statement is
nested.
General Form:
if (test condition 1)
{
if (test condition 2)
{
statement -1;
}
else
{
statement -2;
}
}
else
{
statement-3;
}
statement x;
The else if ladder:
A multipath decision is a chain of ifs in which the statement associated with each
else is an if.
General form:
If(condition 1)
Statement-1;
Else if (condition 2)
Statement-2;
Else if (condition 3)
Statement-3;
Else if (condition n)
Statement-n;
else
default statement;
statement-x;
SWITCH STATEMENT:
The C Language has a built-in multiway decision statement known as a switch.
The switch statement tests the value of a given variable (or expression) against a list of
case values and when a match is found, a block of statements associated with that case is
executed.
General form
Switch(expression)
{
case value-1:
block-1
break;
case value-2:
block-2
break;
..
..
default:
default-block
break;
}
statement-x;
Label:
Statement;
--------goto label;
LOOPING STATEMENT
Most real programs contain some construct that loops within the program,
performing repetitive actions on a stream of data or a region of memory. There are several
ways to loop in C. The three common methods of looping are,
While loop
Do/while loop
For loop
while Loop:
The while loop continues to loop until, the conditional expression becomes false.
The condition is tested upon entering the loop. In a While Loop, the expression is
evaluated when the program enters the top of the loop. If the expression is true, the
statements within the loop are executed. When the bottom of the code block is reached,
program flow is directed back to the top of the loop and the conditional is re-evaluated.
As long as the conditional is evaluated as true, the process repeats. IF the conditional is
evaluated as false, then the code block is skipped. The syntax for the while loop is shown
below,
while (expression)
{
...block of statements to execute...
}
The flowchart for a While Loop is shown below.
do / while Loop:
The Do/While loop is very similar to the while loop. The major difference is that
the expression or conditional is tested at the bottom of the loop. This means that the body
of the loop is executed at least one time regardless of the results of the conditional. If the
result of the conditional is true, then the loop body is repeated. This process is repeated
until the conditional is evaluated as false. The flow chart and the syntax for the Do/while
loop is given below,
do
{
Perform the statements located between the braces
} while (this conditional expression is true);
for loop:
The For Loop is traditionally used to perform a task or repetitive event for a
known number of iterations or in other words, it is used to implement count-controlled
loops. For loop is made up of an initial condition, a conditional expression, a modifier,
and the body or the code block.
The difference or characteristic that determines that it is a For Loop is that the
initial condition, conditional, and modifier all refer to the same variable. The
syntax for the FOR loop is given below along with the flow chart,
EX. NO. : 7
Aim
To write a simple menu driven calculator program using switch statement.
Algorithm
Step 1: Display the menu
Step 2: Read the two variables a & b
Step 3: Enter the option code
Step 4: Evaluate option code with case constants
Step 4.1: case=1
c=a+b
print c
goto step 6
Step 4.2: case=2
c=a-b
print c
goto step 6
Step 4.3: case=3
c=a*b
print c
goto step 6
Step 4.4: case=4
c=a/b
print c
goto step 6
Step 4.5: case=5
Exit
Step 5: Entered case option is invalid code then print Invalid operation Code
Flowchart
Start
Q
Display the Menu
Read choice
No
If n<=4
& n>0
A
Yes
Read a,b
No
Swit
ch
n
Yes
Yes
Case
1
No
c=a+b
Print c
Yes
Case
2
No
c=a-b
Print c
C
Yes
c=a*b
Case
3
No
Print c
Yes
c=a/b
Case
4
No
Print c
Yes
Case
5
A
No
Yes
while
(ch<=5)
No
Print invalid
operation
Stop
Program
#include <stdio.h>
#include <conio.h>
void main()
{
int a,b,c,ch;
clrscr();
do
{
printf("****MENU****\n");
printf("1.Addition\n");
printf("2.Subtraction\n");
printf("3.Multiplication\n");
printf("4.Division\n");
printf("5.exit\n");
printf("enter your choice\n");
scanf("%d",&ch);
if(ch<=4&&ch>0)
{
printf("enter two numbers\n");
scanf("%d%d",&a,&b);
}
switch(ch)
{
case 1:
c=a+b;
printf("Addition:%d\n",c);
break;
case 2:
c=a-b;
printf("Subtraction:%d\n",c);
break;
case 3:
c=a*b;
printf("Multiplication:%d\n",c);
break;
case 4:
c=a/b;
printf("Divition:%d\n",c);
break;
case 5:
exit(0);
break;
default:
printf("invalid operation code");
}
}while(ch<=5);
getch();
}
EX.NO.:8
Aim
To write a C program to print multiplication table for the given number.
Algorithm
Step 1: Read which multiplication table you want m
Step 2: Read the upper limit n.
Step 3: Assign 1 to a.
Step 4: Assign m to total.
Step 5: for i=1 to n
Step 5.1:Print a,m,total
Step 5.2: a <- a+1.
Step 5.3: total <- total+m
Flowchart:
Program
#include<stdio.h>
#include<conio.h>
void main()
{
int n,m,i,total,a=1;
clrscr();
printf("enter which multiplication table you want?\n");
scanf("%d",&m);
printf("enter limit of the multiplication table\n");
scanf("%d",&n);
total=m;
for(i=1;i<=n;i++)
{
printf("%d x %d = %d\n", a,m,total);
a=a+1;
total=total+m;
}
getch();
}
Result
Thus the C program to print multiplication table for the given number was written
and executed successfully.
EX.NO.:9
Aim
To check the given number is Palindrome or not.
Algorithm
Step 1: Enter the number
Step 2: Set a loop upto the number is not equal to zero.
Step 3: remnum%10
Step 4: sumsum+rem
Step 5: rnumrnum*10+rem
Step 6: numnum/10
Step 7: After the end of the loop print the sum and reverse number of the digit.
Step 8: Find whether the reverse number is equal to the given number or not.
If equal the number is palindrome.
Step 9: If not equal the given number is not palindrome.
Flowchart
Start
Read
num
a = num
sum =0
rnum = 0
No
If (num!
=0)
Yes
Rem =num % 10
Sum =sum+rem
Rnum = rnum*10 + rem
num = num /10
If
(a==rnu
m)
Print
sum,re
m
No
Yes
Print
Number is
palindrome
Start
Print Number
is not a
palindrome
Program
#include <stdio.h>
#include <conio.h>
void main()
{
int a,num,sum=0,rnum=0,rem;
clrscr();
printf("Enter the number\n");
scanf("%d",&num);
a=num;
while(num!=0)
{
rem=num%10;
sum=sum+rem;
rnum=rnum*10+rem;
num=num/10;
}
printf("The reverse number of the %d is %d\n",a,rnum);
if(a==rnum)
printf("%d is palindrome\n",a);
else
printf("%d is not a palindrome\n",a);
getch();
}
Result
Thus the palindrome program was written and executed successfully.
EX.NO.:10
Aim
To find whether the given number is palindrome or not.
Algorithm
Step 1: Read the number
Step 2: i2
Step 3: Is i<=num-1 else goto end
Step 4: Is num%i=0
Step 4.1: print not prime.
Step 4.2: goto end
Step 5: i i+1
Step 6: goto Step 4
Step 7: Is i=num print prime.
Flowchart
Start
Sta
Enter the number
No
While(i
<=num
-1)
i++
Print Prime
Yes
No
If(n%i
= = 0)
Yes
Print not prime
Stop
Program
#include <stdio.h>
#include <conio.h>
void main()
{
int num,i=2;
clrscr();
printf("Enter the number\n");
scanf("%d",&num);
while(i<=num-1)
{
if(num%i==0)
{
printf("%d is not a prime number\n",num);
break;
}
i++;
}
if(i==num)
printf("%d is prime number\n",num);
getch();
}
Sample Input and Output:
Enter the number
7
7 is prime number
Enter the number
9
9 is not a prime number
Result
Thus, the prime number program was written and executed successfully.
EX.NO.:11
Aim
To write a program to print the Fibonacci series.
Algorithm
1. Read the value r.
2. Assign a=0, b=1 and c=0.
3. Print the value of a and b.
4. Compute c= a + b.
5. Check the condition (c<=r), go to step 6. Otherwise step 7.
6. Print the value of c.
7. Assign a=b and b=c.
8. Repeat the steps 4 to 7 until (c<=r)
Flowchart
Program
#include<stdio.h>
void main( )
{
int i,a,b,c,r;
clrscr( );
printf(Enter the range:);
scanf(%d,&r);
a=0;b=1;
printf(%d,a);
printf(\n%d,b);
printf(\n);
c=0;
do
{
c=a+b;
if(c<=r)
printf(%d\n,c);
a=b;
b=c;
}
while(c<=r);
getch( );
}
Sample Input and Output:
INPUT:
Enter the range : 10
OUTPUT:
0
1
1
2
3
5
8
Result
Thus, the program for Fibonacci series is implemented.
EX.NO.:12
Aim
To write a program to evaluate sine series sin(x) = x x3/3! +x5/5!+.+xn/n!
Algorithm
1. Read the values of n and angle
2. Compute x = angle (angle*3.141592/180)
3. Assign term=x, sum=x and i=1
4. Repeat the steps 5 to 7 until i<n.
5. Compute term =-(term*x*x) / (2*i)*(2*i+1)
6. Compute the sum =sum +term
7. Compute i=i+1
8. Display the values of angle, x and sum.
Flowchart
Program
#include<stdio.h>
#include<conio.h>
void main( )
{
int i ,n ;
float x,angle,term,sum;
clrscr( );
printf(\n Enter number of terms and angle:);
scanf(%d%f,&n,&angle);
x=angle*3.141592/180;
term =x;
sum=x;
for(i=1;i<n;i++)
{
term=((-term*x*x)/((2*i)*(2*i+1)));
sum=sum+term;
}
printf(\n Entered angle =%f,angle);
printf(\n Its radian value =%f,x);
printf(\n Value of sin(x) =%f,sum);
getch( );
}
Sample Input and Output:
INPUT:
Enter number of terms and angle:3
90
OUTPUT:
Entered angle =90.000000
Its radian value =1.570796
Value of sin(x) =1.004525
Result
Thus the program for the sine series is generated and implemented.
EX.NO.:13
Aim
Program
#include<stdio.h>
#include<conio.h>
void main( )
{
int i ,n ;
float x,angle,term,sum;
clrscr( );
printf(\n Enter number of terms and angle:);
scanf(%d%f,&n,&angle);
x=angle*3.141592/180;
term =1;
sum=1;
for(i=1;i<n;i++)
{
term=((-term*x*x)/((2*i)*(2*i-1)));
sum=sum+term;
}
printf(\n Entered angle =%f,angle);
printf(\n Its radian value =%f,x);
printf(\n value of cos(x) =%f,sum);
getch( );
}
Sample Input and Output:
INPUT:
Enter number of terms and angle:3 90
OUTPUT:
Entered angle =90.000000
Its radian value =1.570796
value of cos(x) =0.019969
Result
Thus the program for the cosine series is generated and implemented.
EX.NO.:14
Aim
To write a program to find the largest and smallest number using Array.
Algorithm
1. Read the number of values (n).
2. Initialize the iterative variable i=0.
3. Repeat the steps 4 & 5 until no. of values minus 1(n-1).
4. Read the elements in an array.
5. Increment the iterative variable.
6. Assign the first element of an array to the small and big variables.
7. Repeat the steps 8 to 10, until no. of values minus 1 (n-1).
8. If the array element is greater than big element,
Assign the element to the big variable.
Otherwise
goto step 10.
9. If the array element is smaller than small element,
Assign the element to the small variable.
Otherwise
goto step 10.
10. Increment the iterative variable.
11. Display the biggest element in the list.
12. Display the smallest element in the list.
Flowchart
start
Next i
Check the
Array
element>bi
g
Y
Assign the element to the big
variable
Next i
Stop
Check the
Array
element<sm
al
Y
Assign the element to the
small variable
Program
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,a[10],big,small;
clrscr();
printf(Enter the size of the list \n);
scanf(%d,&n);
printf(Enter the elements\n);
for(i=0;i<n;i++)
scanf(%d,&a[i]);
small=a[1];
big=a[1];
for(i=1;i<n;i++)
{
if(a[i] >big)
big =a[i];
else if (a[i] <small)
small =a[i];
}
printf(\n Biggest element in the list:%d\n,big);
printf(\n smallest element in the list:%d \n,small);
getch( );
}
Sample Input and Output:
INPUT:
ENTER THE SIZE OF THE LIST
5
Enter the elements
65
45
12
85
68
OUTPUT:
Biggest element in the list : 85
Smallest element in the list : 12
RESULT:
Thus the program for finding largest and smallest number using array is
implemented.
.
EX.NO.: 15
Aim
To write a C program to sort the numbers in an array in Ascending and
Descending order.
Algorithm
1. Read the number of values (n).
2. Initialize the iterative value (i) to 0.
3. Repeat the steps 4 to 5 until the number of values minus 1 (n-1).
4. Read the elements in an array a[i].
5. Increment the iterative value.
6. Initialize the iterative value (i) to 0.
7. Repeat the steps 8 to 12 until the number of values minus 1 (n-1).
8. Assign the iterative value j as i+1.
9. Repeat the steps 10 - 11 until iterative value (j) less than no. of values (n).
10. If a[i]>a[j] then
Swap the values using temporary variable.
11. Increment the iterative value (j)
12. Increment the iterative value (i)
13. Display the array elements in the ascending and descending order.
Flow chart
Start
Sta
Read the number of values
Next i
a[i]>a[
j]
Next j
Next i
Stop
Program
#include<stdio.h>
#include<conio.h>
void main( )
{
int i,j,n,a[20],temp;
clrscr( );
printf(Enter the size of the array \n);
scanf(%d,&n);
printf(Enter the elements of the array:\n);
for(i=0;i<n;i++)
{
scanf(%d,&a[i]);
}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf(The elements sorted in the Ascending order:\n);
for(i=0;i<n;i++)
{
printf(%d\t,a[i]);
}
printf(The elements sorted in the Descending order:\n);
for(i=n-1;i>=0;i--)
{
printf(%d\t,a[i]);
}
getch( );
}
Sample Input and Output:
INPUT:
Enter the size of the array
5
Enter the elements of the array:
20
44
3
56
12
OUTPUT:
The elements sorted in the Ascending oder
3
12
20
44
56
The elements sorted in the Descending oder
56
44
20
12
3
Result
Thus the C program to sort the numbers in an array in Ascending and descending order
has been done and the output is verified.
EX.NO.:16
Aim
To write a C program to sort the numbers using bubble sorting method.
Algorithm
1. Read the number of values (n).
2. Initialize the iterative value (i) to 0.
3. Repeat the steps 4 to 5 until the number of values minus 1 (n-1).
4. Read the elements in an array a[i].
5. Increment the iterative value.
6. Initialize the iterative value (i) to 0.
7. Repeat the steps 8 to 12 until the number of values minus 2 (n-2).
8. Assign the iterative value j as 0.
9. Repeat the steps 10 - 11 until iterative value (j) less than (n-2)-i.
10. If a[j]>a[j+1] then
Swap the values using temporary variable.
11. Increment the iterative value (j)
12. Increment the iterative value (i)
13. Display the array elements in the sorted order.
Flow chart
Start
Sta
Read the number of values
Next i
a[j]>a[j+
1]
Next j
Next i
Next
i
Stop
Program
#include<stdio.h>
#include<conio.h>
void main( )
{
int i,j,n,a[20],temp;
clrscr( );
printf(Sorting the elements using bubble sort\n);
printf(Enter the size of the array \n);
scanf(%d,&n);
printf(Enter the elements of the array:\n);
for(i=0;i<=n-1;i++)
{
scanf(%d,&a[i]);
}
for(i=0;i<=n-2;i++)
{
for(j=0;j<=n-2-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf(The elements sorted using bubble sort are:\n);
for(i=0;i<n-1;i++)
{
printf(%d\t,a[i]);
}
getch( );
}
Result
Thus the C program to sort the numbers in an array using bubble sort has been
done and the output is verified.
EX.NO.:17
Aim
To Display all the elements of an array in reverse order using functions
Description
Array
The elements are stored in a memory locations in sequential order is called Arrays.
For Ex: It is possible to declare an array for the set of salaries of group of employees.
i.e.,
int salary[10];
where,
Lower Bound: The smallest element of an array index is referred as lower bound,
which is fixed at 0 the location of an array.
Upper Bound: The highest element of an array index is referred as upper bound,
which is depends on the program execution.
Range=Upperbound-lowerbound+1
Algorithm
1.
2.
3.
4.
5.
6.
Flowchart
Start
reverse
Stop
reverse
scanf("%d",&a[i]);
}
printf("Reversed elements are:\n");
for(i=n-1;i>=0;i--)
{
printf("%d\n",a[i]);
}
getch();
}
Sample Input and Output:
INPUT:
Enter the total number of elements
5
Enter the Elements:
12
34
56
78
90
OUTPUT:
Reversed elements are:
90
78
56
34
12
Result
Thus all the elements of an array are displayed in reverse order.
EX.NO.:18
Aim
Implement Matrix addition and Multiplication using an array
Algorithm
Matrix Addition:
1. Read the total number of rows & columns for the matrix
2. Read the elements for A Matrix and store it into two dimensional arrays.
3. Read the elements for B Matrix and store it into other two dimensional array.
4. Add the elements of 2 matrixes namely A & B through the nested for Loops and
store the result into a resultant 2-dimesional array C.
5. Display the result
Matrix Multiplication:
1. Read the total number of rows & columns for the matrix
2. Read the elements for A Matrix and store it into two dimensional arrays.
3. Read the elements for B Matrix and store it into other two dimensional array.
4. Multiply the elements of 2 matrixes namely A & B through the nested for Loops
and store the result into a resultant 2-dimesional array C.
5. Display the result
Flowchart
Matrix Addition
Start
Stop
Matrix Multiplication
Start
Stop
Program
1. Matrix Addition
#include<stdio.h>
#include<conio.h>
int a[25][25],b[25][25],c[25][25],m,n,i,j;
void main()
{
clrscr();
printf("Enter the rows and columns of two matrixes\n");
scanf("%d %d",&m,&n);
printf("Enter the elements of A matrix\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter the elements of B matrix\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("Elements of A matrix are:\n");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
{
printf("\t%d",a[i][j]);
}
}
printf("\nElements of B matrix are:\n");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
{
printf("\t%d",b[i][j]);
}
}
2. Matrix Multiplication
#include<stdio.h>
#include<conio.h>
int a[25][25],b[25][25],c[25][25],m,n,i,j,k;
void main()
{
clrscr();
printf("Enter the rows and columns of two matrixes\n");
scanf("%d %d",&m,&n);
printf("Enter the elements of A matrix\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter the elements of B matrix\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("Elements of A matrix are:\n");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
{
printf("\t%d",a[i][j]);
}
}
printf("\nElements of B matrix are:\n");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
{
printf("\t%d",b[i][j]);
}
}
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
{
c[i][j]=0;
for(k=0;k<m;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
printf("\nThe Multiplication of 2 matrixes are:\n");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
{
printf("\t%d",c[i][j]);
}
}
getch();
}
Sample Input and Output:
Matrix Addition
INPUT:
Enter the rows and columns of two matrixes
3
3
Enter the elements of A matrix
1
2
3
4
5
6
7
8
9
Enter the elements of B matrix
1
2
3
4
5
6
7
8
9
OUTPUT:
Elements of A matrix are:
1
4
7
2
5
8
3
6
9
4
10
16
6
12
18
Matrix Multiplication
INPUT:
Enter the rows and columns of two matrixes
3
3
Enter the elements of A matrix
1
2
3
4
5
6
7
8
9
Enter the elements of B matrix
1
2
3
4
5
6
7
8
9
OUTPUT:
Elements of A matrix are:
1
2
3
4
5
6
7
8
9
Elements of B matrix are:
1
4
7
2
5
8
3
6
9
36
42
81
96
126 150
Result
Thus Matrix addition and Multiplication using an array was implemented
successfully.
STRING MANIPULATIONS
The C compiler provides the following string handling functions:
Function
Purpose
strlen()
strcpy()
strcat()
strcmp()
strlwr()
strupr()
strdup()
strrev()
Strncpy()
Strncmp()
Strcmpi()
strnicmp()
Stricmp()
strchr()
strrchr()
strstr()
strncat()
strnset()
strspn()
strpbrk()
EX.NO.:19
Aim
To write a C program to implement string functions without using library functions.
COMPUTING
Start STRING LENGTH
Algorithm:
1. Read the String (str). Read the String
2. Initialize a counter variable len as zero.
3. Increment the counter variable until the null character of string is reached.
Initialize length as zero
4. Print the length of the string (len).
Flow chart:
Repeat the process until
null character is reached
Stop
Program
#include<stdio.h>
#include<conio.h>
void main()
{
char str[50];
int len;
clrscr();
printf("Enter the string:");
scanf("%s",str);
for(len=0;str[len]!='\0';len++);
printf("Length of string is %d\n",len);
getch();
}
INPUT:
Enter the string: psnacollege
OUTPUT:
Length of string is 11
STRING COMPARISON
Algorithm
If the
lengths are
equal
Y
Flow chart
If they
are
same
Increment the counter variable j
If j value is
equal to length
of the string
Y
Display Both
the Strings
are Equal
Stop
Display Both
the Strings are
Unequal
Program
#include<stdio.h>
#include<conio.h>
void main()
{
char str1[20],str2[20];
int i,j=0,len1,len2;
clrscr();
printf("Enter the two strings:");
scanf("%s%s",str1,str2);
for(len1=0;str1[len1]!='\0';len1++);
for(len2=0;str2[len2]!='\0';len2++);
if(len1==len2)
{
for(i=1;i<=len1;i++)
{
if(str1[i]==str2[i])
j+=1;
}
}
if(j==len2)
printf("Both strings are equal");
else
printf("Both strings are not equal");
getch();
}
INPUT:
Enter the two strings: apple apple
Both strings are equal
OUTPUT:
Enter the two strings: boy ball
Both strings are not equal.
COPYING THE STRING
Algorithm
1. Read the String (str1).
2. Initialize a counter variable (i) as zero.
3. Repeat the steps 4 and 5 until the null character of string is reached.
4. Copy each character from string1 to another string (str2).
5. Increment the counter variable.
Start
6. Add the null character to the end of the String (str2).
7. Display the copied String (str2).
Read the String (str1)
Flow chart
Initialize length as zero
Stop
Program
#include<stdio.h>
#include<conio.h>
void main()
{
char str1[20],str2[20];
int i,j;
clrscr();
printf("Enter the first string:");
scanf("%s",str1);
printf("\nThe first string is %s",str1);
for(i=0;str1[i]!='\0';i++)
{
str2[i]=str1[i];
}
str2[i]='\0';
printf("\nCopy the string without using strcpy() function");
printf("\nThe Copied String is %s",str2);
getch();
}
INPUT:
Enter the first String: India
OUTPUT:
The First string is India
Copy the string without using strcpy() function
The Copied String is India
STRING CONCATENATION
Algorithm
1. Read two Strings (str1,str2).
2. Copy each character from string1 to another string (str3) until the null character of
string1 is reached.
3. Continuously copy each character from string2 to string (str3) until the null
character of string2 is reached.
4. Add the null character to the end of the String (str3).
5. Display the concatenated String (str3).
Flow chart
Start
Stop
Program
#include<stdio.h>
#include<conio.h>
void main()
{
char str1[10],str2[10],str3[10];
int i,j;
clrscr();
printf("Enter the two string:");
scanf("%s%s",str1,str2);
for(i=0;str1[i]!='\0';i++)
{
str3[i]=str1[i];
}
for(j=0;str2[j]!='\0';j++)
{
str3[i]=str2[j];
i++;
}
str3[i]='\0';
printf("Concatenated String is.....%s\n",str3);
getch();
}
INPUT:
Enter the two string: Anna
University
OUTPUT:
Concatenated string is. AnnaUniversity
RESULT:
Thus the string manipulation functions are executed successfully without using
library functions.
EX.NO.:20
Aim
To write a C program to arrange the names in alphabetical order.
Algorithm
1 .Read the number of names (n) to be sorted.
2. Read the names in an array names[i].
3. Increment the iterative value.
4. Initialize the iterative value (i) to 0.
5. Repeat the steps 7 to 11 until the number of values minus 1 (n-1).
6. Assign the iterative value j as i+1.
7. Repeat the steps 9 - 10 until iterative value (j) less than no. of values (n).
8. Compare the Strings as
If names[i], names[j]>0 then
Swap the names using temporary variable by using the strcpy() function.
9. Increment the iterative value (j)
Start
Sta
Read the number of values (n)
Read the elements in an array
names[i]
If the value is
greater than 0
stop
Program
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char names[50],temp[20];
int n,i,j;
clrscr();
printf("\n How many names are there to sort?:");
scanf("%d",&n);
printf("\n Enter the %d names one by one\n",n);
for(i=0;i<n;i++)
scanf("%s",names[i]);
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
if(strcmp(names[i],names[j])>0)
{
strcpy(temp,names[i]);
strcpy(names[i],names[j]);
strcpy(names[j],temp);
}
printf("\n Names in alphabetical order are:");
for(i=0;i<n;i++)
printf("\n %s",names[i]);
getch();
}
Sample Input and Output:
INPUT:
How many names are there to sort?
5
Enter the 5 names one by one
India
Asia
Nepal
Srilanka
Bhutan
OUTPUT:
Names in alphabetical order are:
Aravind
Banu
Priya
Sakthi
Yamuna
Result
Thus the C program to arrange the given string in alphabetical order executed
successfully.
FUNCTIONS
A function is meant for performing a specified task. The specified task is repeated
each time the program calls the functions. Functions break large computing tasks in to
smaller ones. They work together to accomplish the goal f the whole program. Every
program must contain one function named main where the program always begins
execution.
The main function may call other functions which in turn, may call still other
functions. When a function is called, the program execution is transferred to the first
statement in the called function. After a function returns to the calling function, execution
continues with the evaluation of the expression in which the call was made. A value can
be returned when a function completes the specified task and that value can be used as an
operand in an expression.
Functions can communicate data between themselves in two ways. One through
global variables and other through an argument or parameter list.
Function definition
A function definition introduces a new function by declaring the type of value it
returns and its parameters, and specifying the statements that are executed when the
function is called. Consider the following example,
#include<stdio.h>
main()
{
printline();
printf(This illustrates the usage of functions\n);
printline();
}
/*the statement defines a function called printline, which prints a line of 40
characters. This function has been called from the main().*/
Printline()
{
int i;
for(i=0;i<=40;i++)
printf(-);
}
The function in which call is continued is known as the calling function and the
function named in the call is said to be the called function. In the above example main()
is the calling function and printline() is the called function. The function printline()
returns no value (void). When a function is required to return a value, the return type has
to be specified explicitly. The general form of C function is give below
function_name(argument list)
argument declaration;
{
Variable declaration;
Function statements;
}
Arguments
EX.MO.:21
Aim:
To write a program to perform sequential search using functions.
Algorithm:
1. Read the number of elements in an array(n).
2. Read the elements of an array using for loop.
3. Read the element to be searched in the array (k)
4. Call the function search() by passing the array, element to be searched, total
no of elements as arguments.
5. Compare each & every element of the array with k.
6. If it matches,
Print as element found
Else
Print as element not found.
Flowchart
Start
Stop
If
found
Y
Display the
element is found
Return
Program
#include<stdio.h>
#include<conio.h>
void search(int a[],int,int);
void main()
{
int a[100],i,j,n,k;
char c='y';
clrscr();
printf("\n Sequential Search\n");
printf("\n Enter total no of elements in the array");
scanf("%d",&n);
printf("\n Enter the elements one by one");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
while(c=='y' || c=='Y')
{
printf("\n Enter the element to be searched");
scanf("%d",&k);
search(a,k,n);
printf("\nDo you want to continue(y/n)?");
c=getch();
}
getch();
}
void search(int a[],int k,int n)
{
int i;
int f=1;
for(i=0;i<n;i++)
{
if(a[i]==k)
f=2;
}
if (f==2)
printf("\n Element Found");
else
printf("\n Element not found");
}
Result
Thus the program for the sequential and binary search is performed.
RECURSIVE FUNCTIONS
A recursive function is one which calls itself. This is another complicated idea
which you are unlikely to meet frequently. We shall provide some examples to illustrate
recursive functions.
Recursive functions are useful in evaluating certain types of mathematical
function. You may also encounter certain dynamic data structures such as linked lists or
binary trees. Recursion is a very useful way of creating and accessing these structures.
Here is a recursive version of the Fibonacci function. .
int fib(int num)
/* Fibonacci value of a number */
{
switch(num) {
case 0:
return(0);
break;
case 1:
return(1);
break;
default: /* Including recursive calls */
return(fib(num - 1) + fib(num - 2));
break;
}
}
Here is an another example to find power using recursive version.
double power(double val, unsigned pow)
{
if(pow == 0) /* pow(x, 0) returns 1 */
return(1.0);
else
return(power(val, pow - 1) * val);
}
Notice that each of these definitions incorporates a test. Where an input value
gives a trivial result, it is returned directly; otherwise the function calls itself, passing a
changed version of the input values. Care must be taken to define functions which will
not call themselves indefinitely, otherwise your program will never finish.
Don't be frightened by the apparent complexity of recursion. Recursive functions
are sometimes the simplest answer to a calculation. However there is always an
alternative non-recursive solution available too. This will normally involve the use of a
loop, and may lack the elegance of the recursive solution
EX.NO.:22
Aim:
To write a C program to calculate the factorial of the given number using
recursive functions.
Algorithm
1. Read the number (a)
2. Call the function rec() by passing the number
3. If the number is 0
Return the factorial value as 1.
4. Else
Compute the factorial value by recursively calling rec() function by
decrementing the number (x).
5. Return the factorial value (f).
6. Display the factorial value.
Flowchart
Start
Stop
recursive(a)
Y
If the
value is 0
Factorial value is 1
N
Compute factorial value as
F=f*rec(a-1)
Program:
#include<stdio.h>
#include<conio.h>
int recursive(int);
void main ()
{
int a,fact;
clrscr();
printf("Enter the number:");
scanf("%u",&a);
fact=recursive(a);
printf("The factorial of %u is %u",a,fact);
getch();
}
int recursive(x)
int x;
{
int f;
if(x==0)
return(1);
else
f=x*recursive(x-1);
return(f);
}
Sample Input and Output:
INPUT:
Enter the number 5
OUTPUT:
The factorial of 5 is 120
Result
Thus the program for the factorial using recursive function is performed.
STRUCTURES IN C
A structure is a collection of variables under a single name. These variables can be
of different types, and each has a name which is used to select it from the structure. A
structure is a convenient way of grouping several pieces of related information together.
Accessing Members of a Structure
Each member of a structure can be used just like a normal variable, but its name
will be a bit longer.
st_rec.name
EX.NO.:23
Aim
To write a program in C to print the mark sheet of n students using structure.
Algorithm
1. Initialize the structure student with its members like name, number, marks, total,
average and grade.
2. Read the number of students (n).
3. Read the student number, name and six subject marks for n students.
4. Find the total marks and average for each student.
5. Award the grade for each student by checking the marks of all the subjects.
6. Print the student mark list displaying the student number, name, total marks,
average and grade for each student.
Flowchart
Start
Initialize the structure student with its
members like name, number, marks,
total, average and grade
If at least one of
the marks is less
than 35
Grade = Fail
N
Y
If
avg>=75
Grade = Distinction
N
Y
If avg <75
& >=60
N
Y
If avg <60
& >=50
N
B Stop
Program
#include<string.h>
#include<stdio.h>
#include<conio.h>
struct student
{
int sno,m1,m2,m3,m4,m5,m6,tot;
char name[10],grade[50];
float avg;
}s[10];
void main()
{
float avg;
int tot,n,i;
clrscr();
printf("Enter the number of students to process..");
scanf("%d",&n);
printf("Enter Student number, name and 6 subject marks ...\n");
for(i=1;i<=n;i++)
{
scanf("%d%s%d%d%d%d%d
%d",&s[i].sno,&s[i].name,&s[i].m1,&s[i].m2,&s[i].m3,&s[i].m4,&s[i].m5
,&s[i].m6);
s[i].tot=s[i].m1+s[i].m2+s[i].m3+s[i].m4+s[i].m5+s[i].m6;
s[i].avg=s[i].tot/6;
if(s[i].m1<35||s[i].m2<35||s[i].m3<35||s[i].m4<35||s[i].m5<35||s[i].m6<35)
strcpy(s[i].grade,"Fail");
else
{
if(s[i].avg>=75)
strcpy(s[i].grade,"Distinction");
else if(s[i].avg<75 && s[i].avg>=60)
strcpy(s[i].grade,"First class");
else if(s[i].avg<60 && s[i].avg>=50)
strcpy(s[i].grade,"Second class");
}
}
printf("Student Mark List \n");
printf("S.No\tSname\tTotal\tAverage\tGrade\n");
printf("-------------------------------------------------\n");
for(i=1;i<=n;i++)
{
printf("%d\t%s\t%d\t%f\t%s",s[i].sno,s[i].name,s[i].tot,s[i].avg,s[i].grade);
printf("\n");
}
getch();
}
Result
Thus the program to print the mark sheet of n students using structure is written
and executed.
EX.NO.:24
Aim
To write a C program to find the length of the string using pointers.
Algorithm
1. Read the String(ptr)
2. Initialize the variable (len) as 0.
3. The ptr value refers to the address of the first character of the string and the
*ptr refers to the actual string.
4. Till the string(*ptr) exist
a) Increment the length
b) Increment the pointer variable(ptr)
5. Display the length of the string
Flowchart
Start
Till the
String
exists
(*ptr)
Stop
Program
#include<stdio.h>
#include<conio.h>
void main()
{
char *ptr;
int len=0;
clrscr();
printf("Enter the String : ");
scanf("%s",ptr);
while(*ptr)
{
++len;
++ptr;
}
printf("Length of the string : %d",len);
getch();
}
Result
Thus the program for finding the string length using pointers is executed.
EX.NO.:25
Aim
To write a C program to print the elements of an array using pointers.
Algorithm
1.
2.
3.
4.
5.
6.
7.
Flowchart
Start
Stop
Program
#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],*ptr,i,n;
clrscr();
printf("\n Enter total number of elements:");
scanf("%d",&n);
printf("\n Enter elements");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
ptr=a;
printf("\nArray elements\n");
for(i=1;i<=n;i++)
printf("%d\n",*(ptr+i));
getch();
}
Thus the program for printing the elements of an array using pointers is
written and executed.
EX.NO.:26
STUDENT MARK STATEMENT USING FILE CONCEPT
Aim
To write a C program to prepare student mark statement using file concept.
Algorithm
1. Initialize the structure student with its members like name, number, marks, total,
average and grade.
2. Read the number of students (n).
3. Create a new file in write mode to store the student mark statement details.
4. Read the student number, name and marks for n students.
5. Find the total marks and average for each student.
6. Print the student mark statement displaying the student number, name, total marks
and average for each student in the output file.
Flowchart
Start
Initialize the structure student with its
members like name, number, marks,
total and average
Stop
Program
#include<stdio.h>
#include<conio.h>
struct student
{
char name[20];
int rn;
int m1,m2,m3,total,avg;
}s[10];
void main()
{
FILE *fp;
int n,i;
clrscr();
fp=fopen("students.c","w");
printf("\n Enter the no. of students:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Enter the name:");
scanf("%s",s[i].name);
printf("\n Enter the no.:");
scanf("%d",&s[i].rn);
printf("\n Enter the Mark m1:");
scanf("%d",&s[i].m1);
printf("\n Enter the Mark m2:");
scanf("%d",&s[i].m2);
printf("\n Enter the Mark m3:");
scanf("%d",&s[i].m3);
fprintf(stdout,"\n Name =%s",s[i].name);
fprintf(stdout,"\n Number =%d",s[i].rn);
fprintf(stdout,"\n Mark1 =%d",s[i].m1);
fprintf(stdout,"\n Mark2 =%d",s[i].m2);
fprintf(stdout,"\n Mark3 =%d",s[i].m3);
s[i].total=s[i].m1+s[i].m2+s[i].m3;
fprintf(stdout,"\n Total =%d",s[i].total);
s[i].avg=s[i].total/3;
fprintf(stdout,"\n Average =%d",s[i].avg);
fprintf(fp,"\n Name =%s",s[i].name);
fprintf(fp,"\n Number =%d",s[i].rn);
fprintf(fp,"\n Mark1 =%d",s[i].m1);
fprintf(fp,"\n Mark2 =%d",s[i].m2);
fprintf(fp,"\n Mark3 =%d",s[i].m3);
fprintf(fp,"\n Total =%d",s[i].total);
fprintf(fp,"\n Average =%d",s[i].avg);
}
fclose(fp);
getch();
}
Sample Input and Output
INPUT:
Enter the no. of students: 2
Enter the name:keerthi
Enter the no.:12
Enter the Mark m1:89
Enter the Mark m2:65
Enter the Mark m3:54
Name =keerthi
Number =12
Mark1 =89
Mark2 =65
Mark3 =54
Total =208
Average =69
Enter the name:kamala
Enter the no.:45
Enter the Mark m1:56
Enter the Mark m2:34
Enter the Mark m3:98
Name =kamala
Number =45
Mark1 =56
Mark2 =34
Mark3 =98
Total =188
Average =62
OUTPUT:
Students.c:
Name =keerthi
Number =12
Mark1 =89
Mark2 =65
Mark3 =54
Total =208
Average =69
Name =kamala
Number =45
Mark1 =56
Mark2 =34
Mark3 =98
Total =188
Average =62
Result
Thus the program for student mark statement using file concept is written
and executed.
EX.NO.:27
Aim
To write a C program to merge two files using command line arguments.
Algorithm
1. Read the input as 3 file names in the command line arguments.
2. Open the first two files in read mode & third file in write mode.
3. Get the characters one by one from the first file and put it in the third file using
file pointer until EOF (End of File) has been reached
4. Get the characters one by one from the second file and appended it with the
third file until EOF has been reached.
5. Display the content of the Output (third) file.
Flowchart
Start
Till not
EOF of
file1
Y
Till not
EOF of
file2
Y
Stop
Program
#include <stdio.h>
#include<conio.h>
void main(int argc,char *argv[])
{
FILE *fp1, *fp2,*fp3;
int c,c1 ;
if(argc==4)
{
fp1 = fopen( argv[1], "r" );
fp2 = fopen( argv[2],"r" ) ;
fp3=fopen(argv[3],"w+");
}
else
{
printf("error");
}
c= getc(fp1) ;
c1=getc(fp2);
while (c != EOF)
{
putc(c,fp3);
c= getc( fp1) ;
}
while(c1!=EOF)
{
putc(c1,fp3);
c1=getc(fp2);
}
fclose ( fp1 );
fclose ( fp2 );
fclose(fp3);
printf("Files successfully merged \n");
getch();
}
Result
Thus the program to merge two files using command line arguments is executed
successfully.
Extra Experiments (University Questions)
1. Find Area of the Circle
2. Convert Centigrade to Fahrenheit
3. Find Biggest among three numbers
4. Check the given year is leap year or not
5. Find the sum of row and column of the given matrix
6. Find the sum of digits of the given number
7. Check the given number is Armstrong number or not
8. Print the given number in Reverse order
9. Check the given string is palindrome or not
10. Find the Factorial value of given number using Recursive function
11. Sort the given number in Ascending order
12. Find the transpose of the given matrix
13. Generate the following triangle
1
123
12345
1234567
14. Generate the following triangle
5
5 5
5 5 5
5 5 5 5
15. Find the roots of given quadratic equation
16. Find the sum of the given series
1 - x2/2! x4/4! - . xn/n!
17. Find the largest element in row and column of the given matrix
18. Find the sum of ODD and EVEN position numbers of the given array
19. Find NCr
20. Print student total marks and grade using UNION
21. Swap two numbers without using temporary
22. Check the given number is ODD or EVEN
rs=rs+m[i][j];
printf("\nSum of Row %d =
%d",i+1,rs);
rs=0;
}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
cs=cs+m[j][i];
printf("\nSum of Column %d =
%d",j+1,cs);
cs=0;
}
getch();
}
6./* sum of digits */
#include<stdio.h>
#include<conio.h>
void main()
{
int n,r,s=0;
clrscr();
printf("Enter no :");
scanf("%d" ,&n);
while(n>0)
{
r=n%10;
s=s+r;
n=n/10;
}
printf("\nSum of digits = %d",s);
getch();
}
7. /* Armstrong number */
#include<stdio.h>
#include<conio.h>
void main()
{
int no,n,r,s=0;
clrscr();
printf("Enter no :");
scanf("%d" ,&n);
no=n;
while(n>0)
{
r=n%10;
s=s+r*r*r;
n=n/10;
}
if(no==s)
printf("\n Armstrong Number");
else
printf("\n Not an Armstrong Number");
getch();
}
8./* Reverse the digits */
#include<stdio.h>
#include<conio.h>
void main()
{
int i=0,j,n,r,a[10];
clrscr();
printf("Enter no :");
scanf("%d" ,&n);
while(n>0)
{
r=n%10;
a[i]=r;
i++;
n=n/10;
}
for(j=0;j<i;j++)
printf("%d",a[j]);
getch();
}
9./* String is palindrome or not*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char name[10];
int i=0,j,len;
clrscr();
printf("Enter Name :");
scanf("%s" ,name);
len=strlen(name);
for(i=0,j=len-1;i<=j;i++,j--)
if(name[i]!=name[j])
{
printf("Not Palindrome");
getch();
exit(0);
}
printf("Palindrome");
getch();
}
10./* Factorial using recursion */
#include<stdio.h>
#include<conio.h>
int fact(int);
void main()
{
int n,f;
clrscr();
printf("Enter the number:");
scanf("%d",&n);
f=fact(n);
printf("\n The factorial value : %d",f);
getch();
}
int fact(int x)
{
int fac;
if(x==0)
return 1;
else
fac=x*fact(x-1);
return fac;
}
11./* sorting of array */
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,temp,n,a[10];
clrscr();
printf("Enter number of element:");
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(a[i]<a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
printf("\nThe sorted Array");
for(i=0;i<n;i++)
printf("\n%d",a[i]);
getch();
}
void main()
{
int i,k,j;
clrscr();
for(i=1;i<=7;i=i+2)
{
printf("\n");
for(k=7-i-1;k>0;k--)
printf(" ");
for(j=1;j<=i;j++)
printf(" %d",j);
}
getch();
}
clrscr();
printf("Enter values for a,b,c ");
scanf("%d %d %d",&a,&b,&c);
r1=(-b+sqrt(b*b-4*a*c))/2*a;
r2=(-b-sqrt(b*b-4*a*c))/2*a;
printf("\nRoot 1 : %f",r1);
printf("\nRoot 2 : %f",r2);
getch();
}
int fact(int);
void main()
{
int i,j=0,x,n;
float s=0.0;
clrscr();
printf("Enter values for x and n");
scanf("%d %d",&x,&n);
for(i=2;i<=n;i=i+2)
{
if(j==0)
{s=s-(pow(x,i)/fact(i));
j=1;
}
else
{s=s+(pow(x,i)/fact(i));
j=0;
}
}
printf("\nSum of the series : %f",1-s);
getch();
}
int fact(int x)
{
int fac;
if(x==0)
return 1;
else
fac=x*fact(x-1);
return fac;
}
17./* Largest element in ROW and
COLUMN */
#include<stdio.h>
#include<conio.h>
void main()
{
int r,c,i,j,max,m[10][10];
clrscr();
printf("Enter no of rows and columns :
");
scanf("%d %d",&r,&c);
for(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf("%d",&m[i][j]);
for(i=0;i<r;i++)
{
max=0;
for(j=0;j<c;j++)
if(m[i][j]>max)
max=m[i][j];
printf("\nRow %d maximum :
%d",i+1,max);
}
for(i=0;i<r;i++)
{
max=0;
for(j=0;j<c;j++)
if(m[j][i]>max)
max=m[j][i];
printf("\nColumn %d maximum :
%d",i+1,max);
}
getch();
}
18./* Sum of ODD and EVEN Position
Numbers */
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,es=0,os=0,a[10];
clrscr();
{
int regno;
int mark[8];
};
void main()
{
int i,j,tot=0;
float avg;
union student st[2];
clrscr();
printf("Enter regno and 8 marks of 2
students");
for(i=0;i<2;i++)
{
printf("\n Enter the register no ");
scanf("%d",&st[i].regno);
printf("\n Enter 8 marks");
for(j=0;j<8;j++)
{
scanf("%d",&st[i].mark[j]);
tot=tot+st[i].mark[j];
}
printf("\nTotal marks : %d",tot);
avg=tot/8;
printf("\nAverage : %f",avg);
if(tot/8 > 59)
printf("\nGrade : First class");
else
printf("\nGrade : Second class");
tot=0;
getch();
}
}
21./* Swap TWO values without
temporary*/
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("Enter two values a b : ");
scanf("%d %d",&a,&b);
a=a+b;
b=a-b;
a=a-b;
printf("Swaped values : a= %d b=
%d",a,b);
getch();
}
22./* ODD or EVEN */
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
printf("Enter The No:");
scanf("%d",&a);
if(a%2==0)
printf("\n Even Number ");
else
printf("Odd Number ");
getch();
}
clrscr();
printf("Enter The Nos a,b:");
scanf("%d %d",&a,&b);
c=add(a,b);
printf("\n Sum of nos : %d ",c);
getch();
}
int add(int x,int y)
{
return x+y;
}