# IGNOU Mcs-011 June

Question 1:

a. Write an algorithm and draw a corresponding flow chart to check whether the given year is a leap year or not.

``````Step 1: Start
Step 2: Input the year
Step 3: Check if the year is evenly divisible by 4, if it is, go to
step 4. If not, go to step 7.
Step 4: Check if the year is evenly divisible by 100, if it is, go to
step 5. If not, go to step 6.
Step 5: Check if the year is evenly divisible by 400, if it is, go to
step 6. If not, go to step 7.
Step 6: Print "Leap year" and go to step 8
Step 7: Print "Not a leap year" and go to step 8.
Step 7: Stop``````

Program

b. Write a C function isodd(num) that accepts an integer argument and returns 1 if the argument is odd and 0 otherwise.

``````#include <stdio.h>

int isodd(int num) //function return 1 if no is odd otherwise 0
{
if (num % 2 != 0)
{
return 1;
}
else
{
return 0;
}
}

int main()
{
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (isodd(num)) // function check wether return value is 1 or 0
{
printf("The number %d is odd.\n", num);
}
else
{
printf("The number %d is not odd.\n", num);
}
return 0;
}
``````

Output:

``````Enter a number: 57
The number 57 is odd.``````

c. Write a C program that invokes this function to generate numbers between the given range.

``````#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
int min, max, i, n_rand;
printf("Enter the minimum number: ");
scanf("%d", &min);
printf("Enter the maximum number: ");
scanf("%d", &max);

srand(time(NULL));  // seed the random number generator
for (i = 0; i < 10; i++)
{  // generate 10 random numbers
n_rand = min + rand() % (max - min + 1);  // generate a random number within the given range
printf("%d\n", n_rand);
}

return 0;
}
``````

Output:

``````Enter the minimum number: 2
Enter the maximum number: 15
11
4
11
9
13
7
2
3
8
15
``````

d. Define an Array. How do we declare and initialize a single-dimensional array and a
2-dimensional array ? Explain with an example for each.

An array is a collection of variables of the same data type, stored in contiguous memory locations. In C, arrays are defined by their data type, their name, and their size (number of elements).

Single-dimensional arrays are defined as follows:

data_type array_name[array_size];

For example, to declare an array of integers named “numbers” with a size of 10:

``int numbers;``

To initialize a single-dimensional array, you can assign values to each element in the array.

``int numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};``

A 2-dimensional array is an array that contains other arrays (i.e. arrays of arrays). It is defined as follows:

data_type array_name[row_size][column_size];

For example, to declare a 2-dimensional array of integers named “matrix” with 3 rows and 4 columns:

``int matrix;``

To initialize a 2-dimensional array, you can assign values to each element in the array.

``````int matrix = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};``````

e. Write a C program to implement STRING COPY operation that copies a string ‘‘str1’’
to another string ‘‘str2’’ without using library function.

``````#include <stdio.h>

int main()
{
char str1, str2;
int i;

printf("Enter a string: ");
scanf("%s", str1);

for (i = 0; str1[i] != '\0'; i++)
{
str2[i] = str1[i];
}

str2[i] = '\0';

printf("String copy: %s", str2);

return 0;
}
``````
``````Enter a string: BeeOwn.com
String copy: BeeOwn.com``````

Question 2:

a. Discuss dynamic memory allocation in C. Write and explain dynamic allocation functions in C.

dynamic memory allocation is the process of allocating memory at runtime, as opposed to compile-time. The two main functions used for dynamic memory allocation in C are malloc() and calloc().

malloc() function is used to allocate a block of memory of a specified size. The syntax is as follows:

``void *malloc(size_t size);``

It takes a single argument, the size of the memory block to be allocated, in bytes. It returns a pointer to the first byte of the allocated memory block, or NULL if the memory allocation failed.

For example:

``````int *ptr;
ptr = (int *)malloc(10 * sizeof(int));``````

This code allocates memory for 10 integers and assigns the address of the first byte of the allocated memory block to the pointer ‘ptr’.

calloc() function is used to allocate and initialize a block of memory for an array of elements. The syntax is as follows:

``void *calloc(size_t nmemb, size_t size);``

It takes two arguments: the number of elements in the array and the size of each element. It returns a pointer to the first byte of the allocated memory block, or NULL if the memory allocation failed.

For example:

``````int *ptr;
ptr = (int *)calloc(10, sizeof(int));``````

This code allocates memory for 10 integers and assigns the address of the first byte of the allocated memory block to the pointer ‘ptr’ and also set all the memory block to zero.

It’s important to note that, once memory is dynamically allocated, it is the responsibility of the programmer to free it when it is no longer needed, using the free() function.

b. Write and explain any two pre-processor directives in C.

#include: This directive is used to include the contents of one file into another file. It is typically used to include header files in a C program. The syntax is as follows:

``````#include <file>

or

#include "file"``````

The angle brackets <> are used for system header files, while double quotes ” ” are used for user-defined header files. The preprocessor searches for the specified file in a predefined list of directories and includes the contents of the file in the program.

#define: This directive is used for creating a macro or a constant in C. It is used to replace a specific token in the code with a predefined value. The syntax is as follows:

``#define PI 3.14``

In this case, every occurrence of the token “PI” in the code will be replaced with the value 3.14 by the preprocessor. It can also be used to define a macro function that can be used to perform a specific operation with the help of the parameters passed to it.

c. Write a C program to find the sum of diagonal elements of a 3 x 3 matrix.

``````#include <stdio.h>
int main()
{
int matrix, i, j, sum = 0,sum1=0;
printf("Enter the elements of the 3x3 matrix: \n");
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
scanf("%d", &matrix[i][j]);
}
}
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
if (i == j)
{
sum = sum + matrix[i][j];
}
if (i + j == 2)
{
sum1 = sum1 + matrix[i][j];
}
}
}
printf("Sum of  Right diagonal elements of the matrix: %d \n", sum);
printf("Sum of Left diagonal  elements of the matrix: %d", sum1);
return 0;
}
``````

Question 3:

a. Write a C program to create a file of numbers given by the user and copy odd numbers to odd.dat file and even numbers to even.dat file.

``````#include <stdio.h>

int main()
{
int n, num;
FILE *odd_file, *even_file;

// Open odd.dat and even.dat in write mode
odd_file = fopen("odd.dat", "w");
even_file = fopen("even.dat", "w");

printf("Enter number of numbers: ");
scanf("%d", &n);

for (int i = 0; i < n; i++)
{
printf("Enter number: ");
scanf("%d", &num);

// Check if number is even or odd
if (num % 2 == 0)
{
fprintf(even_file, "%d\n", num);
}
else
{
fprintf(odd_file, "%d\n", num);
}
}

// Close the files
fclose(odd_file);
fclose(even_file);

return 0;
}
``````

Output:

``````Enter number of numbers: 5
Enter number: 12
Enter number: 25
Enter number: 45
Enter number: 75
Enter number: 95

//Even Numbers Are in even.dat
12

//Odd Numbers Are in odd.dat
25
45
75
95

``````

b. Using recursion, write a C program to find the factorial of a given number.

``````#include <stdio.h>

int factorial(int n)
{
if (n == 0)
{
return 1;
}
return n * factorial(n-1);
}

int main()
{
int num;

printf("Enter a number: ");
scanf("%d", &num);

printf("Factorial of %d is %d\n", num, factorial(num));

return 0;
}
``````

Output:

``````Enter a number: 5
Factorial of 5 is 120``````

Question 4:

a. Write a function to print the sum of the following series : 1 + 22+33… + nn where ‘‘n’’ is passed as an argument to the function.

``````#include <stdio.h>
#include <math.h>

double sum_of_series(int n)
{
double sum = 0;
for (int i = 1; i <= n; i++)
{
sum = sum + pow(i,i);
}
return sum;
}

int main()
{
int n;
double result;
printf("Enter the value of n: ");
scanf("%d", &n);

result = sum_of_series(n);
printf("Sum of the series is: %lf\n", result);
return 0;
}
``````

Output:

``````Enter the value of n: 3
Sum of the series is: 32.000000``````

b. Write a program to calculate the number of vowels (a, e, i, o, u) separately in the entered string and display their individual count.

``````#include <stdio.h>

int main()
{
char str;
int i,a_count = 0, e_count = 0, i_count = 0, o_count = 0, u_count = 0;
printf("Enter a string: ");
scanf("%s", str);

for (i = 0; str[i] != '\0'; i++)
{
if (str[i] == 'a' || str[i] == 'A')
{
a_count++;
}
else if (str[i] == 'e' || str[i] == 'E')
{
e_count++;
}
else if (str[i] == 'i' || str[i] == 'I')
{
i_count++;
}
else if (str[i] == 'o' || str[i] == 'O')
{
o_count++;
}
else if (str[i] == 'u' || str[i] == 'U')
{
u_count++;
}
}
printf("Number of vowels in the string:\n");
printf("A: %d\n", a_count);
printf("E: %d\n", e_count);
printf("I: %d\n", i_count);
printf("O: %d\n", o_count);
printf("U: %d\n", u_count);
return 0;
}``````
``````Enter a string: Welcome to beeown.com
Number of vowels in the string:
A: 0
E: 2
I: 0
O: 1
U: 0``````

Question 5:

a. Write two differences between a structure and a union with appropriate examples.

b. Write a C program to find the substring in a string without using a library function.

``````#include<stdio.h>
#include<string.h>

void main()
{
char st,st1;
int l,l1,i,j,f;
printf("Enter The Main String \n");
gets(st);
printf("Enter The Substring To Search \n");
gets(st1);
f=0;
l=strlen(st);
l1=strlen(st1);

for (i = 0; i < l; i++)
{
for(j = 0; j < l1; j++)
{
if(st[i+j]!=st1[j])
{
break;
}
}
if(j==l1)
{
f=1;
break;
}
}
if(f==1)
printf("Substring Is Found At %d",i+1);
else
}``````
``````Enter The Main String
Welcome To BeeOwn.com
Enter The Substring To Search
Own
Substring Is Found At 15``````

c. Write a program to delete an element from a given location of an array of integers.

``````#include <stdio.h>

int main()
{
int arr,i,p,n;
printf("Enter The Size Of Araay: \n");
scanf("%d",&n);

printf("Enter The element In Araay: \n");
for (i = 0; i < n; i++)
{
scanf("%d",&arr[i]);
}

printf("Enter the index of the element to be deleted: \n");
scanf("%d", &p);

printf("The array beforer deletion: \n");
for (i = 0; i < n; i++)
{
printf("%d ", arr[i]);
}

if (p >= n)
{
printf("Invalid index.\n");
}
for (i = p; i < n - 1; i++)
{
arr[i] = arr[i + 1];
}

printf("\n The array after deletion:\n");
for (i = 0; i < n - 1; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
``````

Output:

``````Enter The Size Of Araay:
5
Enter The element In Araay:
12
36
54
48
65
Enter the index of the element to be deleted:
3
The array beforer deletion:
12 36 54 48 65
The array after deletion:
12 36 54 65 ``````