c&c++ 2nd 3rd
c&c++ 2nd 3rd
Com
Unit-2
Loops and Arrays
#include<stdio.
h>
#include<conio
.h> void main()
{
int
x=1;
clrscr();
Programming with C & C++ B.Com
while(x<6)
{
printf(“\n B.Com 2nd
Year”); x++;
}
getch();
}
Output:
B.Com 2nd
Year B.Com
2 nd
Year
B.Com 2nd
Year B.Com
2 nd
Year
B.Com 2nd
Year
2. Do-while loop/Exit controlled loop
do while is same as while loop.
It is also looping statement.
Do-while loop is used to execute the statements repeatedly until the condition is false.
First It is used to execute the statements then check the condition its true or not.
Hence it is called Exit controlled Loop.
Syntax:
do
{
Statement block;
Increment/decremen
t;
}
While (condition);
Example:
#include<stdio.
h>
#include<conio
.h> void main()
{
int i=1;
2
Programming with C & C++ B.Com
clrscr();
d
o
{
printf(“\n This is a program of do while
loop.”); i++;
while(i<=5);
getch()
;
}
Output:
This is a program of do while
loop. This is a program of do
while loop. This is a program of
do while loop. This is a
program of do while loop. This
is a program of do while loop.
3. for loop/Top Checking loop/Entry Controlled Loop
The for loop is the looping statements.
The for loop is also called Top-Checking loop or Entry controlled loop.
It is used to execute the set of statements when the condition is true.
The for-loop statement contains 3 expressions or conditions separated by semicolons.
There is initialization, condition, increment or decrement at only one statement.
Syntax of for loop statement
for(initialization; condition; increment/decrement/update)
{
Statement block;
}
Output:
#include<stdio.
h>
#include<conio
.h> void main()
{
3
int i; Programming with C & C++ B.Com
clrscr();
for(i=1;i<=15;i=i++)
{
printf(“%5d”,i);
}
getch();
}
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Example:
#include
4
<stdio.h>
#include<conio.
Programming with C & C++ B.Com
h> void main ()
{
int a =
10;
clrscr();
while( a < 20 )
{
printf("value of a: %d\n",
a); a++;
if( a==15)
{
break;
}
}
}
Continue Statement:
Continue statement is unconditional statement or jumping statement.
It is the keyword.
No header file is not required to use continue statement.
The continue statement is exactly opposite of the break statement.
The continue statement is used for continuing the next iteration of the loop
statements like for loop, while loop and do-while loop using if statement.
Syntax: while (condition)
{
Statements;
Increment/decremen
t; if(condition)
{
continue;
}
}
Example:
#include
<stdio.h>
#include<conio.
5
h> void main ()
{
Programming with C & C++ B.Com
int a = 10; clrscr();
while( a < 20 )
{
printf("value of a: %d\n",
a); a++;
if( a==15)
{
continue;
}
}
}
goto statement:
The goto statement is unconditional statement or jumping statement.
The goto is the Keyword.
No header file is not required to use goto statement.
The goto statement is used to jump from one line to another line in the program.
Syntax:
goto label;
Example:
#include<stdio.
h>
#include<conio
.h> void main()
{
clrscr();
printf(“’hellow world\n”);
goto label1;
printf(“’how are
you?”); printf(“are
you ok”); label1:
Type
:
Types refer to data type.
It decides which type of element is stored in the array.
Bas
e:
The address of the first element (0th) is a base address.
Index/
subscript:
Index or subscript is used to access the items in array.
An index starts with 0 and ends with n-1.
Word
:
It indicates the space required for an element.
7
Declaration of a one-dimensional array can be done with data type first.
Programming with C & C++ B.Com
Then followed by the variable name.
Lastly the array size is enclosed in square brackets.
Syntax:
Datatype Arrayname [size];
Example:
int a[2][3] = { { 4, 5, 9 }, {6, 7, 8 }};
4 5 9
6 7 8
Three-dimensional Arrays: OR Multi Dimensional arrays
An array with three subscripts is termed as three-dimensional array.
A three-dimensional array is a collection of one or more two-dimensional
arrays, all of which share a common name.
Two-dimensional array is used to store a table of values. So, a three-
dimensional array is used to form a group of tables and perform collective
manipulations.
Declaration of three-dimensional arrays:
Syntax of declaration of a 3-d array is as follows:
data-type variable-name[size1][size2][size3];
size1 indicates the no. of tables being grouped together.
size2 indicates the no. of rows of each table.
size3 indicates the no. of columns of each table.
Example:
int a[2][4][5];
9
Programming with C & C++ B.Com
Unit -3 .
Chapter 1: Strings
1. What is meant by String? How can you Read and write A string in C? Or
Syntax:
Example:
char name[12];
Initialization of string:
The initialization of strings means creating memory and assigning values for
10
string variables.
Programming with C & C++ B.Com
Syntax:
Data type variable name[size]={characters];
string. #include<stdio.h>
#include<conio.h
clrscr(); printf("%s",str);
Output: Computer
2. gets() function
1. scanf() function:
It is the input statement.
This function is used to receive the characters or strings and store them in
variables.
Syntax:
scanf(“control string”, variable name);
2. printf() function:
The printf() function is output statement.
This function is used to display the values of the variables.
The printf() displays the entered character or string.
Syntax:
printf(“control string”, variable name);
Example:
#include<stdio.h
char name[5];
12
scanf(“%s”,name);
Programming with C & C++ B.Com
gets() function:
The gets function stands for get string.
This function is used to read keystrokes and assigns to the variable.
Syntax:
gets(variable name);
puts function:
The puts function stands for put string.
This function is used to display/print the string or character array.
Syntax:
puts(variable name);
Example:
#include<stdio.h
>
#include<conio.h
>
void main()
char c[30];
clrscr();
gets(c);
puts(c);
13
Programming with C & C++ B.Com
getche();
Example:
#include<stdio.h
char ch;
ch=getchar();
putchar(ch);
Functions Description
strlen() Length Function
14
strcpy()
strncpy()
Programming
Copy Function
with C & C++ B.Com
Copy of „n‟ character Function
strcat() Concatenate Function
strcmp() Compare Function
strchr() Search character Function
strlwr() Lower Case Function
strupr() Upper Case Function
strrev() Reverse Function
character in a given string. It return Null if the desired character is not found.
Syntax: - strlwr(“string”);
Example: - strlwr(“TELUGU”); return: telugu
Syntax: - strupr(“string”);
Example: - strlwr(“english”); return: ENGLISH.
9. strrev(): - Reveres Function: This function can convert into right to left
characters.
Syntax: - strrev(“string”);
Example: - strrev(“MOBILE”); return: ELIBOM
4.What is meant by Function? Explain about Function Prototype /Function
Declaration?
(or) Explain about different types of functions in C? (or)
Define function? Explain the implementation of the functions in C?
A function is a self-contained block or a sub-program of one or more statements that perform a
special task when called.
A function is identified by function name, parentheses, opening braces and closing parentheses.
main()
{
func1( )
.......
{
.......
}
fun1()
........
return 0;
}
In the above figure illustrate there are two functions are there main function and func1 is
another function.
16
Programming with C & C++ B.Com
Types of functions:
The C language supports two types of functions
(1) Library functions or Built-In functions or System Defined Functions
Library functions are also called Built-in functions or system defined functions.
These functions can be developed by the special programmers
The library functions are pre-defined set of functions.
Their task is limited.
One can only use the functions but cannot change or modify them.
Ex:
main(), clrscr(), getch() put()and getche()
(2) User-defined functions
The User-defined functions can be developed by the user.
The user-defined functions are not pre-defined functions.
A user can modify the functions according to the requirement.
Function terminology
Called function and calling function:
main() function calls the function name func1(). Therefore main() is called as calling
function and func1 is known as called function.
Argument or parameter types:
The function takes input from main function is called argument or parameter.
Arguments can be divided into 2 types like actual parameter and formal parameters.
Actual parameters refers to the arguments of calling functions are called actual
arguments.
Formal parameter refers to the arguments of called functions are called formal
parameters.
return statement:
return statement is used to return the values to the calling function.
Function declaration/ Function Prototype
Function declaration means creating of memory for that function.
It is terminated by semicolon;
Syntax:
return type data type function name(data type variable name1…….);
Function definition:
Function definition means body of the function. It contains executable code.
Syntax:
17
Programming with C & C++ B.Com
Ex:
Write a program to show how user-defined function is called.
#include<stdio.h>
int add(int a, int b) /*function definition*/
{
return(a+b);
}
void main()
{
int x=1,y=2,z;
z=add(x,y); /* FUNCTION call/
printf(“z=%d”,z);
}
Output:
Z=3
Call by reference:
Arguments are passed by address is called call by reference or pass by reference.
Called function can work on address of the arguments than values.
In call by reference the return statement is not required.
18
Syntax:
Programming with C & C++ B.Com
return type function name(*argument1, *argument2)
{
Statements;
}
5. Define Recursive function? Explain with illustration
A function is calls itself to solve the problem is called recursive function. The best
example of recursive function is finding the factorial number.
Example:
n ! = n x ( n - 1)
n!= n x ( n - 1)
5!= 5 x (5-1) 2!= 2x1
4!= 4x3
3!= 3 x (3-1)
3!= 3x2
2!= 2 x (2-1)
Direct
A function calls itself; this type of recursion is direct recursion. Only one function
is involved in direct recursion.
19
Programming with C & C++ B.Com
Ex:
int num()
___
num(
);
Indirect
Ex:
int num()
___
sum()
;
}
sum(
)
___
num(
}
);
20
Write a C program to compute factorial number using recursive function
#include <stdio.h>
void main ()
int num;
printf("\n %d , num,);
if(n=
=l)
return
1;
return (n * Fact(n-l));