You are on page 1of 98

SEMESTER-1

ASSIGNMENTS

COMPUTER SCIENCE HONOURS


NNDC CMSA (2020-2023)
CONTENTS :
1. WAP to print the sum and product of digits of an integer.
2. WAP to reverse a number.
3. WAP to compute the sum of the first n terms of the following series,
S=1+1/2+1/3+1/4+……
4. WAP to compute the sum of the first n terms of the following series, S =1-2+3-
4+5…………….
5. Write a function that checks whether a given string is Palindrome or not. Use
this function to find whether the string entered by user is Palindrome or not.
6. Write a function to find whether a given no. is prime or not. Use the same to
generate the prime numbers less than 100.
7. WAP to compute the factors of a given number.
8. Write a macro that swaps two numbers. WAP to use it.
9. WAP to print a triangle of stars as follows (take number of lines from user):
*
***
*****
*******
*********
10. WAP to perform following actions on an array entered by the user :
i) Print the even-valued elements
ii) Print the odd-valued elements
iii) Calculate and print the sum and average of the elements of array
iv) Print the maximum and minimum element of array
v) Remove the duplicates from the array
vi) Print the array in reverse order
The program should present a menu to the user and ask for one of the options.
The menu should also include options to re-enter array and to quit the program.
11. WAP that prints a table indicating the number of occurrences of each alphabet
in the text entered as command line arguments.
12. Write a program that swaps two numbers using pointers.
13. Write a program in which a function is passed address of two variables and
then alter its contents.
14. Write a program which takes the radius of a circle as input from the user,
passes it to another function that computes the area and the circumference of the
circle and displays the value of area and circumference from the main() function.
15. Write a program to find sum of n elements entered by the user. To write this
program, allocate memory dynamically using malloc() / calloc() functions or new
operator.
16. Write a menu driven program to perform following operations on strings:
a) Show address of each character in string
b) Concatenate two strings without using strcat function.
c) Concatenate two strings using strcat function.
d) Compare two strings
e) Calculate length of the string (use pointers)
f) Convert all lowercase characters to uppercase
g) Convert all uppercase characters to lowercase
h) Calculate number of vowels
i) Reverse the string
17. Given two ordered arrays of integers, write a program to merge the two-arrays
to get an ordered array.
18. WAP to display Fibonacci series (i) using recursion, (ii) using iteration.
19. WAP to calculate Factorial of a number (i) using recursion, (ii) using iteration.
20. WAP to calculate GCD of two numbers (i) with recursion (ii) without recursion.
21. Write a menu-driven program to perform following Matrix operations (2-D
array implementation): a) Sum b) Difference c) Product d) Transpose
22. Copy the contents of one text file to another file, after removing all
whitespaces.
23. Write a function that reverses the elements of an array in place. The function
must accept only one pointer value and return void.
24. Write a program that will read 10 integers from user and store them in an
array. Implement array using pointers. The program will print the array elements
in ascending and descending order.
25. Add two distances in meter kilometer system using structure.
26. Add two complex numbers using structures.
27. Calculate the difference between two time periods using structures.
ASSIGNMENT-1 DATE:01.03.2021

PROBLEM STATEMENT: WAP to print the sum and product of digits of an integer.

ALGORITHM : SUM_AND_PRODUCT_OF_AN_INTEGER

INPUT : num
OUTPUT : print the sum and product of ‘num’

STEPS----

STEP 0: START

STEP 1 : READ num

STEP 2 : REPEAT WHILE num!=0

a) rem = num %10

b) sum= sum +num

c) product=product * num

d) num=num/10

STEP 3 : DISPLAY sum and product

STEP 4 : END

SOURCE CODE :

/*Print the sum and product of the digits of an integer */


#include<stdio.h>
#include<stdlib.h>
int main()
{
int num,rem,sum=0,product=1; //Variable declaration
system("@cls||clear");
printf("Enter an integer: ");
scanf(" %d",&num); // Input of integer
while(num!=0)
{
rum=num%10;
sum+=rem; // sum of reminders
product*=rem; // product of reminders
num/=10;
}
printf("\nSum is : %d \nProduct is : %d\n ",sum,product);
getchar();
return 0;
}
OUTPUT :

1)
Enter an integer: 534

Sum is : 12
Product is : 60

2)
Enter an integer: 458

Sum is : 17
Product is : 160

3)
Enter an integer: 324

Sum is : 9
Product is : 24

ASSIGNMENT-2 DATE:01.03.2021
Problem Statement :- WAP to reverse a number.
Algorithm :- reverse()
Input :- A number ‘n’ .
Output :- Print the reverse of ‘n’.
Steps :-
1) Initialize rev= 0 and rem= 1.
2) Repeat
a) rem=n%10 /*rem is the last digit of the number ‘n’*/
b) rev=rev*10+rem /*rev is the reverse of the number ‘n’*/
c) n=n/10
Until (n!=0).
3) Stop.
Source Code :-
#include<stdio.h>
#include<conio.h>
int main()
{
int n,rev=0,rem=1;
printf("\n Welcome User! Enter A Number:- ");
scanf("%d",&n);
do
{
rem=n%10; /*Here 'rem' stands for remainder,equal to modulus of 'n'*/
rev=rev*10+rem; /*'rev' stands for reverse of the number entered.*/
n=n/10; /*value of 'n', for the next passage changes to the n/10*/
}while(n!=0);
printf("\n The Reverse Of The Number You Have Entered Is %d.",rev);
return 0;
}
Output :-
1) Welcome User! Enter A Number:- 123
The Reverse Of The Number You Have Entered Is 321.
2) Welcome User! Enter A Number:- 14
The Reverse Of The Number You Have Entered Is 41.

ASSIGNMENT-3 DATE:01.03.2021
PROBLEM STATEMENT: WAP to compute the sum of the first n terms of the
following series,
S=1+1/2+1/3+1/4+……

I. ALGORITHM FOR THE PROGRAM

Start

Read n

Set sum=0.0

for i=1 to n do loop

sum=1/i+sum

II. STEPS FOR EXECUTION

STEP 1: Print "Enter an integer."

STEP 2: Read the integer 'n'

STEP 3: Check for condition

STEP 4: If condition is true go to step 5

STEP 3: sum=1/i+sum

STEP 4: Print sum

STEP 5: Exit

III. SOURCE CODE FOR THE PROGRAM

#include<stdio.h>

int main()

int n;

float sum=0.0,i;

clrscr();

printf("Enter an integer:");
scanf(" %d",&n);

for(i=1.0;i<=n;i++) //loop for increasing the denominator of each term

sum=1/i+sum;

printf("\n sum = %f",sum);

return 0;

IV. OUTPUT

Enter an integer: 3

sum=1.833333

Enter an integer: 5

sum=2.283334

Enter an integer: 9

sum=2.828969

Enter an integer: 13

sum=3.180134

ASSIGNMENT-4 DATE:01.03.2021
Problem Statement :-
WAP to compute the sum of the first n terms of the following series, S =1-2+3-
4+5…………….
Algorithm :- find_sum()
Input :- A number ‘n’.
Output :- Print the sum of the series up to ‘n’ number of terms.
Steps :-
1) Initialize sum = 0 and temp =1.
2) For(i=1 to n) do
a) sum= sum+(i* temp) /*Summing the product of i and temp*/
b) temp=-temp; /*Assigning temp to be negative*/
c) end For
3) Stop.
Source Code :-
#include<stdio.h>
#include<conio.h>
int main()
{
int sum=0,n,temp=1,i; /*Variable declaration*/
printf("\n Welcome User! Enter The Value Of 'n':- ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
sum=sum+(i*temp); /*Summing the product of i and temp*/
temp=-temp; /*Assigning temp to be negative*/
}
printf("\n The Sum Of Upto %d Terms Is %d",n,sum);
return 0;
}
Output :-
1)Welcome User! Enter The Value Of 'n':- 4
The Sum Of Upto 4 Terms Is -2.
2) Welcome User! Enter The Value Of 'n':- 6
The Sum Of Upto 4 Terms Is -3.

ASSINGMENT-5 DATE:01.03.2021
PROBLEM STATEMENT: Write a function that checks whether a given string is
Palindrome or not. Use this function to find whether the string entered by user is
Palindrome or not.

ALGORITHM: Checks_whether_a_given_string_is_Palindrome_or_not.
Input: Get a string s.
Output: The string s palindrome or not.
Steps:-
Step 1: Print “Enter a string:”
Step 2: Read the string s.
Step 3: FOR LOOP until s[i]!=’NULL’ then
3.1. Increase n.[Calculating string length]

Step 4: FOR LOOP until s[j]!=’NULL’ -


4.1. If s[i]>=’A’ and s[i]<=’Z’ then
If (s[i]+32)==s[j] then [ASCII value of ‘A’=65
and ‘a’=97 so to convert character from upper case to lower
case.]
f++
4.2. Else If s[j]>=’A’ and s[j]<=’Z’ then[ASCII value of ‘A’=65
and ‘a’=97 so to convert character from upper case to lower
case.]

If s[i]==(s[j]+32) then
f++
4.3. Else
If s[i]==s[j] then
f++
Step 5: End Loop
Step 6: If f==n then
6.1. Print “The string s is a Palindrome.”
Step 7: Else
7.1 Print “The string s is not a Palindrome.”
Step 8: Stop

SOURCE CODE :-
/*Checks whether a given string is Palindrome or not*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main(void)
{
int i,f=0,n=0,j;
char s[20];
system("cls");/*To Clear The Screen*/
printf("Enter a string:");
scanf("%s",s); /*Scaning The String s*/
for(i=0;s[i]!='\0';i++)/*Calculating String Length*/
n++;
for(i=n-1,j=0;s[j]!='\0';i--,j++)/*String Compering*/
{
if(s[i]>='A'&&s[i]<='Z')
{
if((s[i]+32)==s[j])/*To Convert Upper Case Character into Lowwer Case Using
ASCII Value*/
f++;/*To Compare With String Length*/
}
else if(s[j]>='A'&&s[j]<='Z')
{
if(s[i]==(s[j]+32))/*To Convert Upper Case Character into Lowwer Case Using
ASCII Value*/
f++;
}
else
{
if(s[i]==s[j])
f++;
}
}
/*Output*/
if(f==n)
printf("\n%s is a Palindrome.\n",s);
else
printf("\n%s is not a Palindrome.\n",s);
getch();
}
Output :-
1.
Enter a string:Malayalam

Malayalam is a Palindrome.
2.
Enter a string:String

String is not a Palindrome.

ASSIGNMENT-6 DATE:01.03.2021
Problem Statement: Write a function to find whether a given no. is prime or not.
Use the same to generate the prime numbers less than 100.
Algorithm: Prime_number
Input: num
Output: Whether 'num' is a prime or not.
Generate prime numbers upto 100.
Steps:
1. START
2. Initialize i=2
3. Read num
4. if (check_prime (num)==1) then
Display num is prime.
else
Display num is not prime
End if
5. Repeat till i<100
if (check_prime(i)==1) then
display i
End if
i++
6. End.
Here we have used a procedure named check_prime ().
The body of the procedure given below,
procedure check_prime (int a)
1. for c=2 till c < = a/2 do
if(a%c == 0 ) then
return 0
End if
c ++
2. return 1
3. End procedure
Source Code:
#include<stdio.h>
#include<stdlib.h>
int check_prime(int );
int main()
{
int i,num;
system("@cls||clear");
printf("\nEnter a number : ");
scanf("%d",&num);
if(check_prime(num)==1)
printf("\n%d is a prime number",num);
else
printf("\n%d is not a prime number",num);
printf("\nPrime numbers upto 100 is :");
for(i=2;i<100;i++)
{
if(check_prime(i)==1)
printf("\n%d",i);

}
printf("\n");
getchar();
return 0;
}
int check_prime(int a)
{
int c;
for(c=2;c<=a/2;c++)
{
if(a%c==0)
return 0;
}
return 1;
}
Output:
1)

Enter a number : 52

52 is not a prime number


Prime numbers upto 100 is :
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97

2)
Enter a number : 53

53 is a prime number
Prime numbers upto 100 is :
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97

3)

Enter a number : 99

99 is not a prime number


Prime numbers upto 100 is :
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97

Assignment:7 DATE:22.12.2020
Problem Statement: WAP to compute the factors of a given number.
Algorithm: factors of number
Input: ‘num’
Output: Factors of ‘num’
Steps:
1. START
2. Read num
3. for i=1 till i<num do
++i
if(num%i==0) then
display i
End if
4. End
Source Code:
#include<stdio.h>
#include<stdlib.h>
int main()
{
int num,i;
system("@cls||clear");
printf("Enter a number: ");
scanf("%d",&num);
printf("Factors of %d are: ", num);
for(i=1;i<=num;i++)
{
if(num %i== 0)
printf(" %d ", i);
}
printf("\n");
return 0;
}
Output:
1)
Enter a number: 10
Factors of 10 are: 1 2 5 10

2)
Enter a number: 23
Factors of 23 are: 1 23

3)
Enter a number: 58
Factors of 58 are: 1 2 29 58

Assignment: 8 DATE:22.12.2020
Problem Statement: Write a macro that swaps two numbers. WAP to use it.

Algorithm: Swapping two numbers


Input: a,b
Output: swapped a,b
Steps:
1. START
2. Read a,b
3. swap(a,b)
4. Display a,b
5. End
Here we have used a procedure/macro named swap. The body of swap is,
Swap(x,y)
1. x=x+y
2. y=x-y
3. x=x-y
4. End procedure
Source Code:
#include<stdio.h>
#include<stdlib.h>
#define SWAP(x,y) {x=x+y;y=x-y;x=x-y;}
int main()
{
int a,b;
system("@cls||clear");
printf("Enter two number : ");
scanf(" %d %d",&a,&b);
printf("\n Values before swapping\n num1= %d num2= %d ",a,b);
SWAP(a,b);
printf("\n Values after swapping\n num1= %d num2= %d\n",a,b);
getchar();
return 0;
}
Output:
1)
Enter two number : 3 4

Values before swapping


num1= 3 num2= 4
Values after swapping
num1= 4 num2= 3

2)
Enter two number : 15 67

Values before swapping


num1= 15 num2= 67
Values after swapping
num1= 67 num2= 15

3)
Enter two number : 72 27

Values before swapping


num1= 72 num2= 27
Values after swapping
num1= 27 num2= 72

ASSIGNMENT-9 DATE:22.12.2020
PROBLEM STATEMENT : WAP to print a triangle of stars as follows (take number of
lines from user):
*
***
*****
*******
*********

Algorithm: Triangle_Pattern
Input: n
Output: Triangle pattern of ‘n’ lines
Steps:
1. START
2. Read n
3. i=1, j=1, k=1
4. Repeat till i<=n
a. Repeat till j<=n-i
Display space
++j
End loop
b. Repeat till k<=2*i-1
Display *
++k
End loop
c. ++i
d. End loop
5. End
Source Code:
#include<stdio.h>
#include<stdlib.h>
int main()
{
int i,j,n,k;
system("@cls||clear");
printf("\nEnter the number of lines:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n-i;j++)
{
printf(" ");
}
for(k=1;k<=2*i-1;k++)
{
printf("*");
}
printf("\n\n");
}
getchar();
return 0;
}

Output:

Output 1:
Enter the number of lines:5
*

***

*****
*******

*********

Output 2:
Enter the number of lines:7
*

***

*****

*******

*********

***********

*************

Output 3:
Enter the number of lines:6
*

***

*****

*******

*********

***********

ASSIGNMENT-10 DATE:05.01.2021
PROBELEM STATEMENT: WAP to perform following actions on an array entered by
the user :
i) Print the even-valued elements
ii) Print the odd-valued elements
iii) Calculate and print the sum and average of the elements of array
iv) Print the maximum and minimum element of array
v) Remove the duplicates from the array
vi) Print the array in reverse order
The program should present a menu to the user and ask for one of the options.
The menu should also include options to re-enter array and to quit the program
A) Algorithm:
1. Create an array, take its size from users and define its elements using a
loop.

2. Take an iterator in a for loop, using which, all the elements of the array are
accessed.

3. Iterator is used to reach out every position of the array, scanning the
particular array element and checking whether it is divisible by 2 , thus
sorting even numbers and printing them.

B) Source Code:
#include <stdio.h>
int main()
{
int array[10];
printf("Enter the 10 array elements: \n");
for (int i = 0; i <= 9; i++)
{
printf(" > ");
scanf("%d", &array[i]);
}
printf("\n\nEven numbers in array: ");
for (int i = 1; i <= 9; i++)
{
if (i % 2 == 0)
{
printf("%d, ", i);
}
}
return 0;
}
C) Output:
Enter the 10 array elements:
> 1 2 3 4 5 6 7 8 9 10
> > > > > > > > >

Even numbers in array: 2, 4, 6, 8,


--------------------------------
Process exited after 9.072 seconds with return value 0
Press any key to continue . . .

II) Print the odd-valued elements.

A) Algorithm:
1. Create an array, take its size from users and define its elements using a
loop.

2. Take an iterator in a for loop, using which, all the elements of the array are
accessed.

3. Iterator is used to reach out every position of the array, scanning the
particular array element and checking whether it is divisible by 2 or not , thus
sorting odd numbers and printing them.

B) Source Code:
#include <stdio.h>
int main()
{
int array[10];
printf("Enter the 10 array elements: \n");
for (int i = 0; i <= 9; i++)
}
printf(" > ");
scanf("%d", &array[i]);
}
printf("\n\nOdd numbers in array: ");
for (int i = 1; i <= 9; i++)
{
if (i % 2 != 0)
{
printf("%d, ", i);
}
}
return 0;
}

C) Output:
Enter the 10 array elements:
> 1 2 3 4 5 6 7 8 9 10
> > > > > > > > >

Odd numbers in array: 1, 3, 5, 7, 9,


--------------------------------
Process exited after 17.26 seconds with return value 0
Press any key to continue . . .

III) Calculate and print the sum and average of the elements of array:

A) Algorithm:
1. Take n, a variable that stores the number of elements of the array.
2. Create an array of size n.
3. Iterate via for loop to take array elements as input, and print them.
4. Iterate via for loop to access each element of array to get the sum of all the
elements.
5. The average is calculated by dividing the overall sum to the number of elements
in the array.
6. Sum and average are printed on the screen.

B) Source Code:
#include <stdio.h>
int main()
{
int Arr[100], n, i, sum = 0;
printf("Enter the number of elements you want to insert : ");
scanf("%d", &n);
for (i = 0; i < n; i++)
{
printf("Enter element %d : ", i + 1);
scanf("%d", &Arr[i]);
sum += Arr[i];
}

printf("\nThe sum of the array is : %d", sum);


printf("\nThe average of the array is : %0.2f", (float)sum / n);
return 0;
}

C) Output:
Enter the number of elements you want to insert : 6
Enter element 1 : 10
Enter element 2 : 20
Enter element 3 : 15
Enter element 4 : 18
Enter element 5 : 21
Enter element 6 : 19

The sum of the array is : 103


The average of the array is : 17.17

IV) Print the maximum and minimum element of array:

A) Algorithm:
1 . Input size and element in array, store it in some variable say size and arr.
2. Declare two variables max and min to store maximum and minimum. Assume
first array element as maximum and minimum both, say max = arr[0] and min =
arr[0].
3 . Iterate through array to find maximum and minimum element in array. Run loop
from first to last array element i.e. 0 to size - 1. Loop structure should look like
for(i=0; i<size; i++).
4 . Inside loop for each array element check for maximum and minimum. Assign
current array element to max, if (arr[i] > max). Assign current array element to min
if it is less than min i.e. perform min = arr[i] if (arr[i] < min).
B) Source Code:
#include <stdio.h>
#include <conio.h>
int main()
{
int a[1000],i,n,min,max;
printf("Enter size of the array : ");
scanf("%d",&n);
printf("Enter elements in array : ");
for(i=0; i<n; i++)
{
scanf("%d",&a[i]);
}
min=max=a[0];
for(i=1; i<n; i++)
{
if(min>a[i])
min=a[i];
if(max<a[i])
max=a[i];
}
printf("minimum of array is : %d",min);
printf("\nmaximum of array is : %d",max);
return 0;
}
C) Output:
Enter size of the array : 5
Enter elements in array : 10
2
30
40
50
minimum of array is : 2
maximum of array is : 50
--------------------------------
Process exited after 25.46 seconds with return value 0
Press any key to continue . . .
V) Remove the duplicates from the array.

A) Algorithm:
1. Input the number of elements of the array.
2. Input the array elements.
3. Repeat from i = 1 to n
- if (arr[i] != arr[i+1])
- temp[j++] = arr[i]
- temp[j++] = arr[n-1]
4. Repeat from i = 1 to j
- arr[i] = temp[i]
return j.

B) Source Code:
#include <stdio.h>
int remove_duplicate(int arr[], int n)
{
if (n == 0 || n == 1)
return n;
int temp[n];
int j = 0;
int i;
for (i = 0; i < n - 1; i++)
if (arr[i] != arr[i + 1])
temp[j++] = arr[i];
temp[j++] = arr[n - 1];

for (i = 0; i < j; i++)


arr[i] = temp[i];
return j;
}
int main()
{
int n;
printf("Enter the size of array :");
scanf("%d", &n);
int arr[n];
int i;
for (i = 0; i < n; i++)
{
printf("\nEnter the elements of array :");
scanf("%d", &arr[i]);
}
printf("\n\nArray Before Removing Duplicates: ");
for (i = 0; i < n; i++)
printf("%d ", arr[i]);

n = remove_duplicate(arr, n);
printf("\nArray After Removing Duplicates: ");
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}

C) Output:
sEnter the size of array :6

Enter the elements of array :10

Enter the elements of array :20

Enter the elements of array :30

Enter the elements of array :30

Enter the elements of array :30

Enter the elements of array :40

Array Before Removing Duplicates: 10 20 30 30 30 40

Array After Removing Duplicates: 10 20 30 40

--------------------------------

Process exited after 21.78 seconds with return value 0

Press any key to continue . . .

VI) Print the array in reverse order.


A) Algorithm:
1. Input the number of elements of an array.
2. Input the array elements.
3. Traverse the array from the last.
4. Print all the elements.

B) Source Code:
#include<stdio.h>
#define size 5
int main()
{
int a[5],i;
printf("enter the numbers into array");
for (i=0;i<size;i++)
{
printf("\n Enter the numbers in pockets [%d]",i);
scanf("%d",&a[i]);
}
printf("\n the reverse array is");
for(i=size-1;i>=0;i--)
{
printf(" %d ",a[i]);
}
return 0;
}
C) Output:
enter the numbers into array
enter the numbers in pockets [0]1
enter the numbers in pockets [1]2
enter the numbers in pockets [2]3
enter the numbers in pockets [3]4
enter the numbers in pockets [4]5

the reverse array is 5 4 3 2 1


--------------------------------
Process exited after 7.535 seconds with return value 0
Press any key to continue . . .
ASSIGNMENT-11 DATE:19.01.2021

Problem Statement: WAP that prints a table indicating the number of occurrences
of each alphabet in the text entered as command line arguments.

Algorithm: frequency of character


Input: argv[1] as command line argument
Output: frequency of each character in given string.
Steps:
1. START
2. if argc != 2 then
Display error message
End
End if
3. frequency_char(argv[1])
4. End
Here we have used procedure named frequency_char() and stringlength().
The bodies are given below,
Procedure frequency_char(char *s)
1. Initialise count = 0
2. n = stringlength(s)
3. for i=0 till i<n do
i. count=1
ii. if s[i] = ‘\0’ then
a. for j=i+1 till j<n do
if(s[i]==s[j]) then
count++
s[j]=’\0’;
End if
++j
End loop.
Display count of s[i]
End if
++i
End loop
3. End procedure
Procedure stringlength(char *s)
1. for j=0 till s[j] = ‘\0’ do
++j
2. Return j
3. End procedure
Source Code:
#include<stdio.h>
#include<stdlib.h>
int stringlength(char *);
void frequency_char(char *);
int main(int argc,char *argv[])
{
if(argc!=2)
{
printf("Wrong number of argument passed");
printf("\nNOTE: pass your argument between \" \"\n ");
return -1;
}
frequency_char(argv[1]);
return 0;
}
void frequency_char(char *s)
{
int i,j,count=0,n;
n=stringlength(s);
printf("Frequency count charecter in string : \n");
for(i=0;i<n;i++)
{
count=1;
if(s[i])
{
for(j=i+1;j<n;j++)
{
if(s[i]==s[j])
{
count++;
s[j]='\0';
}
}
printf(" '%c'= %d \n",s[i],count);
}
}
}
int stringlength(char *s)
{
int j;
for(j=0;s[j];j++);
return j;
}
Output:
1)
./assignment11 "Rahul Biswas"
Frequency count charecter in string :
'R'= 1
'a'= 2
'h'= 1
'u'= 1
'l'= 1
' '= 1
'B'= 1
'i'= 1
's'= 2
'w'= 1

2)
./assignment11 "Argho ghosh"

Frequency count charecter in string :

'A'= 1

'r'= 1

'g'= 2

'h'= 3

'o'= 2

' '= 1

's'= 1

3)
./assignment11 "Deblina nandy"

Frequency count charecter in string :

'D'= 1

'e'= 1

'b'= 1

'l'= 1

'i'= 1

'n'= 3

'a'= 2

' '= 1

'd'= 1

'y'= 1

ASSINGMENT-12 DATE:22.12.2020

PROBLEM STATEMENT: Write a program that swaps two numbers using pointers.

ALGORITHM: Swap_two_numbers_using_pointers.
Input: Get two numbers.
Output: After swap two numbers.
Steps:-
Step 1: Start
Step 2: Print “Enter two numbers:”
Step 3: Read two numbers a and b.
Step 4: Transfer their address into two pointers p and q. [p=&a and q=&b]
Step 5: Assign *p value to a s variable [s=*p]

Step 6: Assign *q value to *p [*p=*q]


Step 7: Assign s value to *q [*q=s]
Step 8: Print “Result of A=value of a and B=value of b”
Step 9: Stop

SOURCE CODE :-
/*Swap two numbers using pointers*/
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

void main()
{
int *p,*q,a,b,s;
system("cls");
printf("Enter A and B:");
scanf("%d%d",&a,&b);/*Scanning Two Values*/
p=&a;/*p Stored Address of variable a*/
q=&b;/*q Stored Address of variable b*/
/*Swapping*/
s=*p;
*p=*q;
*q=s;
/*Output*/
printf("Result:\nA=%d\nB=%d",a,b);
getch();
}
OUTPUT:-
1.
Enter A and B: 22 40
Result:
A=40
B=22
2.
Enter A and B: 508 40
Result:
A=40
B=508

ASSINGMENT-13 DATE:01.03.2021

PROBLEM STATEMENT : . Write a program in which a function is passed address of


two variables and then alter its contents.

ALGORITHM : swaping two variables

INPUT : a=4,b=5
OUTPUT : after swapping: a=4,b=5

STEPS----

STEP 1 : start

STEP 2:input a and b

STEP 3 : a)temp=*x
b)*x=*y
c)*y=temp

STEP 4 :print a and b

STEP 5:Exit

SOURCE CODE :

/* swapping two numbers without third veriable using pointer*/


#include<stdio.h>
#include<conio.h>
void swap(int *,int *);
int main()

{
int a,b;
printf("enter two numbers");
scanf("%d %d",&a,&b);
printf("\nbefore swapping: a=%d,b=%d",a,b);
swap(&a,&b);
printf("\nafter swapping: a=%d, b=%d", a,b);

}
void swap(int *x,int *y)
{
int temp;
temp=*x;
*x=*y;
*y=temp;
}

OUTPUT:
enter two numbers 4
5
before swapping: a=4,b=5
after swapping : a=5,b=4

--------------------------------------
Process exited after 2.992 seconds with return value 0
Press any key to continue . . .

ASSINGMENT-14 DATE:01.03.2021

Problem Statement :- Write a program which takes the radius of a circle as input
from the user, passes it to another function that computes the area and the
circumference of the circle and displays the value of area and circumference from
the main() function.

ALGORITHM:
INPUT : An integer ‘r’.
OUTPUT : Print the values of a and c.

STEPS:

Step 1: Start .
Step 2: declare variable r, a and c.
Step 3: Read the number ‘r’.
Step 4: call function area_cir((&r, &a, &c).
Step 6: print ‘a’ and ‘c’.
Step 7: Stop

Here we have used a procedure by the name of ‘area_cir()’ where location of two
variables(&r, &a, &c) are passed as two arguments(r, a, c). The body of the procedure is as
follows:
Procedure area_cir()
Steps:-

Step 1: Start
Step 2: *a=3.14*(*r)*(*r);
Step 3: *c=2*3.14*(*r);
Step 4: Stop.

SOURCE CODE :

/*calculating the area and circumference of a circle from given input using function
calling*/

#include<stdio.h>
#include<conio.h>
void area_cir(int *, float *,float *); //function declaration
void main() //main function
{
int r; //variable declaration
float a,c;
printf("enter the radius: ");
scanf("%d",&r);
area_cir(&r,&a,&c); //calling the function 'area_cir()'
printf("\n area = %f, circumference = %f",a,c);
getch();
}
void area_cir(int *r, float *a, float *c)
{
*a=3.14*(*r)*(*r); // calculating the area
*c=2*3.14*(*r); // calculating the circumference
}
OUTPUT :

1)
enter the radius: 5
area = 78.500000, circumference = 31.400000

2)
enter the radius: 7
area = 153.860001, circumference = 43.959999

3)
enter the radius: 10
area = 314.000000, circumference = 62.799999

ASSIGNMENT -15 DATE:01.03.2021

Problem Statement: Write a program to find sum of n elements entered by the


user. To write this program, allocate memory dynamically using malloc() / calloc()
functions or new operator.

Algorithm:-
Algorithm sum()
Input:- An dynamically allocated array of numbers.
Output:- Print sum of the elements of the array.
Steps:-
1) Initialize sum =0.
2) Dynamically allocate memery for an array ‘a’. /* done using malloc() or calloc()
function*/
3) print “Enter The Number Of Elements”
4) Read n. */ n is the number of elements /*
5) Print “Enter The Elements:- ”
6) For(i=0 to n-1) do
Read a[i]
End for
7) For(i=0 to n) do
sum=sum+a[i] /* summation of all elements of the array*/
End for
8) print sum
9) End
Source Code:-
#include<stdio.h>
#include<conio.h>
int main()
{
int *a,i,sum=0,n;
clrscr();
a=(int *)malloc(50*sizeof(int)); /* dynamic allocation using malloc()*/
printf("\n Welcome User. \n Enter The Number Of Elements:- ");
scanf("%d",&n);
printf("Enter The Elements:- ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
sum=sum+a[i]; /*Sum of all of the elements of the array*/
}
printf("\n Sum=%d",sum);
getch();
return 0;
}

Output:-
Welcome User!
Please The Number Of Elements:- 5
Enter The Elements:- 1
2
3
4
5
Sum= 15

ASSIGNMENT-16 DATE:19.01.2021

PROBLEM STATEMENT :
Write a menu driven program to perform following operations on strings:
a) Show address of each character in string
b) Concatenate two strings without using strcat function.
c) Concatenate two strings using strcat function.
d) Compare two strings
e) Calculate length of the string (use pointers)
f) Convert all lowercase characters to uppercase
g) Convert all uppercase characters to lowercase
h) Calculate number of vowels
i) Reverse the string

ALGORITHM:
INPUT: given strings.
OUTPUT: concatenation, length, upper case and lower case transformation,
counting vowels, reverse and comparing them.

Steps for execution


STEP 1: START
STEP 2: while True:
STEP 2.1: display the options and accept the option
STEP 2.2: switch(option):
STEP 2.2.1: case 1: showAddresses()
STEP 2.2.2: case 2: concatWithoutUsingStrcat()
STEP 2.2.3: case 3: concatWithoutStrcat()
STEP 2.2.4: case 4: compare_strings()
STEP 2.2.5: case 5: printLength()
STEP 2.2.6: case 6: printLowerCase()
STEP 2.2.7: case 7: printUpperCase()
STEP 2.2.8: case 8: totalNumberOfVowels()
STEP 2.2.9: case 9: printReversedString()
STEP 2.2.10: case 10: exit()
STEP 2.3: END SWITCH
STEP 5: END WHILE
STEP 6: STOP

definition of showAddresses()
STEP 1: START
STEP 2: take str
STEP 3: for i = 0; i < 9; i++:
STEP 3.1: print address of str[i]
STEP 4: END

definition of function compare_strings()


STEP 1: take two strings a and b as inputs
STEP 2: while a[i] == b[i]:
STEP 2.1: if a[i] == b[i] == '\0':
STEP 2.1.1: break
STEP 2.2: count++
STEP 3: END WHILE
STEP 4: if a[count] == b[count] == '\n':
STEP 4.1: print equal strings
STEP 5: else:
STEP 5.2: print unequal strings

definition of printLength()
STEP 1: START
STEP 2: take string str
STEP 3: *pt = str
STEP 4: while pt != '\0':
STEP 4.1: pt++
STEP 4.2: length++
STEP 5: END WHILE

definition of printUpperCase()
STEP 1: START
STEP 2: take string str
STEP 3: while str != '\0':
STEP 3.1: if str1[i] >= 'a' && str1[i] <= 'z':
STEP 3.1.1 str1[i] = str1[i] - 32
STEP 3.2 END WHILE
STEP 4: print str
STEP 5: END

definition of printLowerCase()
STEP 1: START
STEP 2: take string str
STEP 3: while str != '\0':
STEP 3.1: if str1[i] >= 'A' && str1[i] <= 'Z':
STEP 3.1.1 str1[i] = str1[i] + 32
STEP 3.2 END WHILE
STEP 4: print str
STEP 5: END

definition of totalNumberOfVowels()
STEP 1: START
STEP 2: take str
STEP 3: while str != '\0':
STEP 3.1: if (
str1[i] == 'a' ||
str1[i] == 'A' ||
str1[i] == 'e' ||
str1[i] == 'E' ||
str1[i] == 'i' ||
str1[i] == 'I' ||
str1[i] == 'o' ||
str1[i] == 'O' ||
str1[i] == 'u' ||
str1[i] == 'U'):
STEP 3.1.1: count++;
STEP 3.2 END WHILE
STEP 4: print count
STEP 5: END

definition of printReversedString()
STEP 1: START
STEP 2: take str
STEP 3: for i = 10; i > 0; i--:
STEP 3.1: print str[i]
STEP 4: END

definition of concatWithoutUsingStrcat()
STEP 1: START
STEP 2: take str1, str2
STEP 3: for i = 0; str1[i] != '\0'; i++
STEP 3.1: length++
STEP 4: for j = 0; str1[j] != '\0'; j++, i++
STEP 4.1: str1[i] = str2[j]
STEP 5: str1[i] = '\0';
STEP 6: END

definition of concatUsingStrcat()
STEP 1: START
STEP 2: take str1, str2
STEP 3: print strcat(str1, str2)
STEP 6: END

SOURCE CODE:

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

void showOptions();
void compare_strings();
void showAddresses();
void concatUsingStrcat();
void concatWithoutUsingStrcat();
void printLength();
void printUpperCase();
void printLowerCase();
void printReversedString();
void totalNumberOfVowels();

char str1[10], str2[10], *pt, rev[10];


int ch, i, j, count = 0, end;

int main()
{
while (1)
{
showOptions();
printf("\n Enter your choice: ");
scanf("%d", &ch);
switch (ch)
{
case 1:
showAddresses();
break;
case 2:
concatWithoutUsingStrcat();
break;
case 3:
concatUsingStrcat();
break;
case 4:
compare_strings();
break;
case 5:
printLength();
break;
case 6:
printUpperCase();
break;
case 7:
printLowerCase();
break;
case 8:
totalNumberOfVowels();
break;
case 9:
printReversedString();
break;
case 10:
exit(0);
break;
default:
printf(" wrong choice!");
}
}
}

void showAddresses()
{
printf("\n enter a string: ");
scanf("%s", str1);
printf("\n address of each character in the string is asfollows:\n ");
for (i = 0; str1[i] != '\0'; i++)
{
printf("%x, ", &str1[i]);
}
}

void showOptions()
{
printf("\n\n Options are: \n");
printf(" 1. show address of each character in a string\n");
printf(" 2. concatenate two strings without using strcat()\n");
printf(" 3. concatenate two strings using strcat()\n");
printf(" 4. compare two strings \n");
printf(" 5. find length of a string\n");
printf(" 6. convert all lower case character to upper case \n");
printf(" 7. convert all upper case character to lower case\n");
printf(" 8. count number of vowels\n");
printf(" 9. reverse the string\n");
printf(" 10. exit\n\n");
}

void concatUsingStrcat()
{
printf("\n enter first string: ");
scanf("%s", str1);
printf(" enter second string: ");
scanf("%s", str2);
printf("\n strings after concatenation: %s", strcat(str1, str2));
}
void concatWithoutUsingStrcat()
{
printf("\n enter first string: ");
scanf("%s", str1);
printf(" enter second string: ");
scanf("%s", str2);
for (i = 0; str1[i] != '\0'; i++)
;
for (j = 0; str2[j] != '\0'; i++, j++)
{
str1[i] = str2[j];
}
str1[i] = '\0';
printf("\n strings after concatenation is: %s", str1);
}

void printLength()
{
printf("\n enter a string: ");
scanf("%s", str1);
pt = str1;
i = 0;
while (*pt != '\0')
{
i++;
pt++;
}
printf("\n length of string : %d", i);
}

void printUpperCase()
{
i = 0;
printf("\n enter a string: ");
scanf("%s", str1);
while (str1[i] != '\0')
{
if (str1[i] >= 'a' && str1[i] <= 'z')
{
str1[i] = str1[i] - 32;
}
i++;
}
printf("\n after upper case conversion: %s", str1);
}

void printLowerCase()
{
i = 0;
printf("\n enter a string: ");
scanf("%s", str1);
while (str1[i] != '\0')
{
if (str1[i] >= 'A' && str1[i] <= 'Z')
{
str1[i] = str1[i] + 32;
}
i++;
}
printf("\n after lower case conversion: %s", str1);
}

void totalNumberOfVowels()
{
printf("\n enter a string: ");
scanf("%s", str1);
count = 0;
i = 0;
while (str1[i] != '\0')
{
if (str1[i] == 'a' ||
str1[i] == 'A' ||
str1[i] == 'e' ||
str1[i] == 'E' ||
str1[i] == 'i' ||
str1[i] == 'I' ||
str1[i] == 'o' ||
str1[i] == 'O' ||
str1[i] == 'u' ||
str1[i] == 'U')

count++;
i++;
}
printf("\n total number of vowels: %d", count);
}
void printReversedString()
{
printf("\n enter a string: ");
scanf("%s", str1);

while (str1[count] != '\0')


count++;

end = count - 1;

for (i = 0; i < count; i++)


{
rev[i] = str1[end];
end--;
}

rev[i] = '\0';

printf(" reversed string is: %s\n", rev);


}

void compare_strings()
{
printf("\n enter first string: ");
scanf("%s", str1);
printf("\n enter second string: ");
scanf("%s", str2);
int c = 0;
while (str1[c] == str2[c])
{
if (str1[c] == '\0' || str2[c] == '\0')
break;
c++;
}

if (str1[c] == '\0' && str2[c] == '\0')


{
printf(" equal strings!\n");
}
else
{
printf(" unequal strings\n");
}
}
Output:

$ ./a.out
Options are:
1. show address of each character in a string
2. concatenate two strings without using strcat()
3. concatenate two strings using strcat()
4. compare two strings
5. find length of a string
6. convert all lower case character to upper case
7. convert all upper case character to lower case
8. count number of vowels
9. reverse the string
10. exit

Enter your choice: 1

enter a string: hello

address of each character in the string is as follows:


c26cb8c0, c26cb8c1, c26cb8c2, c26cb8c3, c26cb8c4,

Options are:
1. show address of each character in a string
2. concatenate two strings without using strcat()
3. concatenate two strings using strcat()
4. compare two strings
5. find length of a string
6. convert all lower case character to upper case
7. convert all upper case character to lower case
8. count number of vowels
9. reverse the string
10. exit

Enter your choice: 2

enter first string: apple


enter second string: ball

strings after concatenation is: appleball


Options are:
1. show address of each character in a string
2. concatenate two strings without using strcat()
3. concatenate two strings using strcat()
4. compare two strings
5. find length of a string
6. convert all lower case character to upper case
7. convert all upper case character to lower case
8. count number of vowels
9. reverse the string
10. exit

Enter your choice: 3

enter first string: apple


enter second string: ball

strings after concatenation: appleball

Options are:
1. show address of each character in a string
2. concatenate two strings without using strcat()
3. concatenate two strings using strcat()
4. compare two strings
5. find length of a string
6. convert all lower case character to upper case
7. convert all upper case character to lower case
8. count number of vowels
9. reverse the string
10. exit

Enter your choice: 10

[end]

ASSIGNMENT- 17 DATE:01.03.2021

Problem Statement:- Given two ordered arrays of integers, write a program to


merge the two-arrays to get an ordered array.

Algorithm:-
Algorithm merge_sum()
Input:- Two sorted integer arrays where the first array is ‘array1’ and the second array is
‘array2’.
Output:- A combined sorted array of integers which were in the two arrays, which were
taken as input.
Steps:-
2)Initialize k=0
9)Initialize i=0 and j=0.
10)While(i<a && j<b)
do
if (array[i]<array2[j])
array3[k]=array1[i]
i++
else
array3[k]=array2[j]
j++
end if
k++
end while
11)if(i>=a) then
while(j<b)
do
array3[k]=array2[j]
j++
k++
end while
end if
12) if(j<=b) then
while(i<a)
do
array3[k]=array1[i]
i++
k++
end while
end if
14) for(i to a+b) do
Print array3[i]
End For
15) End.
Source Code:-
#include<stdio.h>
#include<conio.h>
int main()
{ int array1[50],array2[50],array3[100],a,b,i,j,k=0;
clrscr();
printf(" \n Enter The Size Of Array 1:- ");
scanf("%d",&a);
printf("\n Enter The Size Of Array 2:- ");
scanf("%d",&b);
printf("\n Enter Elements Of Array 1 In A Sorted Manner:- ");
for(i=0;i<a;i++)
{
scanf("%d",&array1[i]);
}
printf("\n Enter Elements Of Array 2 In A Sorted Manner:- ");
for(i=0;i<b;i++)
{
scanf("%d",&array2[i]);
i=0;
j=0
while(i<a&&j<b)
{
if(array1[i]<array2[j])
{
array3[k]=array1[i];
i++;
}
else
{
array3[k]=array2[j];
j++;
}
k++;
}
if(i>=a)
{
while(j<b)
{
array3[k]=array2[j];
j++;
k++;
}
}

if(j>=b)
while(i<a)
{
array3[k]=array1[i];
i++;
k++;
}
}
printf("\nAfter Merging:\t");
for(i=0;i<=a+b;i++)
{
printf("%d\t",array3[i]);
}
return 0;
getch();
}
Output:-
(correction use more data as input)
Enter The Size Of Array 1:- 3
Enter The Size Of Array 2:- 3
Enter Elements Of Array 1 In A Sorted Manner:- 1
2
3
Enter Elements Of Array 2 In A Sorted Manner:- 4
5
6
After Merging: 1 2 3 4 5 6

ASSIGNMENT-18 DATE:01.03.2021

Problem Statement : WAP to display Fibonacci series (i) using recursion, (ii) using
iteration.
Algorithm: Fibonacci series using recursion
Input: num
Output: Fibonacci series upto ‘num’th term
Steps:
1. START
2. Read num[read the number of range]
3. for (i=0 to i<num) do
a. display fib(i)
4. End
Here we have used a procedure called fib(), the body of fib() is,
Procedure fib(int n)
1. if(n<=1)
return n;
2. return fib(n-1) = fib (n-2);
3. End procedure
Source Code:
//Fibonacci Series using Recursion
#include<stdio.h>
#include<stdlib.h>
int fib(int );
int main ()
{
int i,num;
system("@cls||clear");
printf("Enter the number of the range : ");
scanf("%d",&num);
printf("\n");
for(i=0;i<num;i++)
{
printf("%d ", fib(i));
}
printf("\n");
getchar();
return 0;
}
int fib(int n)
{
if (n <= 1)
return n;
return fib(n-1) + fib(n-2);
}
Output:
1
Enter the number of the range : 10

0 1 1 2 3 5 8 13 21 34
2
Enter the number of the range : 15

0 1 1 2 3 5 8 13 21 34 55 89 144 233 377


3
Enter the number of the range : 5

0 1 1 2 3

Algorithm: Fibonacci series using iteration.


Input: n
Output: Fibonacci series upto ‘n’th term
Steps:
1. START
2. Initialise t1=0, t2=1, count=2, display=0
3. Read n
4. Display t1, t2
5. Repeat while(count<n)
a. Display t1 + t2
b. t1 = t2
c. t2 = display
d. ++ count
e. Display display
6. End
Source Code:
#include <stdio.h>
#include<stdlib.h>
int main()
{
int count, n, t1=0, t2=1, display=0;
system("@cls||clear");
printf("Enter number of terms: ");
scanf("%d",&n);
printf("Fibonacci Series: %d %d ", t1, t2);

count=2;
while (count<n)
{
display=t1+t2;
t1=t2;
t2=display;
++count;
printf(" %d ",display);
}
printf("\n");
getchar();
return 0;
}
Output:
1)
Enter number of terms: 5
Fibonacci Series: 0 1 1 2 3
2)
Enter number of terms: 10
Fibonacci Series: 0 1 1 2 3 5 8 13 21 34
3)
Enter number of terms: 20
Fibonacci Series: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584
4181

ASSINGMENT-19 DATE:19.01.2021

Problem Statement :- WAP to calculate Factorial of a number (i) using recursion, (ii)
using iteration.

I. ALGORITHM: (Using recursion)

INPUT : An integer ‘num’.


OUTPUT : Print the value of its factorial by calling a function.

STEPS:

Step 1: Start .
Step 2: declare variable num.
Step 3: Read the integer ‘num’.
Step 4: call function ‘factorial(num)’ to print the factorial of the given number.
Step 5: Stop

Here we have used a procedure by the name of ‘factorial()’ where location of variable(n)
are passed as argument(num). The body of the procedure is as follows:
Procedure factorial()
Steps:-

Step 1: Start
Step 2: if (n==0),
return 1;
return n*factorial(n-1);
Step 3: Stop.

SOURCE CODE :

#include <stdio.h>

int factorial(int n) // function to find factorial of given number using recursion


{
if (n == 0)
return 1;
return n * factorial(n - 1);
}
int main()
{
int num;
printf("Enter a no:");
scanf("%d", &num);
printf("Factorial of %d is %d", num, factorial(num));
return 0;
}

II. ALGORITHM: (Using iteration)

INPUT : An integer ‘num’.


OUTPUT : Print the value of its factorial by calling a function.

STEPS:

Step 1: Start .
Step 2: declare variable num.
Step 3: Read the integer ‘num’.
Step 4: call function ‘factorial(num)’ to print the factorial of the given number.
Step 5: Stop

Here we have used a procedure by the name of ‘factorial()’ where location of variable(n)
are passed as argument(num). The body of the procedure is as follows:
Procedure factorial()
Steps:-

Step 1: Start
Step 2: declare i and initialise f=1.
Step 3: for (i = 2; i <= n; i++)
f *= i;
Step 4: return f.
Step 5: Stop.

SOURCE CODE :

#include <stdio.h>
int factorial(int n) // function to find factorial of given number using iteration
{
int f = 1, i;
for (i = 2; i <= n; i++)
f *= i;
return f;
}

int main()
{
int num;
printf("Enter a no:");
scanf("%d", &num);
printf("Factorial of %d is %d", num, factorial(num));
return 0;
}

OUTPUT :

1)
Enter a no:5
Factorial of 5 is 120
2)
Enter a no:10
Factorial of 10 is 3628800
3)
Enter a no:4
Factorial of 4 is 24

ASSINGMENT-20 DATE:19.01.2021

Problem Statement :- WAP to calculate GCD of two numbers (i) with recursion (ii)
without recursion.

I. ALGORITHM: (Using recursion)

INPUT : Integers a and b.


OUTPUT : Print the value of its gcd by calling a function.

STEPS:

Step 1: Start .
Step 2: declare variable a and b.
Step 3: Read the integers a and b.
Step 4: call function ‘gcd(num)’ to print the gcd of the given numbers.
Step 5: Stop

Here we have used a procedure by the name of ‘gcd()’ where location of variable(a,b) are
passed as arguments(a,b). The body of the procedure is as follows:
Procedure gcd()
Steps:-

Step 1: Start
Step 2: if (a == 0)
return b;
Step 3:if (b == 0)
return a;
Step 4: if (a == b) // base case
return a;
Step 5: if (a > b) // a is greater
return gcd(a-b, b);
else
return gcd(a, b-a);
Step 6: Stop.

SOURCE CODE :

// C program to find GCD of two numbers using recursion

#include <stdio.h>
int gcd(int a, int b) // Recursive function to return gcd of a and b
{
// Everything divides 0
if (a == 0)
return b;
if (b == 0)
return a;

// base case
if (a == b)
return a;

// a is greater
if (a > b)
return gcd(a-b, b);
return gcd(a, b-a);
}

int main()
{
int a,b;
printf("Enter first number:");
scanf("%d", &a);
printf("Enter second number:");
scanf("%d", &b);
printf("GCD of %d and %d is %d ", a, b, gcd(a, b));
return 0;
}

II. ALGORITHM: (Using iteration)

INPUT : Integers a and b.


OUTPUT : Print the value of it’s gcd by calling a function.

STEPS:

Step 1: Start .
Step 2: declare variable a and b.
Step 3: Read the integers a and b.
Step 4: call function ‘gcd(num)’ to print the gcd of the given numbers.
Step 5: Stop

Here we have used a procedure by the name of ‘gcd()’ where location of variable(a,b) are
passed as arguments(a,b). The body of the procedure is as follows:
Procedure gcd()
Steps:-

Step 1: Start
Step 2: Declare variables i and gcd.
Step 3: for(i=1; i <= a && i <= b; ++i)
Step 4: if(a%i==0 && b%i==0)
gcd = i;
Step 5: return gcd
Step 6: Stop.

SOURCE CODE :

// C program to find GCD of two numbers using iteration

#include <stdio.h>
int gcd(int a, int b) // iterative function to return gcd of a and b
{
int i,gcd;
for(i=1; i <= a && i <= b; ++i)
{
// Checks if i is factor of both integers
if(a%i==0 && b%i==0)
gcd = i;
}
return gcd;
}

int main()
{
int a,b;
printf("Enter first number:");
scanf("%d", &a);
printf("Enter second number:");
scanf("%d", &b);
printf("GCD of %d and %d is %d ", a, b, gcd(a, b));
return 0;
}

OUTPUT :

1)
Enter first number:98
Enter second number:56
GCD of 98 and 56 is 14
2)
Enter first number:5
Enter second number:7
GCD of 5 and 7 is 1
3)
Enter first number:9
Enter second number:3
GCD of 5 and 7 is 3
ASSINGMENT -21 DATE:27.01.2021

PROBLEM: Write a menu-driven program to perform following Matrix operations


(2-D array implementation): a) Sum b) Difference c) Product d) Transpose

ALGORITHM: Menu driven matrix

INPUT : mat1[10][10],mat2[10][10],r1,c1,r2,c2,option,check,t

OUTPUT : summation of two matrices,difference of two matrices,


multiplication of two matrices,transpose of two matrices

STEPS:
1. [Initialize]
set check=1
2. Repeat steps a- h untill ‘true’
a. Repeat till ‘true’
i. if check == 1 then
[calling procedure to take input of matrix 1]
matInput(mat1,&r1,&c1)
[calling procedure to take input of matrix 1]
matInput(mat2,&r2,&c2)
end loop
else
if check==0 then
end loop
else
display error massage
read check
End if
End if
End loop
b. [calling procedure to display first matrix]
matprint(mat1,&r1,&c1)
c. [calling procedure to display second matrix]
matprint(mat2,&r2,&c2)
d. Display Menu [1 for sum, 2 for subtract, 3 for multiply, 4 for Transpose,
5 for exit]
e. Read option
f. switch(option)
case 1: if(r1!=r2) or (c1!=c2) then
Display Error message
End switch
End if
[calling a procedure to add two matrix]
matsum(mat1,mat2,sum,&r1,&c1)
[calling a procedure to display sum]
Matprint(sum,&r1,&c1)
End switch
case 2: if(r1=r2) or (c1!=c2) then
Display Error message
End switch
End if
[calling a procedure to subtract two matrix]
matdifference(mat1,mat2,difference,&r1,&c1)
[calling a procedure to display result matrix]
matprint(difference,&r1,&c1)
End switch
case 3: if (c1!=c2) then
Display Error message
End switch
End if
[calling a procedure to multiply two matrix]
matsum(mat1,mat2,multiply,&r1,&c1,&c2)
[calling a procedure to display sum]
matprint(multiply,&r1,&c2)
End switch
case 4: Display Transpose Menu[1 to transpose first, 2 to transpose
second and 3 to transpose both]
Read option
Switch(t)
case 1:
[calling procedure to transpose first matrix]
matTranspose(mat1,transpose,&r1,&c1)
[calling procedure to print result matrix]
matprint(transpose,&c1,&r1)
End switch
case 2:
[calling procedure to transpose second matrix]
matTranspose(mat2,transpose,&r2,&c2)
[calling procedure to print result matrix]
matprint(transpose,&c2,&r2)
End switch
case 3:
[calling procedure to transpose first matrix]
matTranspose(mat1,transpose,&r1,&c1)
[calling procedure to print result matrix]
matprint(transpose,&c1,&r1)
[calling procedure to transpose second matrix]
matTranspose(mat2,transpose,&r2,&c2)
[calling procedure to print result matrix]
matprint(transpose,&c2,&r2)
End switch
case 5:
Exit
Default:
Display error message
End switch
g. [Read check to use old matrices or insert new matrices]
Read check
End loop
3. End

Here we have used several procedures. The bodies of procedure are given below,

Procedure matInput(int matrix[][10], int *row, int *col)


1. [Read row number and column number of matrix]
Read row, col.
2. Repeat i=0 till i<*row
i. repeat j=0 till j<*col
Read matrix [i][j]
++j
End loop
ii. ++i
iii. End loop
3. End procedure

Procedure matPrint(int matrix[][10], int *row, int *col)


1. Repeat i=0 i<*row
i. Repeat j=0 till j<*col
Display matrix[i][j]
++j
End loop
2. End procedure

Procedure matSum(int matrix1[][10], int matrix2[][10], int sum[][10], int *row,


int *col)
1. Repeat i=0 till i<*row
i. Repeat i=0 till j<*col
sum[i][j]=matrix1[i][j] + matrix2[i][j]
++j
End loop
ii. ++i
iii. End loop
2. End loop
3.End procedure

Procedure matdifference(int matrix1[][10], int matrix2[][10], int difference[][10],


int *row, int *col)
1. Repeat i=0 till i<*row
i. Repeat j=0 till j<*col
difference[i][j]=matrix1[i][j] - matrix2[i][j]
++j
End loop
ii. ++i
iii. End loop
2. End loop
3.End procedure
Procedure matMultiply(int matrix1[][10], int matrix2[][10], int multiply[][10],
int *row1, int *col1, int *col2)
1. Repeat i=0 till i<*row1
i. Repeat j=0 till j<*col2
a. multiply[i][j]=0
b. Repeat k=0 till k<*col1
multiply[i][j]+=matrix1[i][k]*matrix2[k][j]
++k
End loop
c. ++j
d. End loop
ii. ++i
iii. End loop
2. End procedure
Procedure matTranspose(int matrix[][10], int *row, int *col)
1. Repeat i=0 till i<*row
a. Repeat j=0 till j<*col
transpose[j][i]=matrix[i][j]
++j
End loop
b. ++i
c. End loop
2. End procedure

SOURCE CODE:
#include<stdio.h>
#include<stdlib.h>

//User defined Functions


void matInput(int matrix[][10],int *,int *);
void matPrint(int matrix[][10],int *,int *);
void matSum(int matrix1[][10],int matrix2[][10],int sum[][10],int *,int *);
void matDifference(int matrix1[][10],int matrix2[][10],int difference[][10],int *,int *);
void matMultiply(int matrix1[][10],int matrix2[][10],int multiply[][10],int *,int *,int *,int
*);
void matTranspose(int matrix[][10],int transpose[][10],int *,int *);
void usr_clrscr();

//Main funtion
int main()
{
int mat1[10][10],mat2[10][10],sum[10][10],difference[10][10],multiply[10]
[10],transpose[10][10],r1,c1,r2,c2,option,check=1,t; //Variable declaration
do
{
while(1)
{
if(check==1)
{
usr_clrscr();
printf("Enter first matrix: "); //Inserting the first matrix
matInput(mat1,&r1,&c1);
printf("\nEnter second matrix: "); //Inserting the second matrix
matInput(mat2,&r2,&c2);
break;
}
else
{
if(check==0)
break;
else
{
printf("\nError(1)!!!Read options carefully");
printf("\nEnter 1 to insert new matrices.\nEnter 0 to operate old ones");
printf("\nChoice: ");
scanf("%d",&check);
}
}
}
usr_clrscr();
printf("\nFirst Matrix is \n");
matPrint(mat1,&r1,&c1);
printf("\nSecond matrix is \n");
matPrint(mat2,&r2,&c2);
printf("\n\t\t\t\t\tMATRIX__MENU"); //Menu
printf("\n1.Enter 1 to print the summation of two matrices.\n2.Enter 2 to
print the difference of two matrics.\n3.Enter 3 to print the multiplication of two
matrics.\n4.Enter 4 to print the transpose of matrix.\n5.Enter 5 to Exit.");
printf("\nMy choice is : ");
scanf("%d",&option);
switch(option)
{
case 1:
if((r1!=r2)||(c1!=c2))
{
printf("\nError(2)!!!Cannot perform
summation.\n#NOTE:Row number and column number of both matrices have to same for
summantion.");
break;
}
matSum(mat1,mat2,sum,&r1,&c2);
printf("\nSummation of given matrices is :\n");
matPrint(sum,&r1,&c1);
break;
case 2:
if((r1!=r2)||(c1!=c2))
{
printf("\nError(3)!!!Cannot perform
difference.\n#NOTE:Row number and column number of both matrices have to same for
difference.");
break;
}
matDifference(mat1,mat2,difference,&r1,&c1);
printf("\nDifference of given matrices is : \n");
matPrint(difference,&r1,&c1);
break;
case 3:
if(c1!=r2)
{
printf("\nError(4)!!!Cannot perform
multiplication.\n#NOTE:Column number of first matrix and row number of matrix have
to same for multiplication.");
break;
}
matMultiply(mat1,mat2,multiply,&r1,&c1,&r2,&c2);
printf("\nMultiplication of given matrices is : \n");
matPrint(multiply,&r1,&c2);
break;
case 4:
printf("\nEnter 1 to transpose the first matrix.\nEnter 2 to
transpose the second matrix.\nEnter 3 to transpose both matrics.");
printf("\nMy choice is : ");
scanf("%d",&t);
switch(t)
{
case 1:
matTranspose(mat1,transpose,&r1,&c1);
printf("Transpose of first matrix is :\n");
matPrint(transpose,&c1,&r1);
break;
case 2:
matTranspose(mat2,transpose,&r2,&c2);
printf("Transpose of first matrix is :\n");
matPrint(transpose,&c2,&r2);
break;
case 3:
matTranspose(mat1,transpose,&r1,&c1);
printf("Transpose of first matrix is :\n");
matPrint(transpose,&c1,&r1);
matTranspose(mat2,transpose,&r2,&c2);
printf("Transpose of second matrix is :\n");
matPrint(transpose,&c2,&r2);
break;
default:
printf("Error(5)!!!READ CAREFULLY!");
break;
}
break;
case 5:
exit(0);
default:
printf("Error(6)!!!Read matrix menu again\n");
break;

}
printf("\nDo you want to insert new matrices??");
printf("\nEnter 1 to insert new matrices.\nEnter 0 to operate old ones");
printf("\nChoice: ");
scanf("%d",&check);
}while(1);

return 0;
}
// Function to insert any matrix
void matInput(int matrix[][10],int *row,int *col)
{
int i,j;
printf("\nEnter the number of rows and columns : ");
scanf("%d %d",row,col);
printf("\nEnter the elements of matrix \n");
for(i=0;i<*row;++i)
for(j=0;j<*col;++j)
{
printf("\nElement (%d,%d): ",i+1,j+1);
scanf("%d",&matrix[i][j]);
}
}
//Function to display a matrix
void matPrint(int matrix[][10],int *row,int *col)
{
int i,j;
for(i=0;i<*row;++i)
{
for(j=0;j<*col;++j)
{
printf("%d\t",matrix[i][j]);
}
printf("\n");
}
}
//User defined clear screen function(for liinux and windows)
void usr_clrscr()
{
system("@cls||clear");
}
//Function to do sum two matrices
void matSum(int matrix1[][10],int matrix2[][10],int sum[][10],int *row,int *col)
{
int i,j;
for(i=0;i<*row;++i)
for(j=0;j<*col;++j)
{
sum[i][j]=matrix1[i][j]+matrix2[i][j];
}
}
//Function to do difference of two matrics
void matDifference(int matrix1[][10],int matrix2[][10],int difference[][10],int *row,int
*col)
{
int i,j;
for(i=0;i<*row;++i)
for(j=0;j<*col;++j)
{
difference[i][j]=matrix1[i][j]-matrix2[i][j];
}
}
//Function to do multiplication of two matrices
void matMultiply(int matrix1[][10],int matrix2[][10],int multiply[][10],int *row1,int
*col1,int *row2,int *col2){
int i,j,k;
for(i=0;i<*row1;++i)
{
for(j=0;j<*col2;++j)
{
multiply[i][j]=0;
for(k=0;k<*col1;k++)
{
multiply[i][j]+=matrix1[i][k]*matrix2[k][j];
}
}
}
}
//Function to transpose a matrix
void matTranspose(int matrix[][10],int transpose[][10],int *row,int *col)
{
int i,j;
for(i=0;i<*row;++i)
for(j=0;j<*col;++j)
transpose[j][i]=matrix[i][j];
}

OUTPUT:
1)------------------------------------------------------------------------------------------
Enter first matrix:
Enter the number of rows and columns : 3 3

Enter the elements of matrix

Element (1,1): 1

Element (1,2): 2

Element (1,3): 3

Element (2,1): 4

Element (2,2): 5

Element (2,3): 6

Element (3,1): 7

Element (3,2): 8

Element (3,3): 9

Enter second matrix:


Enter the number of rows and columns : 3 3

Enter the elements of matrix

Element (1,1): 1

Element (1,2): 2

Element (1,3): 3

Element (2,1): 4

Element (2,2): 5

Element (2,3): 6

Element (3,1): 7

Element (3,2): 8

Element (3,3): 9
First Matrix is
1 2 3
4 5 6
7 8 9

Second matrix is
1 2 3
4 5 6
7 8 9

MATRIX__MENU
1.Enter 1 to print the summation of two matrices.
2.Enter 2 to print the difference of two matrics.
3.Enter 3 to print the multiplication of two matrics.
4.Enter 4 to print the transpose of matrix.
5.Enter 5 to Exit.
My choice is : 1

Summation of given matrices is :


2 4 6
8 10 12
14 16 18

Do you want to insert new matrices??


Enter 1 to insert new matrices.
Enter 0 to operate old ones
Choice: 0

2)-------------------------------------------------------------------------------------------

First Matrix is
1 2 3
4 5 6
7 8 9

Second matrix is
1 2 3
4 5 6
7 8 9

MATRIX__MENU
1.Enter 1 to print the summation of two matrices.
2.Enter 2 to print the difference of two matrics.
3.Enter 3 to print the multiplication of two matrics.
4.Enter 4 to print the transpose of matrix.
5.Enter 5 to Exit.
My choice is : 3
Multiplication of given matrices is :
30 36 42
66 81 96
102 126 150

Do you want to insert new matrices??


Enter 1 to insert new matrices.
Enter 0 to operate old ones
Choice: 0

3)--------------------------------------------------------------------------------------------

First Matrix is
1 2 3
4 5 6
7 8 9

Second matrix is
1 2 3
4 5 6
7 8 9

MATRIX__MENU
1.Enter 1 to print the summation of two matrices.
2.Enter 2 to print the difference of two matrics.
3.Enter 3 to print the multiplication of two matrics.
4.Enter 4 to print the transpose of matrix.
5.Enter 5 to Exit.
My choice is : 4

Enter 1 to transpose the first matrix.


Enter 2 to transpose the second matrix.
Enter 3 to transpose both matrics.
My choice is : 3
Transpose of first matrix is :
1 4 7
2 5 8
3 6 9
Transpose of second matrix is :
1 4 7
2 5 8
3 6 9

Do you want to insert new matrices??


Enter 1 to insert new matrices.
Enter 0 to operate old ones
Choice: 0

ASSIGNMENT: 22 DATE:27.01.2021

Problem Statement : . Copy the contents of one text file to another file, after
removing all whitespaces.

Algorithm :- copy_text_file_removing_whitespaces.
Input:- Take texts from a text file as each character.
Output:- Copy text in an another text file as each character without any spaces.
Steps :-
Step 1: Start
Step 2: Open existed FILE1.txt file using fopen in read mode and f1 file type
pointer pointing this file.[f1=fopen(“FILE1.txt”,”r”)]
Step 3: Open existed FILE1.txt file using fopen in write mode and f1 file type
pointer pointing this file.[f2=fopen(“FILE2.txt”,”w”)]
Step 4: If f1==NULL then
Print “NO SUCH A FILE EXIST”
Exit the program
Step 5: If f2==NULL then
Print “NO MEMORY TO CREATE A FILE”
Exit the program
Step 6: Else
6.1. Repeat loop
a. Assign each character of f1 using file function fgetc in ch.
[ch=fgetc(f1)]
b. If ch!=’ ‘ and ch!=EOF then
Insert character ch in f2 [fputc(ch,f2)]
Until ch!=EOF
6.2. End loop
Step 7: Stop

Source Code :-
#include<stdio.h>
void main()
{
FILE *f1,*f2;
char ch;
f1=fopen("FILE1.txt","r");
f2=fopen("FILE2.txt","w");
if(f1==NULL)
{
printf("\n NO SUCH FILE EXIST\n");
exit(1);
}
if(f2==NULL)
{
printf("\n NO MEMORY TO CREATE A FILE \n");
exit(1);
}
else
{
do
{
ch=fgetc(f1);
if(ch!=' '&&ch!=EOF)
fputc(ch,f2);
}while(ch!=EOF);
}
}
Input / Output :-
1) In FILE1: -
I study bsc computer science honors
In FILE2: -
Istudybsccomputersciencehonors

ASSIGNMENT-23 DATE:27.01.2021

Problem Statement : Write a function that reverses the elements of an array in


place. The function must accept only one pointer value and return void.

Algorithm:

INPUT: taking array elements from user.


OUTPUT: printing elements in ascending and descending order.
Steps for execution
STEP 1: START
STEP 2: take elements of arr
STEP 3: for i = 0; i < 10; ++i:
STEP 3.1 print arr[i]
STEP 4: for i = 9; i >= 0; --i:
STEP 4.1 print arr[i]
STEP 5: STOP

Source code :

#include <stdio.h>
int main()
{
int data[10];

printf("Enter elements: \n");


for (int i = 0; i < 10; ++i)
{
printf(" > ");
scanf("%d", data + i);
}
printf("\n printing elements in ascending order: ");
for (int i = 0; i < 10; ++i)
{
printf("%d, ", *(data + i));
}
printf("\n printing elements in descending order: ");
for (int i = 9; i >= 0; --i)
{
printf("%d, ", *(data + i));
}
return 0;
}

Output:
Enter elements:
>8
>5
>9
>3
>7
>6
>5
>7
>8
>8

printing elements in ascending order: 8, 5, 9, 3, 7, 6, 5, 7, 8, 8,


printing elements in descending order: 8, 8, 7, 5, 6, 7, 3, 9, 5, 8,
ASSIGNMENT-24 DATE:27.01.2021

Problem Statement: Write a program that will read 10 integers from user and store
them in an array. Implement array using pointers. The program will print the array
elements in ascending and descending order.

Algorithm for the program

INPUT: taking array elements from user.


OUTPUT: printing elements of array in reverse.

Steps for execution

STEP 1: START
STEP 2: take elements of arr
STEP 3: reverse(arr)
STEP 4: print arr
STEP 5: STOP

defination of reverse(arr, size)


STEP 1: START
STEP 2: *ptr1 = arr, *ptr2 = arr + size -1
STEP 3.: while ptr1 < ptr2:
3.1 temp = ptr1
3.2 ptr1 = ptr2
3.3 ptr2 = temp
3.4 ptr1++
3.5 ptr2--
STEP 4: END WHILE
STEP 5: return reversed array
STEP 6: STOP

Source code:

#include <stdio.h>
void reverse(int array[], int array_size)
{
int temp;
int *pointer1 = array, *pointer2 = array + array_size - 1;
while (pointer1 < pointer2)
{
temp = *pointer1;
*pointer1 = *pointer2;
*pointer2 = temp;
pointer1++;
pointer2--;
}
}

int main()
{
int array[10];
printf(" enter the 10 elements of array: \n");
for(int x = 0; x < 10; x++){
printf(" > ");
scanf("%d", &array[x]);
}
reverse(array, 10);
printf("\n reversed elements of array: ");
for (int p = 0; p < 10; p++)
{
printf("%d, ", array[p]);
}
return 0;
}
Output
enter the 10 elements of array:
> 6
> 4
> 8
> 2
> 6
> 8
> 5
> 8
> 4
> 8

reversed elements of array: 8, 4, 8, 5, 8, 6, 2, 8, 4, 6,

ASSIGNMENT-25 DATE:02.02.2021

Problem Statement : Add two distances in meter kilometer system using structure.

Algorithm: Distance calculation


Distance is data type defined to store data values named kilometer, meter.
Input: start, stop
Output: Distance between “start” and “stop”
Steps:
1. START
2. Read start.kilometer, start.meter
3. Read stop.kilometer, stop.meter
4. if(stop.kilometer < start.kilometer) then
i. stop.kilometer = stop.kilometer + start.kilometer
ii. start.kilometer = stop.kilometer – start.kilometer
iii. stop.kilometer = stop.kilometer – start.kilometer
iv. End if
5. if(stop.meter < start.meter) then
i. stop.meter = stop.meter + start.meter
ii. start.meter = stop.meter – start.meter
iii. stop.meter = stop.meter – start.meter
iv. End if
6. diff.kilometer = stop.kilometer – start.kilometer
7. diff.meter = stop.meter – start.meter
8. Display diff.kilometer, diff.meter
9.End
Source Code:
#include<stdio.h>
#include<stdlib.h>
typedef struct Distance
{
int kilometer;
int meter;
}dis;
int main()
{
dis start, stop, diff;
system("@cls||clear");
printf("Enter starting distance(km:m): ");
scanf(" %d %d",&start.kilometer,&start.meter);
printf("\nEnter stop distance(km:m) :");
scanf(" %d %d",&stop.kilometer,&stop.meter);
if(stop.kilometer<start.kilometer)
{
stop.kilometer=stop.kilometer+start.kilometer;
start.kilometer=stop.kilometer-start.kilometer;
stop.kilometer=stop.kilometer-start.kilometer;
}
if(stop.meter<start.meter)
{
stop.meter=stop.meter+start.meter;
start.meter=stop.meter-start.meter;
stop.meter=stop.meter-start.meter;
}
diff.kilometer=stop.kilometer-start.kilometer;
diff.meter=stop.meter-start.meter;
printf("\nDistance is %dkm %dm\n",diff.kilometer,diff.meter);
getchar();
return 0;
}
Output:
1)
Enter starting distance(km:m): 5 500

Enter stop distance(km:m) :6 700

Distance is 1km 200m


2)
Enter starting distance(km:m): 5 700

Enter stop distance(km:m) :6 300

Distance is 1km 400m


3)
Enter starting distance(km:m): 7 400

Enter stop distance(km:m) :3 489


Distance is 4km 89m

ASSIGNMENT-26 DATE:02.02.2021

Problem Statement: Add two complex numbers using structures.

Algorithm:-

Algorithm: find_sum_complex()

Input: two number ‘cnum1’ & ‘cnum2’.

Output: Print the sum of ‘cnum1’ & ‘cnum2’.

Steps:-

1) Start
2) Declare a struct complex with typedef as follow:
1) Real.
2) Imaginary.

3)In function int main()

1) Read real and imaginary part of first number as ‘cnum1.real’ &


‘cnum1.imaginary’.

2) Read real and imaginary part of second number as ‘cnum2.real’&


‘cnum2.imaginary’.

4)sum.real = cnum1.real+cnum2.real

5)sum.imaginary = cnum1.imaginary+cnum2.imaginary

6)Print the sum of both the real and imaginary part together.

7)STOP

Source code:

#include<stdio.h>

//declaring structure

typedef struct

float real;

float imaginary;

}complex;

int main(){

//declaring structure variable using complex

complex cnum1,cnum2,sum;

printf("Enter real and imaginary part of first complex number:\n");

scanf("%f%f",&cnum1.real,&cnum1.imaginary);

printf("Enter real and imaginary part of second complex number:\n");

scanf("%f%f",&cnum2.real,&cnum2.imaginary);

sum.real = cnum1.real + cnum2.real;

sum.imaginary = cnum1.imaginary + cnum2.imaginary;

printf("SUM = %0.2f+i%0.2f",sum.real,sum.imaginary);

return 0;
}

Output:

1) Enter real and imaginary part of first complex number:


7
3
Enter real and imaginary part of second complex number:
2
5
SUM = 9.00 + i8.00
2) Enter real and imaginary part of first complex number:
2
5
Enter real and imaginary part of second complex number:
3
7
SUM = 8.00 + i9.00

ASSIGNMENT-27 DATE:02.02.2021

Problem Statement : Calculate the difference between two time periods using
structures.

Algorithm: Time is a data type defined to store three data named seconds, minutes and
hours.
Input: Start, Stop.
Output: Time period between “Start” and “Stop”.
Steps:
1. START
2. Read start.hours, start.minutes, start.seconds
[Read start time period]
3. Read stop.hours, stop.minutes, stop.second
[Read stop time period]
4. if (stop.seconds < start.seconds) then
i. stop.minutes = stop.minutes – 1
ii. stop.seconds += 60
iii. End if
5. if(stop.minutes < start.minutes) then
i. stop.hours = stop.hours – 1
ii. stop.minutes += 60
iii. End if
6. if(stop.hours < start.hours)
i. stop.hours += 24
ii. End if
7. diff.seconds = stop.seconds - start.seconds
8. diff.minutes = stop.minutes – start.minutes
9. diff.hours = stop.hours – start.hours
10. Display diff.hours, diff.minutes, diff.seconds
[Display the difference period]
11. End
Source Code:
#include<stdio.h>
#include<stdlib.h>
typedef struct time
{
int seconds;
int minutes;
int hours;
}Time;
int main()
{
Time start,stop,diff;
printf("Enter start time(HH:MM:DD): ");
scanf("%d %d %d",&start.hours,&start.minutes,&start.seconds);
printf("\nEnter stop time(HH:MM:DD): ");
scanf("%d %d %d",&stop.hours,&stop.minutes,&stop.seconds);
if(stop.seconds<start.seconds)
{
stop.minutes = stop.minutes-1;
stop.seconds += 60;
}
if(stop.minutes<start.minutes)
{
stop.hours= stop.hours-1;
stop.minutes += 60;
}
if(stop.hours<start.hours)
stop.hours+=24;
diff.seconds = stop.seconds - start.seconds;
diff.minutes = stop.minutes - start.minutes;
diff.hours = stop.hours - start.hours;
printf("\nThe time difference is %dH:%dM:
%dS\n",diff.hours,diff.minutes,diff.seconds);
return 0;
}
Output:
1
Enter start time(HH:MM:DD): 03 21 00

Enter stop time(HH:MM:DD): 03 09 50

The time difference is 23H:48M:50S


2.
Enter start time(HH:MM:DD): 09 30 27

Enter stop time(HH:MM:DD): 07 42 27


The time difference is 22H:12M:0S
3.
Enter start time(HH:MM:DD): 03 28 44

Enter stop time(HH:MM:DD): 14 29 23

The time difference is 11H:0M:39S

You might also like