Module-3 POP Study Material
Module-3 POP Study Material
if (condition)
if (condition)
if (condition)
else
printf("dangling else!\n");
In such situations, else clause belongs to the closest if statement which is incomplete that is the
innermost if statement!
we can make else clause belong to desired if statement by enclosing all if statements in block outer to
which if statement to associate the else clause.
Example:
if (condition) {
if (condition)
if (condition)
} else
printf("else associates with the outermost if statement!\n");
5. Develop a C program to count the number of odd and even numbers using for loop.
#include<stdio.h>
int main()
{
int i,n,even=0,odd=0;
printf("\nEnter the Ending value:");
scanf("%d",&n);
printf("\nEven numbers:");
for(i=0;i<=n;i++)
{
if(i%2==0)
{
printf("\n%d",i);
even++;
}
}
printf("\nOdd numbers:");
for(i=1;i<=n;i++)
{
if(i%2==1)
{
printf("\n%d",i);
odd++;
}
}
printf("\nTotal even numbers:%d",even);
printf("\nTotal odd numbers:%d",odd);
return 0;
}
6. Develop a C program to find factorial of given number with and without recursion
if (n < 0)
printf("Error! Please enter any positive integer number");
else
{
for(i=1; i<=n; ++i)
{
factorial *= i;
}
printf("Factorial of Number %d = %d", n, factorial);
}
getch();
}
ARRAYS:
An array is defined as the collection of similar type of data items stored at contiguous memory
locations.
Arrays are used to store multiple values in a single variable, instead of declaring separate
variables for each value.
Arrays are the derived data type in C programming language which can store the primitive type
of data such as int, char, double, float, etc.
It also has the capability to store the collection of derived data types, such as pointers, structure,
etc. The array is the simplest data structure where each data element can be randomly accessed
by using its index number.
C array is beneficial if you have to store similar elements. For example, if we want to store the
marks of a student in 6 subjects, then we don't need to define different variables for the marks
in the different subject. Instead of that, we can define an array which can store the marks in
each subject at the contiguous memory locations.
By using the array, we can access the elements easily. Only a few lines of code are required to
access the elements of the array.
Properties of Array:
The array contains the following properties.
• Each element of an array is of same data type and carries the same size, i.e., int = 4
bytes.
• Elements of the array are stored at contiguous memory locations where the first element
is stored at the smallest memory location.
• Elements of the array can be randomly accessed since we can calculate the address of
each element of the array with the given base address and the size of the data element.
Advantage of C Array
1) Code Optimization: Less code to the access the data.
2) Ease of traversing: By using the for loop, we can retrieve the elements of an array easily.
3) Ease of sorting: To sort the elements of the array, we need a few lines of code only.
4) Random Access: We can access any element randomly using the array.
Disadvantage of C Array
1) Fixed Size: Whatever size, we define at the time of declaration of the array, we can't
exceed the limit. So, it doesn't grow the size dynamically like LinkedList which we will
learn later.
Declaration of C Array
We can declare an array in the C language in the following way.
data_type array_name[array_size];
Example: int marks[5];
Initialization of C Array:
The simplest way to initialize an array is by using the index of each element.
We can initialize each element of the array by using the index.
Consider the following example.
marks[0]=80;//initialization of array
marks[1]=60;
marks[2]=70;
marks[3]=85;
marks[4]=75;
#include<stdio.h>
int main(){
int i=0;
int marks[5];//declaration of array
marks[0]=80;//initialization of array
marks[1]=60;
marks[2]=70;
marks[3]=85;
marks[4]=75;
//traversal of array
for(i=0;i<5;i++){
printf("%d \n",marks[i]);
}//end of for loop
return 0;
}
printf("%d", myNumbers[0]);
// Now outputs 33 instead of 25
int marks[5]={20,30,40,50,60};
In such case, there is no requirement to define the size. So it may also be written as
the following code.
int marks[ ]={20,30,40,50,60};
Let's see the C program to declare and initialize the array in C.
#include<stdio.h>
int main(){
int i=0;
int marks[5]={20,30,40,50,60};//declaration and initialization of array
//traversal of array
for(i=0;i<5;i++){
printf("%d \n",marks[i]);
}
return 0;
}
Multidimensional Arrays:
If we want to store data as a tabular form, like a table with rows and columns, we need
to get familiar with multidimensional arrays.
Arrays can have any number of dimensions. Here, we will introduce two-dimensional
arrays (2D).
Two-Dimensional Arrays:
This statement accesses the value of the element in the first row (0) and third column
(2) of the matrix array.
Example
SCOPE OF VARIABLES