You are on page 1of 150

BCSE102P -

Structured and Object-Oriented


Programming

School of Computer Science and Engineering (SCOPE)


Vellore Institute of Technology
Vellore.
DIGITAL ASSIGNMENT 1

PROGRAM-1 21BKT0077

Write a program in C to store elements in an array and print it.

Test Data :

Input 10 elements in the array :

element - 0 : 1
element - 1 : 1
element - 2 : 2
.......
Expected Output :

Elements in array are: 1 1 2 3 4 5 6 7 8 9

[Pick the date] Page 2


PROGRAM:
#include <stdio.h>

void main()

int arr[10];

int i;

printf("\n\nRead and Print elements of an array:\n");

printf("Input 10 elements in the array :\n");

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

printf("element - %d : ",i);

scanf("%d", &arr[i]);

printf("\nElements in array are: ");

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

[Pick the date] Page 3


{

printf("%d ", arr[i]);

printf("\n");

INPUT & OUTPUT:

[Pick the date] Page 4


RESULT:

Therefore, this is the program in C to store elements in an array


and print it.

[Pick the date] Page 5


PROGRAM-2 21BKT0077

Write a program in C to read n number of values in an array and


display it in reverse order.

Test Data :

Input the number of elements to store in the array :3


Input 3 number of elements in the array :

element - 0 : 2
element - 1 : 5
element - 2 : 7

Expected Output :

The values store into the array are : 2 5 7


The values store into the array in reverse are : 7 5 2
[Pick the date] Page 2
PROGRAM:
#include <stdio.h>

void main()

int i,n,a[100];

printf("\n\nRead n number of values in an array and display it in


reverse order:\n");

printf("Input the number of elements to store in the array :");

scanf("%d",&n);

printf("Input %d number of elements in the array :\n",n);

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

printf("element - %d : ",i);

scanf("%d",&a[i]);

}
[Pick the date] Page 3
printf("\nThe values store into the array are : \n");

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

printf("% 5d",a[i]);

printf("\n\nThe values store into the array in reverse are :\n");

for(i=n-1;i>=0;i--)

printf("% 5d",a[i]);

printf("\n\n");

[Pick the date] Page 4


INPUT & OUTPUT:

[Pick the date] Page 5


RESULT:

Therefore, this is the program in C to read n number of values in

an array and display it in reverse order.

[Pick the date] Page 6


PROGRAM-3 21BKT0077

Write a program in C to find the sum of all elements of the array.

Test Data :

Input the number of elements to store in the array :3


Input 3 number of elements in the array :

element - 0 : 2
element - 1 : 5
element - 2 : 8

Expected Output :

Sum of all elements stored in the array is : 15

[Pick the date] Page 2


PROGRAM:

#include <stdio.h>

void main()

int a[100];

int i, n, sum=0;

printf("\n\nFind sum of all elements of array:\n");

printf("Input the number of elements to be stored in the array :");

scanf("%d",&n);

printf("Input %d elements in the array :\n",n);

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

[Pick the date] Page 3


printf("element - %d : ",i);

scanf("%d",&a[i]);

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

sum += a[i];

printf("Sum of all elements stored in the array is : %d\n\n", sum);

[Pick the date] Page 4


INPUT & OUTPUT:

[Pick the date] Page 5


RESULT:

Therefore, this is the program in C to find the sum of all elements


of the array.

[Pick the date] Page 6


PROGRAM-4 21BKT0077

Write a program in C to copy the elements of one array into


another array.

Test Data :

Input the number of elements to store in the array :3


Input 3 number of elements in the array :

element - 0 : 15
element - 1 : 10
element - 2 : 12

Expected Output :

The elements stored in the first array are : 15 10 12


The elements copied into the second array are : 15 10 12
[Pick the date] Page 2
PROGRAM:
#include <stdio.h>

void main()
{
int arr1[100], arr2[100];
int i, n;

printf("\n\nCopy the elements one array into another array :\n");

printf("Input the number of elements to be stored in the array :");


scanf("%d",&n);

printf("Input %d elements in the array :\n",n);


for(i=0;i<n;i++)
{
printf("element - %d : ",i);
scanf("%d",&arr1[i]);
}
/* Copy elements of first array into second array.*/
[Pick the date] Page 3
for(i=0; i<n; i++)
{
arr2[i] = arr1[i];
}

/* Prints the elements of first array */


printf("\nThe elements stored in the first array are :\n");
for(i=0; i<n; i++)
{
printf("% 5d", arr1[i]);
}

/* Prints the elements copied into the second array. */


printf("\n\nThe elements copied into the second array are :\n");
for(i=0; i<n; i++)
{
printf("% 5d", arr2[i]);
}
printf("\n\n");
}

[Pick the date] Page 4


INPUT & OUTPUT:

[Pick the date] Page 5


RESULT:

Therefore, this is the program in C to copy the elements of one


array into another array.

[Pick the date] Page 6


PROGRAM-5 21BKT0077

Write a program in C to count a total number of duplicate


elements in an array.

Test Data :

Input the number of elements to store in the array :3


Input 3 number of elements in the array :

element - 0 : 5
element - 1 : 1
element - 2 : 1

Expected Output :

Total number of duplicate elements found in the array is : 1

[Pick the date] Page 2


PROGRAM:
#include <stdio.h>

void main()
{
int arr1[100];
int arr2[100];
int arr3[100];
int n,mm=1,ctr=0;
int i, j;

printf("\n\nCount total number of duplicate elements in an


array:\n");

printf("Input the number of elements to be stored in the array :");


scanf("%d",&n);

printf("Input %d elements in the array :\n",n);


for(i=0;i<n;i++)
{

[Pick the date] Page 3


printf("element - %d : ",i);
scanf("%d",&arr1[i]);
}
for(i=0;i<n; i++)
{
arr2[i]=arr1[i];
arr3[i]=0;
}
for(i=0;i<n; i++)
{
for(j=0;j<n;j++)
{
if(arr1[i]==arr2[j])
{
arr3[j]=mm;
mm++;
}
}
mm=1;
}
for(i=0; i<n; i++)

[Pick the date] Page 4


{
if(arr3[i]==2){ctr++;}
}
printf("The total number of duplicate elements found in the array is:
%d \n", ctr);

printf("\n\n");
}

[Pick the date] Page 5


INPUT & OUTPUT:

[Pick the date] Page 6


RESULT:

Therefore, this is the program in C to count a total number of


duplicate elements in an array.

[Pick the date] Page 7


PROGRAM-6 21BKT0077

Write a program in C to print all unique elements in an array.

Test Data :

Input the number of elements to store in the array :4


Input 3 number of elements in the array :

element - 0 : 3
element - 1 : 2
element - 2 : 2
element – 3 : 5

Expected Output :

The unique elements found in the array are:


35
[Pick the date] Page 2
PROGRAM:
#include <stdio.h>
int main()
{
int arr1[100], n,ctr=0;
int i, j, k;
printf("\n\nPrint all unique elements of an array:\n");
printf("Input the number of elements to be stored in the array: ");
scanf("%d",&n);
printf("Input %d elements in the array :\n",n);
for(i=0;i<n;i++)
{
printf("element - %d : ",i);
scanf("%d",&arr1[i]);
}
printf("\nThe unique elements found in the array are: \n");
for(i=0; i<n; i++)
{
ctr=0;
for(j=0,k=n; j<k+1; j++)
{
[Pick the date] Page 3
/*Increment the counter when the seaarch value is duplicate.*/
if (i!=j)
{
if(arr1[i]==arr1[j])
{
ctr++;
}
}
}
if(ctr==0)
{
printf("%d ",arr1[i]);
}
}
printf("\n\n");
}

[Pick the date] Page 4


INPUT & OUTPUT:

[Pick the date] Page 5


RESULT:

Therefore, this is the program in C to print all unique elements in


an array.

[Pick the date] Page 6


PROGRAM-7 21BKT0077

Write a program in C to count the frequency of each element of

an array.

Test Data :

Input the number of elements to store in the array : 3

Input 3 number of elements in the array :

element - 0 : 25

element - 1 : 12

element - 2 : 43

element – 3 : 5

Expected Output :

The frequency of all elements of an array :

25 occurs 1 times

12 occurs 1 times

43 occurs 1 times
[Pick the date] Page 2
PROGRAM:

#include <stdio.h>

void main()
{
int arr1[100], fr1[100];
int n, i, j, ctr;

printf("\n\nCount frequency of each element of an array:\n");

printf("Input the number of elements to be stored in the array :");


scanf("%d",&n);

printf("Input %d elements in the array :\n",n);


for(i=0;i<n;i++)
{
printf("element - %d : ",i);
scanf("%d",&arr1[i]);
fr1[i] = -1;

[Pick the date] Page 3


}
for(i=0; i<n; i++)
{
ctr = 1;
for(j=i+1; j<n; j++)
{
if(arr1[i]==arr1[j])
{
ctr++;
fr1[j] = 0;
}
}

if(fr1[i]!=0)
{
fr1[i] = ctr;
}
}
printf("\nThe frequency of all elements of array : \n");
for(i=0; i<n; i++)
{

[Pick the date] Page 4


if(fr1[i]!=0)
{
printf("%d occurs %d times\n", arr1[i], fr1[i]);
}
}
}

[Pick the date] Page 5


INPUT & OUTPUT:

[Pick the date] Page 6


RESULT:

Therefore, this is the program in C to count the frequency of each

element of an array.

[Pick the date] Page 7


PROGRAM-8 21BKT0077

Write a program in C to separate odd and even integers in separate arrays.

Test Data :

Input the number of elements to store in the array : 5

Input 5 number of elements in the array :

element - 0 : 25

element - 1 : 47

element - 2 : 42

element – 3 : 56

element – 4 : 32

Expected Output :

The Even elements are :

42 56 32

The Odd elements are :

25 47
[Pick the date] Page 2
PROGRAM:

#include <stdio.h>

void main()
{
int arr1[10], arr2[10], arr3[10];
int i,j=0,k=0,n;

printf("\n\nSeparate odd and even integers in separate arrays:\n");

printf("Input the number of elements to be stored in the array :");


scanf("%d",&n);

printf("Input %d elements in the array :\n",n);


for(i=0;i<n;i++)
{
printf("element - %d : ",i);
scanf("%d",&arr1[i]);
}

[Pick the date] Page 3


for(i=0;i<n;i++)
{
if (arr1[i]%2 == 0)
{
arr2[j] = arr1[i];
j++;
}
else
{
arr3[k] = arr1[i];
k++;
}
}

printf("\nThe Even elements are : \n");


for(i=0;i<j;i++)
{
printf("%d ",arr2[i]);
}

[Pick the date] Page 4


printf("\nThe Odd elements are :\n");
for(i=0;i<k;i++)
{
printf("%d ", arr3[i]);
}
printf("\n\n");
}

[Pick the date] Page 5


INPUT & OUTPUT:

[Pick the date] Page 6


RESULT:

Therefore, this is the program in C to separate odd and even


integers in separate arrays.

[Pick the date] Page 7


PROGRAM-9 21BKT0077

Write a program in C to sort elements of array in ascending order.

Test Data :

Input the number of elements to store in the array : 5

Input 5 number of elements in the array :

element - 0 : 2

element - 1 : 7

element - 2 : 4

element – 3 : 5

element – 4 : 9

Expected Output :

Elements of array in sorted ascending order:

24579

[Pick the date] Page 2


PROGRAM:

#include <stdio.h>

void main()
{
int arr1[100];
int n, i, j, tmp;

printf("\n\nsort elements of array in ascending order :\n ");

printf("Input the size of array : ");


scanf("%d", &n);

printf("Input %d elements in the array :\n",n);


for(i=0;i<n;i++)
{
printf("element - %d : ",i);
scanf("%d",&arr1[i]);
}

[Pick the date] Page 3


for(i=0; i<n; i++)
{
for(j=i+1; j<n; j++)
{
if(arr1[j] <arr1[i])
{
tmp = arr1[i];
arr1[i] = arr1[j];
arr1[j] = tmp;
}
}
}
printf("\nElements of array in sorted ascending order:\n");
for(i=0; i<n; i++)
{
printf("%d ", arr1[i]);
}
printf("\n\n");
}

[Pick the date] Page 4


INPUT & OUTPUT:

[Pick the date] Page 5


RESULT:

Therefore, this is the program in C to sort elements of array in


ascending order.

[Pick the date] Page 6


PROGRAM-10 21BKT0077

Write a program in C to find the second largest element in an array.

Test Data :

Input the number of elements to store in the array : 5

Input 5 number of elements in the array :

element - 0 : 2

element - 1 : 9

element - 2 : 1

element – 3 : 4

element – 4 : 6

Expected Output :

The Second largest element in the array is : 6

[Pick the date] Page 2


PROGRAM:

#include <stdio.h>

void main(){
int arr1[50],n,i,j=0,lrg,lrg2nd;

printf("\n\nFind the second largest element in an array :\n");

printf("Input the size of array : ");


scanf("%d", &n);
/* Stored values into the array*/
printf("Input %d elements in the array :\n",n);
for(i=0;i<n;i++)
{
printf("element - %d : ",i);
scanf("%d",&arr1[i]);
}
/* find location of the largest element in the array */
// lrg=arr1[0];
lrg=0;
for(i=0;i<n;i++)
{
[Pick the date] Page 3
if(lrg<arr1[i])
{
lrg=arr1[i];
j = i;
}
}

/* ignore the largest element and find the 2nd largest element in the
array */
lrg2nd=0;
for(i=0;i<n;i++)
{
if(i==j)
{
i++; /* ignoring the largest element */
i--;
}
else
{
if(lrg2nd<arr1[i])
{
lrg2nd=arr1[i];
}

[Pick the date] Page 4


}
}

printf("The Second largest element in the array is : %d \n\n", lrg2nd);


}

[Pick the date] Page 5


INPUT & OUTPUT:

[Pick the date] Page 6


RESULT:

Therefore, this is the program in C to find the second largest


element in an array.

[Pick the date] Page 7


PROGRAM-11 21BKT0077

Write a program in C for subtraction of two Matrices.

Test Data :
Input the size of the square matrix (less than 5): 2

Input elements in the first matrix :

element - [0],[0] : 5

element - [0],[1] : 6

element - [1],[0] : 7

element - [1],[1] : 8

Input elements in the second matrix :

element - [0],[0] : 1

element - [0],[1] : 2

element - [1],[0] : 3

element - [1],[1] : 4

Expected Output :

The First matrix is :

56

78

The Second matrix is :

[Pick the date] Page 2


12

34

The Subtraction of two matrix is :

44

44

PROGRAM:

#include <stdio.h>

void main()
{
int arr1[50][50],brr1[50][50],crr1[50][50],i,j,n;

printf("\n\nSubtraction of two Matrices :\n");


printf("Input the size of the square matrix (less than 5): ");
scanf("%d", &n);

/* Stored values into the array*/


printf("Input elements in the first matrix :\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)

[Pick the date] Page 3


{
printf("element - [%d],[%d] : ",i,j);
scanf("%d",&arr1[i][j]);
}
}

printf("Input elements in the second matrix :\n");


for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
printf("element - [%d],[%d] : ",i,j);
scanf("%d",&brr1[i][j]);
}
}
printf("\nThe First matrix is :\n");
for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<n;j++)
printf("%d\t",arr1[i][j]);
}

[Pick the date] Page 4


printf("\nThe Second matrix is :\n");
for(i=0;i<n;i++)
{
printf("\n");
for(j=0;j<n;j++)
printf("%d\t",brr1[i][j]);
}
/* calculate the subtraction of the matrix */
for(i=0;i<n;i++)
for(j=0;j<n;j++)
crr1[i][j]=arr1[i][j]-brr1[i][j];
printf("\nThe Subtraction of two matrix is : \n");
for(i=0;i<n;i++){
printf("\n");
for(j=0;j<n;j++)
printf("%d\t",crr1[i][j]);
}
printf("\n\n");
}

[Pick the date] Page 5


INPUT & OUTPUT:

[Pick the date] Page 6


RESULT:

Therefore, this is the program in C for subtraction of two Matrices.

[Pick the date] Page 7


PROGRAM-12 21BKT0077

Write a program in C for multiplication of two square Matrices.

Test Data :
Input the rows and columns of first matrix : 2 2

Input the rows and columns of second matrix : 2 2

Input elements in the first matrix :

element - [0],[0] : 1

element - [0],[1] : 2

element - [1],[0] : 3

element - [1],[1] : 4

Input elements in the second matrix :

element - [0],[0] : 5

element - [0],[1] : 6

element - [1],[0] : 7

element - [1],[1] : 8

Expected Output :

The First matrix is :

12

34

[Pick the date] Page 2


The Second matrix is :

56

78

The Subtraction of two matrix is :

19 22

43 50

PROGRAM:

#include <stdio.h>

void main()
{
int arr1[50][50],brr1[50][50],crr1[50][50],i,j,k,r1,c1,r2,c2,sum=0;

printf("\n\nMultiplication of two Matrices :\n");

printf("\nInput the rows and columns of first matrix : ");


scanf("%d %d",&r1,&c1);
printf("\nInput the rows and columns of second matrix : ");
scanf("%d %d",&r2,&c2);
if(c1!=r2){
printf("Mutiplication of Matrix is not possible.");
[Pick the date] Page 3
printf("\nColumn of first matrix and row of second matrix must be
same.");
}
else
{
printf("Input elements in the first matrix :\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
printf("element - [%d],[%d] : ",i,j);
scanf("%d",&arr1[i][j]);
}
}
printf("Input elements in the second matrix :\n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
printf("element - [%d],[%d] : ",i,j);
scanf("%d",&brr1[i][j]);

[Pick the date] Page 4


}
}
printf("\nThe First matrix is :\n");
for(i=0;i<r1;i++)
{
printf("\n");
for(j=0;j<c1;j++)
printf("%d\t",arr1[i][j]);
}

printf("\nThe Second matrix is :\n");


for(i=0;i<r2;i++)
{
printf("\n");
for(j=0;j<c2;j++)
printf("%d\t",brr1[i][j]);
}
//multiplication of matrix
for(i=0;i<r1;i++)
for(j=0;j<c2;j++)
crr1[i][j]=0;

[Pick the date] Page 5


for(i=0;i<r1;i++) //row of first matrix
{
for(j=0;j<c2;j++) //column of second matrix
{
sum=0;
for(k=0;k<c1;k++)
sum=sum+arr1[i][k]*brr1[k][j];
crr1[i][j]=sum;
}
}
printf("\nThe multiplication of two matrices is : \n");
for(i=0;i<r1;i++)
{
printf("\n");
for(j=0;j<c2;j++)
{
printf("%d\t",crr1[i][j]);
}
}
}
printf("\n\n");

[Pick the date] Page 6


}

INPUT & OUTPUT:

[Pick the date] Page 7


RESULT:

Therefore, this is the program in C for multiplication of two square


Matrices.

[Pick the date] Page 8


PROGRAM-13 21BKT0077

Write a program in C to find the number occurring odd number


of times in an array.

Expected Output :

The given array is : 8 3 8 5 4 3 4 3 5


The element odd number of times is : 3

PROGRAM:

#include <stdio.h>

int findOddCountElem (int *arr1, int n )


{
int i, ResultXor = 0;
for(i = 0; i < n; i++)
{
[Pick the date] Page 2
ResultXor = ResultXor ^ arr1[i];
}
return ResultXor;
}

int main()
{
int i;
int arr1[] = {8, 3, 8, 5, 4, 3, 4, 3, 5};

int ctr = sizeof(arr1)/sizeof(arr1[0]);


printf("The given array is : ");

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


{
printf("%d ", arr1[i]);
}
printf("\n");

printf("Number of odd number occur(s) : %d times.\n",


findOddCountElem(arr1, ctr));
return 0;
}

[Pick the date] Page 3


INPUT & OUTPUT:

RESULT:

Therefore, this is the program in C to find the number occurring


odd number of times in an array.

[Pick the date] Page 4


PROGRAM-14 21BKT0077

Write a program in C to find the missing number from a given


array. There are no duplicates in list.

Expected Output :

The given array is : 1 3 4 2 5 6 9 8


The element odd number of times is : 7

PROGRAM:

#include <stdio.h>

int pickMissNumber(int *arr1, int ar_size)


{
int i, sum = 0, n = ar_size + 1;
for(i = 0; i < ar_size; i++)
{
[Pick the date] Page 2
sum = sum + arr1[i];
}

return (n*(n+1))/2 - sum;


}

int main()
{
int i;
int arr1[] = {1, 3, 4, 2, 5, 6, 9, 8};

int ctr = sizeof(arr1)/sizeof(arr1[0]);


printf("The given array is : ");

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


{
printf("%d ", arr1[i]);
}
printf("\n");

printf("The missing number is : %d \n", pickMissNumber(arr1, ctr));


return 0;
}
[Pick the date] Page 3
INPUT & OUTPUT:

RESULT:

Therefore, this is the program in C to find the missing number

from a given array. There are no duplicates in list.

[Pick the date] Page 4


BCSE102P -
Structured and Object-Oriented
Programming

School of Computer Science and Engineering (SCOPE)


Vellore Institute of Technology
Vellore.
DIGITAL
ASSIGNMENT 2
Name: Chaparala Sanjay Reg No: 21BKT0077

1. Write a program find the sum of N numbers using C.

CODE:

#include<stdio.h>
int main()
{
int Number, i, Sum = 0;

printf("\nPlease Enter any Integer Value\n");


scanf("%d", &Number);

for(i = 1; i <= Number; i++)


{
Sum = Sum + i;
}

printf("Sum of Natural Numbers = %d", Sum);


return 0;
}
OUTPUT:

RESULT:

Therefore, this is the program in C to find the sum of N numbers using C.


2. Write a program develop a Simple Calculator using Switch Case.

CODE:

#include <stdio.h>

int main() {

char op;
double first, second;
printf("Enter an operator (+, -, *, /): ");
scanf("%c", &op);
printf("Enter two operands: ");
scanf("%lf %lf", &first, &second);

switch (op) {
case '+':
printf("%.1lf + %.1lf = %.1lf", first, second, first +
second);
break;
case '-':
printf("%.1lf - %.1lf = %.1lf", first, second, first -
second);
break;
case '*':
printf("%.1lf * %.1lf = %.1lf", first, second, first *
second);
break;
case '/':
printf("%.1lf / %.1lf = %.1lf", first, second, first /
second);
break;
// operator doesn't match any case constant
default:
printf("Error! operator is not correct");
}

return 0;
}
OUTPUT:

RESULT:

Therefore, this is the program in C to develop a Simple Calculator using


Switch Case.
3. Write a program to find sum of digits Example 3456=18

CODE:

#include <stdio.h>

/* Function to get sum of digits */


int getSum(int n)
{
int sum = 0;
while (n != 0) {
sum = sum + n % 10;
n = n / 10;
}
return sum;
}

// Driver code
int main()
{
int n;
scanf("%d",&n);
printf(" %d ", getSum(n));
return 0;
}
OUTPUT:

RESULT:

Therefore, this is the program in C to find sum of digits.


4. Write a program to find the reverse of a given number and find the
sum of digits.

CODE:

#include <stdio.h>

/* Function to get sum of digits */


int getSum(int n)
{
int sum = 0;
while (n != 0) {
sum = sum + n % 10;
n = n / 10;
}
return sum;
}

// Driver code
int main() {

int n, reverse = 0, remainder;

printf("Enter an integer: ");


scanf("%d", &n);

while (n != 0) {
remainder = n % 10;
reverse = reverse * 10 + remainder;
n /= 10;
}

printf("Reversed number = %d \n", reverse);


printf("Sum of digits = %d",getSum(reverse));

return 0;
}
OUTPUT:

RESULT:

Therefore, this is the program in C to find the reverse of a given number


and to find the sum of digits.
5. Write a program to find the Factorial of a given Number using
recursive function.

CODE:

#include<stdio.h>
long int multiplyNumbers(int n);
int main() {
int n;
printf("Enter a positive integer: ");
scanf("%d",&n);
printf("Factorial of %d = %ld", n, multiplyNumbers(n));
return 0;
}

long int multiplyNumbers(int n) {


if (n>=1)
return n*multiplyNumbers(n-1);
else
return 1;
}
OUTPUT:

RESULT:

Therefore, this is the program in C to find the Factorial of a given Number


using recursive function.
6. Write a program to the Check the given number is an Armstrong
Number or Not.

CODE:

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

int main() {
int num, originalNum, remainder, n = 0;
float result = 0.0;

printf("Enter an integer: ");


scanf("%d", &num);

originalNum = num;

// store the number of digits of num in n


for (originalNum = num; originalNum != 0; ++n) {
originalNum /= 10;
}

for (originalNum = num; originalNum != 0; originalNum /= 10) {


remainder = originalNum % 10;

// store the sum of the power of individual digits in result


result += pow(remainder, n);
}

// if num is equal to result, the number is an Armstrong number


if ((int)result == num)
printf("%d is an Armstrong number.", num);
else
printf("%d is not an Armstrong number.", num);
return 0;
}
OUTPUT:

RESULT:

Therefore, this is the program in C to Check the given number is an


Armstrong Number or Not.
7. Write a program to Convert the given Decimal Number into Binary.

CODE:

#include <stdio.h>
int main()
{
int a[10], number, i, j;
printf("\n Please Enter the Number You want to Convert : ");
scanf("%d", &number);

for(i = 0; number > 0; i++)


{
a[i] = number % 2;
number = number / 2;
}

printf("\n Binary Number of a Given Number = ");


for(j = i - 1; j >= 0; j--) {
printf(" %d ", a[j]);
}
printf("\n");
return 0;
}
OUTPUT:

RESULT:

Therefore, this is the program in C to convert the given Decimal Number


into Binary.
8. Write a program to arrange numbers in an Array in incremental
order.

CODE:

#include <stdio.h>

void main()
{
int arr1[100];
int n, i, j, tmp;

printf("\n\nsort elements of array in ascending order :\n ");


printf("----------------------------------------------\n");

printf("Input the size of array : ");


scanf("%d", &n);

printf("Input %d elements in the array :\n",n);


for(i=0;i<n;i++)
{
printf("element - %d : ",i);
scanf("%d",&arr1[i]);
}

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


{
for(j=i+1; j<n; j++)
{
if(arr1[j] <arr1[i])
{
tmp = arr1[i];
arr1[i] = arr1[j];
arr1[j] = tmp;
}
}
}
printf("\nElements of array in sorted ascending order:\n");
for(i=0; i<n; i++)
{
printf("%d ", arr1[i]);
}
printf("\n\n");
}
OUTPUT:

RESULT:

Therefore, this is the program in C to arrange numbers in an Array in


incremental order.
9. Delete Element from an Array.

CODE:

#include <stdio.h>

int main()
{
int array[100], position, c, n;

printf("Enter number of elements in array\n");


scanf("%d", &n);

printf("Enter %d elements\n", n);

for ( c = 0 ; c < n ; c++ )


scanf("%d", &array[c]);

printf("Enter the location where you wish to delete element\n");


scanf("%d", &position);

if ( position >= n+1 )


printf("Deletion not possible.\n");

else
{
for ( c = position - 1 ; c < n - 1 ; c++ )
array[c] = array[c+1];

printf("Resultant array is\n");


for( c = 0 ; c < n - 1 ; c++ )
printf("%d\n", array[c]);
}
return 0;
}
OUTPUT:

RESULT:

Therefore, this is the program in C to Delete Element from an Array.


10. Write a program to Delete Word from a String.

CODE:

#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
char str[100], word[20];
int i, j, ls, lw, temp, chk=0;
printf("Enter the String: ");
gets(str);
printf("Enter a Word: ");
gets(word);
ls = strlen(str);
lw = strlen(word);
for(i=0; i<ls; i++)
{
temp = i;
for(j=0; j<lw; j++)
{
if(str[i]==word[j])
i++;
}
chk = i-temp;
if(chk==lw)
{
i = temp;
for(j=i; j<(ls-lw); j++)
str[j] = str[j+lw];
ls = ls-lw;
str[j]='\0';
}
}
printf("\nNew String = %s", str);
getch();
return 0;
}
OUTPUT:

RESULT:

Therefore, this is the program in C to Delete Word from a String.


11. Write a program to find the reverse of a String without using
String Functions.

CODE:

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

int main()
{
char Str[100];
int i, len;
printf("\n Please Enter String : ");
gets(Str);

len = strlen(Str);

printf("\n Reverse string is : ");


for (i = len - 1; i >= 0; i--)
{
printf("%c", Str[i]);
}
return 0;
}
OUTPUT:

RESULT:

Therefore, this is the program in C to find the reverse of a String without


using String Functions.
12. Write a program to find the given String is a palindrome or not
without using String Functions.

CODE:

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

void main()
{
char string[25], reverse_string[25] = {'\0'};
int i, length = 0, flag = 0;

printf("Enter a string \n");


gets(string);
/* keep going through each character of the string till its end
*/
for (i = 0; string[i] != '\0'; i++)
{
length++;
}
printf("The length of the string '%s' = %d\n", string, length);
for (i = length - 1; i >= 0 ; i--)
{
reverse_string[length - i - 1] = string[i];
}
/* Check if the string is a Palindrome */

for (flag = 1, i = 0; i < length ; i++)


{
if (reverse_string[i] != string[i])
flag = 0;
}
if (flag == 1)
printf ("%s is a palindrome \n", string);
else
printf("%s is not a palindrome \n", string);
}
OUTPUT:

RESULT:

Therefore, this is the program in C to find the given String is a palindrome


or not without using String Functions.
13. Write a program to perform swapping operation by using
Functions with Call by Value.

CODE:

#include<stdio.h>

void swap(int,int);

void main( )
{
int n1,n2;
printf("Enter the two numbers to be swapped\n");
scanf("%d%d",&n1,&n2);
printf("\nThe values of n1 and n2 in the main function before
calling the swap function are n1 = %d n2 = %d",n1,n2);
swap(n1,n2);
printf("\nThe values of n1 and n2 in the main function after
calling the swap function are n1 = %d n2 = %d",n1,n2);}

void swap(int n1,int n2)


{
int temp;
temp=n1;
n1=n2;
n2=temp;
printf("\nThe values of n1 and n2 in the swap function after
swapping are n1 = %d n2 = %d",n1,n2);
}
OUTPUT:

RESULT:

Therefore, this is the program in C to perform swapping operation by using


Functions with Call by Value.
14. Write a program to perform swapping operation by using
Functions with Call by Reference.

CODE:

#include <stdio.h>

/* Swap function declaration */


void swap(int * num1, int * num2);

int main()
{
int num1, num2;

/* Input numbers */
printf("Enter two numbers: \n");
scanf("%d%d", &num1, &num2);

/* Print original values of num1 and num2 */


printf("Before swapping in main \n");
printf("Value of num1 = %d \n", num1);
printf("Value of num2 = %d \n\n", num2);

/* Pass the addresses of num1 and num2 */


swap(&num1, &num2);

/* Print the swapped values of num1 and num2 */


printf("After swapping in main \n");
printf("Value of num1 = %d \n", num1);
printf("Value of num2 = %d \n\n", num2);

return 0;
}

/**
* Function to swap two numbers
*/
void swap(int * num1, int * num2)
{
int temp;

// Copy the value of num1 to some temp variable


temp = *num1;

// Copy the value of num2 to num1


*num1= *num2;

// Copy the value of num1 stored in temp to num2


*num2= temp;

printf("After swapping in swap function \n");


printf("Value of num1 = %d \n", *num1);
printf("Value of num2 = %d \n\n", *num2);
}
OUTPUT:

RESULT:

Therefore, this is the program in C to perform swapping operation by using


Functions with Call by Reference.
BCSE102P -
Structured and Object-Oriented
Programming

School of Computer Science and Engineering (SCOPE)


Vellore Institute of Technology
Vellore.
DIGITAL
ASSIGNMENT 3
NAME:CHAPARALA SANJAY REG.NO.: 21BKT0077

1. Write a C++ program to collect the details of a set of


students including their name, registration number,
department, pass percentage. Calculate the grade of students
without using structures.

CODE:

#include <iostream>
using namespace std;
class student
{
private:
char name[100];
char reg_no[100];
char dept[150];
int marks;
public:
void get_data()
{
cout<<"Enter Name"<<endl;
cin>>name;
cout<<"Enter Registration Number"<<endl;
cin>>reg_no;
cout<<"Enter Department"<<endl;
cin>>dept;
cout<<"Enter Pass Percentage"<<endl;
cin>>marks;
}
void display()
{
cout<<"Name: "<<name<<endl;
cout<<"Registration Number: "<<reg_no<<endl;
cout<<"Department: "<<dept<<endl;
cout<<"Pass percentage: "<<marks<<endl;
}
void display_grade()
{

if (marks >= 90 && marks <=100){


cout<<"Your grade is A+";
}
else if (marks >= 80 && marks <=89){
cout<<"Your grade is A";
}
else if (marks >= 70 && marks <= 79){
cout<<"Your grade is B";
}
else if (marks >= 60 && marks <=69){
cout<<"Your grade is C";
}
else if (marks >= 50 && marks <=59){
cout<<"Your grade is D";
}
else if (marks <=50 && marks>=0){
cout<<"Your grade is F";
}

else{
cout<<"Please enter valid marks";
}
cout<<endl;
}
};
int main()
{
int n,i;

cout<<"Enter number of Students: "<<endl;


cin>>n;
student s[n];
for(i=0;i<n;i++){

cout << "Enter student details" << endl;


s[i].get_data();

}
for(i=0;i<n;i++){
cout << "Student Details " << endl;
s[i].display();
cout << "Student Grade " << endl;
s[i].display_grade();
cout<<endl;
}
return 0;
}
OUTPUT:

RESULT:

Therefore, this is the program in C++ to collect the details of a set of


students including their name, registration number, department, pass
percentage and then to calculate the grade of students without using
structures.
2. Write a C++ program to calculate the area and volume of
sphere, cube and cuboid.

CODE:

#include <iostream>
using namespace std;

void cube(int s, int area, int vol){


cout<<"Enter the side of cube : "<<endl;
cin>>s;
cout<<endl;
area=6*s*s;
vol=s*s*s;
cout<<"Area of CUBE = "<<area<<endl;
cout<<"Volume of CUBE = "<<vol<<endl;

}
void cuboid(int l,int b, int h,int area,int vol){
cout<<"Enter the dimensions of cuboid: "<<endl;
cin>>l;
cout<<endl;
cin>>b;
cout<<endl;
cin>>h;
cout<<endl;
area=2*(l*b+b*h+l*h);
vol=l*b*h;
cout<<"Area of CUBOID = "<<area<<endl;
cout<<"Volume of CUBOID = "<<vol<<endl;
}

void sphere(int r,int area,int vol){


cout<<"Enter the radius of sphere : "<<endl;
cin>>r;
cout<<endl;
area=4*3.14*r*r;
vol=(4/3)*3.4*r*r*r;
cout<<"Area of SPHERE = "<<area<<endl;
cout<<"Volume of SPHERE = "<<vol<<endl;

int main(){
int type;
int a,l,b,h,r;
int area, volume;
cout<<"Enter '1' for cube"<<endl;
cout<<"Enter '2' for cuboid"<<endl;
cout<<"Enter '3' for sphere"<<endl;
cin>>type;
cout<<endl;

switch(type){
case 1:
cube(a,area,volume);
break;
case 2:
cuboid(l,b,h,area,volume);
break;
case 3:
sphere(r,area,volume);
break;

return 0;
}
OUTPUT:

RESULT:

Therefore, this is the program in C++ to calculate the area and


volume of sphere, cube and cuboid.
3. Write a C++ program to check if the given number is prime
or not, odd or even, and if it is a palindrome or not using
switch case.

CODE:

#include <iostream>
using namespace std;
int main()
{
int i, n, num, digit, rev = 0;
bool is_prime = true;
char c1;
cout << "Enter a positive number: " << endl;
cin >> num;
cout << "Which condition should be checked Palindrome(P) or
Prime(p) or odd(O) : " << endl;
cin >> c1;
switch (c1)
{
case 'P':
n = num;
do
{
digit = num % 10;
rev = (rev * 10) + digit;
num = num / 10;
} while (num != 0);
cout << " The reverse of the number is: " << rev << endl;
if (n == rev)
cout << " The number is a palindrome.";
else
cout << " The number is not a palindrome.";
break;
case 'p':
if (num == 0 || num == 1)
{
is_prime = false;
}
// loop to check if n is prime
for (i = 2; i <= num / 2; ++i)
{
if (num % i == 0)
{
is_prime = false;
break;
}
}
if (is_prime)
cout << num << " is a prime number";
else
cout << num << " is not a prime number";
break;
case 'O':
if (num % 2 == 0)
cout << num << " is even.";
else
cout << num << " is odd.";
break;
}
return 0;
}
OUTPUT:

RESULT:

Therefore, this is the program in C++ to check if the given number is


prime or not, odd or even, and if it is a palindrome or not using switch
case.
4. Write a C++ program to perform the addition of two matrix
with
options:
1) 2x2 matrix addition
2) 3x3 matrix addition
3) show the result
4) exit from program

CODE:

#include <iostream>
#include<stdlib.h>
using namespace std;

int two(int i, int j, int t1[2][2],int t2[2][2], int


s2[2][2]){
cout<<"Enter the elements of 1st 2x2 matrix :"<<endl;
for(i=0;i<2;++i){
for(j=0;j<2;++j){
cin>>t1[i][j];
}
}
cout<<"Enter the elements of 2nd 2x2 matrix :"<<endl;
for(i=0;i<2;++i){
for(j=0;j<2;++j){
cin>>t2[i][j];
}
}
for(i=0;i<2;++i){
for(j=0;j<2;++j){
s2[i][j]=t1[i][j]+t2[i][j];
}
}
return s2[2][2];
}

int three(int i, int j,int th1[3][3],int th2[3][3], int


s3[3][3]){
cout<<"Enter the elements of 1st 3x3 matrix :"<<endl;
for(i=0;i<3;i++){
for(j=0;j<3;j++){
cin>>th1[i][j];
}
}
cout<<"Enter the elements of 2nd 3x3 matrix :"<<endl;
for(i=0;i<3;i++){
for(j=0;j<3;j++){
cin>>th2[i][j];
}
}
for(i=0;i<3;i++){
for(j=0;j<3;j++){
s3[i][j]=th1[i][j]+th2[i][j];
}
}
return s3[3][3];
}

void display(int i, int j,int s2[2][2], int s3[3][3]){


cout<<"The sum of 3x3 matrices is :"<<endl;
for(i=0;i<3;i++){
for(j=0;j<3;j++){
cout<<s3[i][j]<<endl;
}
}
cout<<"The sum of 2x2 matrices is :"<<endl;
for(i=0;i<2;i++){
for(j=0;j<2;j++){
cout<<s2[2][2]<<endl;
}
}
}

int main()
{
int opt, i, j;
int two1[2][2],two2[2][2],sum2[2][2];
int three1[3][3],three2[3][3],sum3[3][3];

while(opt<=4){
cout<<"Enter an option :"<<endl;
cin>>opt;
if(opt==1)
two(i,j,two1,two2,sum2);
else if(opt==2)
three(i,j,three1,three2,sum3);
else if(opt==3)
display( i,j,sum2,sum3);
else if(opt==4)
break;

}
return 0;

}
OUTPUT:
RESULT:

Therefore, this is the program in C++ to perform the addition of


two matrix with options:
1) 2x2 matrix addition
2) 3x3 matrix addition
3) show the result
4) exit from program
BCSE102P -
Structured and Object-Oriented
Programming

School of Computer Science and Engineering (SCOPE)


Vellore Institute of Technology
Vellore.
DIGITAL
ASSIGNMENT 4
NAME:CHAPARALA SANJAY REG.NO.: 21BKT0077

1. Write a C++ program to perform employee yearly salary


increment using operation overload.

CODE:

#include<iostream>
#include<string>
using namespace std;

class employee{
public:
string name;
string ID;
int age;
int exp;
int basic_salary;
int net_salary;
int increment;

void getdata(){
cout<<"enter employee name :"<<endl;
cin>>name;
cout<<"enter employee ID :"<<endl;
cin>>ID;
cout<<"enter employee age :"<<endl;
cin>>age;
cout<<"enter employee Basic salary :"<<endl;
cin>>basic_salary;
cout<<"enter employee experience :"<<endl;
cin>>exp;
cout<<"enter employee net salary :"<<endl;
cin>>net_salary;
}

employee operator+=(employee &emp){


if(emp.exp>=0 && emp.exp<5)
emp.increment=0.1*emp.basic_salary;
else if(emp.exp>=5 && emp.exp<10)
emp.increment=0.15*emp.basic_salary;
else if(emp.exp>=10 && emp.exp<15)
emp.increment=0.2*emp.basic_salary;
else
emp.increment=0;

return emp;
}

void print_increment(){
cout<<"Yearly increment for the employee "<<name<<" is =
"<<increment<<endl;
}
};

int main(){
int n,i;
cout<<"Enter number of employees : "<<endl;
cin>>n;

employee emp[n];
for(i=0;i<n;i++){
cout<<"Enter employee details :"<<endl;
emp[i].getdata();
emp[i]+=emp[i];
}
cout<<endl;

for(i=0;i<n;i++){
emp[i].print_increment();

}
}
OUTPUT:

RESULT:

Therefore, this is the program in C++ to perform employee yearly salary


increment using operation overload.
2. Write a C++ program to create a student record with the
necessary information and classify the student group into 3
different groups like sports, cultural, and social service.

CODE:

#include<iostream>
using namespace std;
class student{
protected:
string reg_no;
string name;
string branch;
public:
void getdata(){
cout << "Name: "; cin >> name;
cout << "Reg no: "; cin >> reg_no;
cout << "Branch: "; cin >> branch;}
};

class sports : public student{


protected:
string sports_name;
string position;
int sport_fees;
public:
void sport_getdata(){
cout << "Name of the sport: "; cin >> sports_name;
cout << "Role you play in your respective sport: "; cin >>
position;
cout << "Fees paid: "; cin >> sport_fees;
}
void print_s(){
cout << "Name of the sport: " << sports_name << endl;
cout << "Role you play in your respective sport: " <<
position << endl;
cout << "Fees paid: " << sport_fees << endl;
}
};

class cultural: public student{


protected:
string cultural_name;
string designation;
public:
void cul_getdata(){
cout << "Name of art form: "; cin >> cultural_name;
cout << "Designation: "; cin >> designation;
}
void print_c(){
cout << "Art: " << cultural_name << endl;
cout << "Designation: " << designation << endl;
}
};

class social_service: public student{


protected:
string social_event;
string designation;
int charity;
public:
void soc_getdata(){
cout << "Name of the social event you were in: "; cin >>
social_event;
cout << "Designation: "; cin >> designation;
cout << "Amount of money donated: "; cin >> charity;
}
void print_ss(){
cout << "Social event: " << social_event << endl;
cout << "Designation: "<< designation << endl;
cout << "Amount of money donated: "<< charity << endl;
}
};

int main(){
sports a;cultural b;social_service c;
a.getdata();a.sport_getdata();
b.getdata();b.cul_getdata();
c.getdata();c.soc_getdata();
cout<<endl;
cout<<endl;
a.print_s();
cout<<endl;
b.print_c();
cout<<endl;
c.print_ss();
cout<<endl;
}
OUTPUT:

RESULT:

Therefore, this is the program in C++ to create a student record


with the necessary information and classify the student group
into 3 different groups like sports, cultural, and social service.
3. Write a C++ program to perform matrix addition and
subtraction using operator overloading for a 3x3 matrix.

CODE:

#include<iostream>
using namespace std;

class Matrix
{
int a[3][3];
public:
void accept();
void display();
void operator +(Matrix x);
void operator -(Matrix x);
};
void Matrix::accept()
{
cout<<"\n Enter Matrix Element (3 X 3) : \n";
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
cout<<" ";
cin>>a[i][j];
}
}
}
void Matrix::display()
{
for(int i=0; i<3; i++)
{
cout<<" ";
for(int j=0; j<3; j++)
{
cout<<a[i][j]<<"\t";
}
cout<<"\n";
}
}
void Matrix::operator +(Matrix x)
{
int mat[3][3];
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
mat[i][j]=a[i][j]+x.a[i][j];
}
}
cout<<"\n Addition of Matrix : \n\n";
for(int i=0; i<3; i++)
{
cout<<" ";
for(int j=0; j<3; j++)
{
cout<<mat[i][j]<<"\t";
}
cout<<"\n";
}
}

void Matrix::operator -(Matrix x)


{
int mat[3][3];
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
mat[i][j]=a[i][j]-x.a[i][j];
}
}
cout<<"\n Subtraction of Matrix : \n\n";
for(int i=0; i<3; i++)
{
cout<<" ";
for(int j=0; j<3; j++)
{
cout<<mat[i][j]<<"\t";
}
cout<<"\n";
}
}

int main()
{
Matrix m,n;
m.accept(); // Accepting Rows
n.accept(); // Accepting Columns
cout<<"\n First Matrix : \n\n";
m.display(); // Displaying First Matrix
cout<<"\n Second Matrix : \n\n";
n.display(); // Displaying Second Matrix
m+n;
m-n;
return 0;
}
OUTPUT:
RESULT:

Therefore, this is the program in C++ to perform matrix addition and


subtraction using operator overloading for a 3x3 matrix.
4. Write a C++ program to perform string handling operations
of string cat, copy, length and reverse of a string by using
operation overload.

CODE:

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
class String
{
public:
string s;
String (string s=""){
this->s = s;
}
String operator+ (String const &obj){
String res;
res.s = s + obj.s;
return res;
}
void operator= (String const &obj) {
s = obj.s;
}
int operator! () {
return s.length();
}
void operator< (String const &obj)
{
s = obj.s;
reverse(s.begin(), s.end());
}
};
int main(){
String A("ABCD");
String B("WXYZ");
cout << "String A: " << A.s << " String B: " << B.s << endl;
String C = A+B;
cout << "String C (After Performing C = A+B): " << C.s <<
endl;
A = B;
cout << "String A (After performing A = B): " << A.s << endl;
cout << "Length of String A (After performing !A): " << !A <<
endl;
A < B;
cout << "String A (After performing A < B): " << A.s << endl;
return 0;
}
OUTPUT:

RESULT:

Therefore, this is the program in C++ to perform string handling


operations of string cat, copy, length and reverse of a string by
using operation overload.
5. Write a C++ program for maintaining a university student
record with basic student information like register number,
name, age, school, department. University- grandparent
class, school- parent class, department- child class.

CODE:

#include<iostream>
using namespace std;
class university
{
protected:
string registration_number;
char name[50];
int age;
};
class school:public university
{
protected:
char school_name[50];
};
class department:public school
{
private:
char department_name[50];
public:
void input()
{
cout<<"Enter registration number : ";
cin>>registration_number;
cout<<"Enter Name : ";
cin>>name;
cout<<"Enter age : ";
cin>>age;
cout<<"Enter school name : ";
cin>>school_name;
cout<<"Enter department name : ";
cin>>department_name;
}
void output()
{
cout<<endl;
cout<<endl;
cout<<"Registration number of the student is : ";
cout<<registration_number;
cout<<endl;
cout<<"Name of the student is : ";
cout<<name;
cout<<endl;
cout<<"Age of the student is : ";
cout<<age;
cout<<endl;
cout<<"Name of the school is : ";
cout<<school_name;
cout<<endl;
cout<<"Name of the department is : ";
cout<<department_name;
cout<<endl;
}
};
int main()
{
department a;
a.input();
a.output();
return 0;
}
OUTPUT:

RESULT:

Therefore, this is the C++ program for maintaining a university


student record with basic student information like register
number, name, age, school, department. University- grandparent
class, school- parent class, department- child class.

You might also like