Professional Documents
Culture Documents
ASSIGNMENTS
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
c) product=product * num
d) num=num/10
STEP 4 : END
SOURCE CODE :
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+……
Start
Read n
Set sum=0.0
sum=1/i+sum
STEP 3: sum=1/i+sum
STEP 5: Exit
#include<stdio.h>
int main()
int n;
float sum=0.0,i;
clrscr();
printf("Enter an integer:");
scanf(" %d",&n);
sum=1/i+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]
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
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
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
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.
2)
Enter two number : 15 67
3)
Enter two number : 72 27
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
> > > > > > > > >
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
> > > > > > > > >
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];
}
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
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];
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
--------------------------------
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
Problem Statement: WAP that prints a table indicating the number of occurrences
of each alphabet in the text entered as command line arguments.
2)
./assignment11 "Argho ghosh"
'A'= 1
'r'= 1
'g'= 2
'h'= 3
'o'= 2
' '= 1
's'= 1
3)
./assignment11 "Deblina nandy"
'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]
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
INPUT : a=4,b=5
OUTPUT : after swapping: a=4,b=5
STEPS----
STEP 1 : start
STEP 3 : a)temp=*x
b)*x=*y
c)*y=temp
STEP 5:Exit
SOURCE CODE :
{
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
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.
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 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();
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);
end = count - 1;
rev[i] = '\0';
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++;
}
$ ./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
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
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
[end]
ASSIGNMENT- 17 DATE:01.03.2021
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
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.
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>
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.
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 :
#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;
}
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 :
#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
INPUT : mat1[10][10],mat2[10][10],r1,c1,r2,c2,option,check,t
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,
SOURCE CODE:
#include<stdio.h>
#include<stdlib.h>
//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
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
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
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
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
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
Algorithm:
Source code :
#include <stdio.h>
int main()
{
int data[10];
Output:
Enter elements:
>8
>5
>9
>3
>7
>6
>5
>7
>8
>8
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.
STEP 1: START
STEP 2: take elements of arr
STEP 3: reverse(arr)
STEP 4: print arr
STEP 5: 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
ASSIGNMENT-25 DATE:02.02.2021
Problem Statement : Add two distances in meter kilometer system using structure.
ASSIGNMENT-26 DATE:02.02.2021
Algorithm:-
Algorithm: find_sum_complex()
Steps:-
1) Start
2) Declare a struct complex with typedef as follow:
1) Real.
2) 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(){
complex cnum1,cnum2,sum;
scanf("%f%f",&cnum1.real,&cnum1.imaginary);
scanf("%f%f",&cnum2.real,&cnum2.imaginary);
printf("SUM = %0.2f+i%0.2f",sum.real,sum.imaginary);
return 0;
}
Output:
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