[go: up one dir, main page]

0% found this document useful (0 votes)
80 views50 pages

C-LAB Key by Kishore Gogikar

C lab solutions

Uploaded by

kishore gogikar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views50 pages

C-LAB Key by Kishore Gogikar

C lab solutions

Uploaded by

kishore gogikar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 50

BCA PRACTICAL QUESTION BANK,OSMANIA UNIVERSITY -2023

C-LANGUAGE LAB QUESTIONS BCA-SEM-I


1A. // C-program-Working of arithmetic operators
#include <stdio.h>
int main()
{
int a = 9,b = 4, c;
c = a+b;
printf("a+b = %d \n",c);
c = a-b;
printf("a-b = %d \n",c);
c = a*b;
printf("a*b = %d \n",c);
c = a/b;
printf("a/b = %d \n",c);
c = a%b;
printf("Remainder when a divided by b = %d \n",c);
return 0;
}
Out put:
a+b = 13
a-b = 5
a*b = 36
a/b = 2
Remainder when a divided by b=1

Page 1 of 50
BCA PRACTICAL QUESTION BANK,OSMANIA UNIVERSITY -2023

1B.// C-program on -Working of logical operators

#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10, result;
result = (a == b) && (c > b);
printf("(a == b) && (c > b) is %d \n", result);
result = (a == b) && (c < b);
printf("(a == b) && (c < b) is %d \n", result);
result = (a == b) || (c < b);
printf("(a == b) || (c < b) is %d \n", result);
result = (a != b) || (c < b);
printf("(a != b) || (c < b) is %d \n", result);
result = !(a != b);
printf("!(a != b) is %d \n", result);
result = !(a == b);
printf("!(a == b) is %d \n", result);
return 0;
}

OUTPUT:

Page 2 of 50
BCA PRACTICAL QUESTION BANK,OSMANIA UNIVERSITY -2023

(a == b) && (c > b) is 1
(a == b) && (c < b) is 0
(a == b) || (c < b) is 1
(a != b) || (c < b) is 0
!(a != b) is 1
!(a == b) is 0

1C.//C-Program on-working of bitwise operators


#include <stdio.h>
main() {
unsigned int a = 60; /* 60 = 0011 1100 */
unsigned int b = 13; /* 13 = 0000 1101 */
int c = 0;
c = a & b; /* 12 = 0000 1100 */
printf("Line 1 - Value of c is %d\n", c );
c = a | b; /* 61 = 0011 1101 */
printf("Line 2 - Value of c is %d\n", c );
c = a ^ b; /* 49 = 0011 0001 */
printf("Line 3 - Value of c is %d\n", c );
c = ~a; /*-61 = 1100 0011 */
printf("Line 4 - Value of c is %d\n", c );
c = a << 2; /* 240 = 1111 0000 */
printf("Line 5 - Value of c is %d\n", c );

Page 3 of 50
BCA PRACTICAL QUESTION BANK,OSMANIA UNIVERSITY -2023

c = a >> 2; /* 15 = 0000 1111 */


printf("Line 6 - Value of c is %d\n", c );
}
OUTPUT:
Line 1 - Value of c is 12
Line 2 - Value of c is 61
Line 3 - Value of c is 49
Line 4 - Value of c is -61
Line 5 - Value of c is 240
Line 6 - Value of c is 15.

1D.//C-Program on-ternary operator


#include <stdio.h>
int main() {
int age;
// take input from users
printf("Enter your age: ");
scanf("%d", &age);
// ternary operator to find if a person can vote or not
(age >= 18) ? printf("You can vote") : printf("You cannot vote");
return 0;
}

OUT PUT:

Page 4 of 50
BCA PRACTICAL QUESTION BANK,OSMANIA UNIVERSITY -2023

Enter your age: 12


You cannot vote
2A.//C program to find roots of a quadratic equation.
#include <math.h>
#include <stdio.h>
int main() {
double a, b, c, discriminant, root1, root2, realPart, imagPart;
printf("Enter coefficients a, b and c: ");
scanf("%lf %lf %lf", &a, &b, &c);
discriminant = b * b - 4 * a * c;
// condition for real and different roots
if (discriminant > 0) {
root1 = (-b + sqrt(discriminant)) / (2 * a);
root2 = (-b - sqrt(discriminant)) / (2 * a);
printf("root1 = %.2lf and root2 = %.2lf", root1, root2);
}
// condition for real and equal roots
else if (discriminant == 0) {
root1 = root2 = -b / (2 * a);
printf("root1 = root2 = %.2lf;", root1);
}
// if roots are not real
else {
realPart = -b / (2 * a);

Page 5 of 50
BCA PRACTICAL QUESTION BANK,OSMANIA UNIVERSITY -2023

imagPart = sqrt(-discriminant) / (2 * a);


printf("root1 = %.2lf+%.2lfi and root2 = %.2f-%.2fi", realPart, imagPart,
realPart, imagPart);
}
return 0;
}
OUTPUT:
Enter coefficients a, b and c: 2.3
4
5.6
root1 = -0.87+1.30i and root2 = -0.87-1.30i
2B.//C-Program on reverse of number.
#include <stdio.h>
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", reverse);
return 0;
}
Page 6 of 50
BCA PRACTICAL QUESTION BANK,OSMANIA UNIVERSITY -2023

OUTPUT;
Enter an integer: 2345
Reversed number = 5432

2C.// Write a Program to Find Sum of Digits of a given number


#include<stdio.h>
int main ()
{
int num, sum = 0;
num = 1234;
printf("The number is = %d\n",num);
//loop to find sum of digits
while(num!=0){
sum += num % 10;
num = num / 10;
}
//output
printf("Sum: %d\n",sum);
return 0;
}
OUTPUT:
The number is = 1234
Sum: 10
2D.// C-Program on Multiplication table.

Page 7 of 50
BCA PRACTICAL QUESTION BANK,OSMANIA UNIVERSITY -2023

#include <stdio.h>
int main() {
int n, i;
printf("Enter an integer: ");
scanf("%d", &n);
for (i = 1; i <= 10; ++i) {
printf("%d * %d = %d \n", n, i, n * i);
}
return 0;
}
OUTPUT:
Enter an integer: 9
9*1=9
9 * 2 = 18
9 * 3 = 27
9 * 4 = 36
9 * 5 = 45
9 * 6 = 54
9 * 7 = 63
9 * 8 = 72
9 * 9 = 81
9 * 10 = 90
2E.// C-Program on Armstrong Number.
#include <math.h>

Page 8 of 50
BCA PRACTICAL QUESTION BANK,OSMANIA UNIVERSITY -2023

#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;
}
Run Code

Page 9 of 50
BCA PRACTICAL QUESTION BANK,OSMANIA UNIVERSITY -2023

Output
Enter an integer: 1634
1634 is an Armstrong number.

2F.// C-Program on number is prime or not.


#include <stdio.h>
int main() {
int n, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
// 0 and 1 are not prime numbers
// change flag to 1 for non-prime number
if (n == 0 || n == 1)
flag = 1;
for (i = 2; i <= n / 2; ++i) {
// if n is divisible by i, then n is not prime
// change flag to 1 for non-prime number
if (n % i == 0) {
flag = 1;
break;
}
}
// flag is 0 for prime numbers
if (flag == 0)

Page 10 of 50
BCA PRACTICAL QUESTION BANK,OSMANIA UNIVERSITY -2023

printf("%d is a prime number.", n);


else
printf("%d is not a prime number.", n);
return 0;
}
OUTPUT:
Enter a positive integer: 29
29 is a prime number.

2G. // C-Program on Magic Number.


#include <stdio.h>
#include <stdlib.h>
int main()
{
int num, temp, rev=0, digit, sumOfDigits=0;
printf("Enter a Number \n");
scanf("%d",&num);
temp = num;
//Calculating Sum of digits
while(temp > 0){
//Extract digit and add them
sumOfDigits += temp % 10;
temp = temp / 10;
}

Page 11 of 50
BCA PRACTICAL QUESTION BANK,OSMANIA UNIVERSITY -2023

temp = sumOfDigits;
//Compute reverse of Sum of Digits
while( temp > 0){
rev = rev*10 + temp % 10;
temp = temp / 10;
}
if(rev*sumOfDigits == num)
printf("Magic Number \n");
else
printf("Not a Magic Number \n");
return 0;
}
3A.// C-Program on sin (x) series.
Logic:
First the computer reads the value of x and limit from the user.
Now convert x to radian value x=x*(3.1415\180)
Then using for loop the value of sin(x) is calculated.
//Finally the value of sin(x) is printed.
//Program for sin(x) series in C.

#include <stdio.h>
#include <math.h>
int fac(int x)
{

Page 12 of 50
BCA PRACTICAL QUESTION BANK,OSMANIA UNIVERSITY -2023

int i,fac=1;
for(i=1;i<=x;i++)
fac=fac*i;
return fac;
}
int main()
{
float x,Q,sum=0;
int i,j,limit;
printf("Enter the value of x of sinx series: ");
scanf("%f",&x);
printf("Enter the limit upto which you want to expand the series: ");
scanf("%d",&limit);
Q=x;
x = x*(3.1415/180);
for(i=1,j=1;i<=limit;i++,j=j+2)
{
if(i%2!=0)
{
sum=sum+pow(x,j)/fac(j);
}
else
sum=sum-pow(x,j)/fac(j);
}

Page 13 of 50
BCA PRACTICAL QUESTION BANK,OSMANIA UNIVERSITY -2023

printf("Sin(%0.1f): %f",Q,sum);
return 0;
}
OUTPUT:
Enter the value of x of sinx series: 40
Enter the limit upto which you want to expand the series: 5
Sin(40.0): 0.642772

3B . C program to find the sum of cos(x) series


#include <stdio.h>
#include <math.h>
void main()
{
int n, x1, i, j;
float x, sign, cosx, fact;
printf("Enter the number of the terms in a series\n");
scanf("%d", &n);
printf("Enter the value of x(in degrees)\n");
scanf("%f", &x);
x1 = x;
/* Degrees to radians */
x = x * (3.142 / 180.0);
cosx = 1;

Page 14 of 50
BCA PRACTICAL QUESTION BANK,OSMANIA UNIVERSITY -2023

sign = -1;
for (i = 2; i <= n; i = i + 2)
{
fact = 1;
for (j = 1; j <= i; j++)
{
fact = fact * j;
}
cosx = cosx + (pow(x, i) / fact) * sign;
sign = sign * (-1);
}
printf("Sum of the cosine series = %7.2f\n", cosx);
printf("The value of cos(%d) using library function = %f\n", x1,
cos(x));
}

4A. C-Program to convert decimal to binary


#include<stdio.h>
#include<stdlib.h>
int main(){
int a[10],n,i;
system ("cls");
printf("Enter the number to convert: ");
scanf("%d",&n);

Page 15 of 50
BCA PRACTICAL QUESTION BANK,OSMANIA UNIVERSITY -2023

for(i=0;n>0;i++)
{
a[i]=n%2;
n=n/2;
}
printf("\nBinary of Given Number is=");
for(i=i-1;i>=0;i--)
{
printf("%d",a[i]);
}
return 0;
}
OUTPUT:
Enter the number to convert: 5
Binary of Given Number is=101
4B. C-Program to convert binary to decimal
#include <stdio.h>
#include <math.h>
// function prototype
int convert(long long);
int main() {
long long n;
printf("Enter a binary number: ");
scanf("%lld", &n);

Page 16 of 50
BCA PRACTICAL QUESTION BANK,OSMANIA UNIVERSITY -2023

printf("%lld in binary = %d in decimal", n, convert(n));


return 0;
}
// function definition
int convert(long long n) {
int dec = 0, i = 0, rem;
while (n!=0) {
rem = n % 10;
n /= 10;
dec += rem * pow(2, i);
++i;
}
return dec;
}
OUTPUT:
Enter a binary number: 1101
1101 in binary = 13 in decimal
4C. C Program to Convert Binary to Octal
#include <stdio.h>
int main()
{
long int binarynum, octalnum = 0, j = 1, remainder;

printf("Enter the value for binary number: ");

Page 17 of 50
BCA PRACTICAL QUESTION BANK,OSMANIA UNIVERSITY -2023

scanf("%ld", &binarynum);
while (binarynum != 0)
{
remainder = binarynum % 10;
octalnum = octalnum + remainder * j;
j = j * 2;
binarynum = binarynum / 10;
}
printf("Equivalent octal value: %lo", octalnum);
return 0;
}
4D . C Program to Convert Octal to Binary
#include <stdio.h>
#define MAX 1000
int main()
{
char octalnum[MAX];
long i = 0;

printf("Enter any octal number: ");


scanf("%s", octalnum);
printf("Equivalent binary value: ");
while (octalnum[i])
{

Page 18 of 50
BCA PRACTICAL QUESTION BANK,OSMANIA UNIVERSITY -2023

switch (octalnum[i])
{
case '0':
printf("000"); break;
case '1':
printf("001"); break;
case '2':
printf("010"); break;
case '3':
printf("011"); break;
case '4':
printf("100"); break;
case '5':
printf("101"); break;
case '6':
printf("110"); break;
case '7':
printf("111"); break;
default:
printf("\n Invalid octal digit %c ", octalnum[i]);
return 0;
}
i++;
}

Page 19 of 50
BCA PRACTICAL QUESTION BANK,OSMANIA UNIVERSITY -2023

return 0;
}
4E. C Program to Convert Hexadecimal to Binary
#include <stdio.h>
#define MAX 1000

int main()
{
char binarynum[MAX], hexa[MAX];
long int i = 0;
printf("Enter the value for hexadecimal ");
scanf("%s", hexa);
printf("\n Equivalent binary value: ");
while (hexa[i])
{
switch (hexa[i])
{
case '0':
printf("0000"); break;
case '1':
printf("0001"); break;
case '2':
printf("0010"); break;
case '3':

Page 20 of 50
BCA PRACTICAL QUESTION BANK,OSMANIA UNIVERSITY -2023

printf("0011"); break;
case '4':
printf("0100"); break;
case '5':
printf("0101"); break;
case '6':
printf("0110"); break;
case '7':
printf("0111"); break;
case '8':
printf("1000"); break;
case '9':
printf("1001"); break;
case 'A':
printf("1010"); break;
case 'B':
printf("1011"); break;
case 'C':
printf("1100"); break;
case 'D':
printf("1101"); break;
case 'E':
printf("1110"); break;
case 'F':

Page 21 of 50
BCA PRACTICAL QUESTION BANK,OSMANIA UNIVERSITY -2023

printf("1111"); break;
case 'a':
printf("1010"); break;
case 'b':
printf("1011"); break;
case 'c':
printf("1100"); break;
case 'd':
printf("1101"); break;
case 'e':
printf("1110"); break;
case 'f':
printf("1111"); break;
default:
printf("\n Invalid hexa digit %c ", hexa[i]);
return 0;
}
i++;
}
return 0;
}

4F. C Program to Convert Binary to Hexadecimal

Page 22 of 50
BCA PRACTICAL QUESTION BANK,OSMANIA UNIVERSITY -2023

#include <stdio.h>
int main()
{
long int binaryval, hexadecimalval = 0, i = 1, remainder;
printf("Enter the binary number: ");
scanf("%ld", &binaryval);
while (binaryval != 0)
{
remainder = binaryval % 10;
hexadecimalval = hexadecimalval + remainder * i;
i = i * 2;
binaryval = binaryval / 10;
}
printf("Equivalent hexadecimal value: %lX", hexadecimalval);
return 0;
}
5A. C-Program on Pascal Triangle;
#include < stdio.h >
long factorial(int);
int main()
{
int i, n, c;
printf("Enter the number of rows you wish to see in pascal triangle\n");
scanf("%d", & n);

Page 23 of 50
BCA PRACTICAL QUESTION BANK,OSMANIA UNIVERSITY -2023

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


for (c = 0; c <= (n - i - 2); c++) printf(" ");
for (c = 0; c <= i; c++) printf("%ld ", factorial(i) / (factorial(c) * factorial(i -
c)));
printf("\n");
}
return 0;
}
long factorial(int n) {
int c;
long result = 1;
for (c = 1; c <= n; c++) result = result * c;
return result;
}
5B. C-Program on Pyramid of Numbers
#include <stdio.h>
int main() {
int i, space, rows, k = 0, count = 0, count1 = 0;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; ++i) {
for (space = 1; space <= rows - i; ++space) {
printf(" ");
++count;
}
Page 24 of 50
BCA PRACTICAL QUESTION BANK,OSMANIA UNIVERSITY -2023

while (k != 2 * i - 1) {
if (count <= rows - 1) {
printf("%d ", i + k);
++count;
} else {
++count1;
printf("%d ", (i + k - 2 * count1));
}
++k;
}
count1 = count = k = 0;
printf("\n");
}
return 0;
}
6A. Recursion: C-Program to find factorial of a number.
#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;

Page 25 of 50
BCA PRACTICAL QUESTION BANK,OSMANIA UNIVERSITY -2023

}
long int multiplyNumbers(int n) {
if (n>=1)
return n*multiplyNumbers(n-1);
else
return 1;
}
OUTPUT:
Enter a positive integer: 6
Factorial of 6 = 720

6B. Recursion:Fibonacci Series up to n terms


#include<stdio.h>
void printFibonacci(int n){
static int n1=0,n2=1,n3;
if(n>0){
n3 = n1 + n2;
n1 = n2;
n2 = n3;
printf("%d ",n3);
printFibonacci(n-1);
}
}
int main(){

Page 26 of 50
BCA PRACTICAL QUESTION BANK,OSMANIA UNIVERSITY -2023

int n;
printf("Enter the number of elements: ");
scanf("%d",&n);
printf("Fibonacci Series: ");
printf("%d %d ",0,1);
printFibonacci(n-2);//n-2 because 2 numbers are already printed
return 0;
}
OUTPUT:
Enter the number of elements:15
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
6C. Recursion: C-Program for finding GCD of two numbers
#include <stdio.h>
int hcf(int n1, int n2);
int main() {
int n1, n2;
printf("Enter two positive integers: ");
scanf("%d %d", &n1, &n2);
printf("G.C.D of %d and %d is %d.", n1, n2, hcf(n1, n2));
return 0;
}

int hcf(int n1, int n2) {


if (n2 != 0)

Page 27 of 50
BCA PRACTICAL QUESTION BANK,OSMANIA UNIVERSITY -2023

return hcf(n2, n1 % n2);


else
return n1;
}
OUTPUT:
Enter two positive integers: 366 and 60
G.C.D of 366 and 60 is 6.
7.Finding the maximum, minimum, average and standard deviation of given set
of numbers using arrays
A.Finding the maximum and minimum of arrays elements
#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++)
{
Page 28 of 50
BCA PRACTICAL QUESTION BANK,OSMANIA UNIVERSITY -2023

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;
}

7B. Calculate mean(Average) and standard deviation .


#include <stdio.h>
#include <math.h>
void main() {
float x [50]; /* max. 50 elements in array x */
int n; /*number of elements in array x */
float sum, mean, sd; /* sum, mean and standard deviation.*/
int i;
clrscr();
/* read data */
printf("How many numbers (max 50)? ");
scanf("%d", &n);
printf ("Enter numbers: ");
for(i = 0; i<n; i++)

Page 29 of 50
BCA PRACTICAL QUESTION BANK,OSMANIA UNIVERSITY -2023

scanf("%f", &x[i]);
/* calculate mean */
sum= 0.0;
for(i = 0; i < n; i++)
sum+= x[i];
mean = sum / n;
/* calculate standard deviation */
sum= 0.0;
for(i = 0; i < n; i++)
sum+= (x[i] - mean) * (x[i] - mean);
sd = sqrt(sum / n);
printf("Mean = %6.3f\n", mean);
printf("Standard Deviation: %6.3f\n", sd);
getch();
}
8. Reversing an array, removal of duplicates from array
8A. Reversing an array.
#include <stdio.h>
#include <stdlib.h>
#define n 6
int main(){
int arr[n] = {9, 8, 7, 2, 4, 3};
int temp;
for(int i = 0; i<n/2; i++){

Page 30 of 50
BCA PRACTICAL QUESTION BANK,OSMANIA UNIVERSITY -2023

temp = arr[i];
arr[i] = arr[n-i-1];
arr[n-i-1] = temp;
}
for(int i = 0; i < n; i++){
printf("%d,", arr[i]);
}
}
8B. Removal of Duplicate elements in an array
#include <stdio.h>
#include <conio.h>
int main ()
{
// declare local variables
int arr[20], i, j, k, size;

printf (" Define the number of elements in an array: ");


scanf (" %d", &size);

printf (" \n Enter %d elements of an array: \n ", size);


// use for loop to enter the elements one by one in an array
for ( i = 0; i < size; i++)
{
scanf (" %d", &arr[i]);

Page 31 of 50
BCA PRACTICAL QUESTION BANK,OSMANIA UNIVERSITY -2023

}
// use nested for loop to find the duplicate elements in array
for ( i = 0; i < size; i ++)
{
for ( j = i + 1; j < size; j++)
{
// use if statement to check duplicate element
if ( arr[i] == arr[j])
{
// delete the current position of the duplicate element
for ( k = j; k < size - 1; k++)
{
arr[k] = arr [k + 1];
}
// decrease the size of array after removing duplicate element
size--;
// if the position of the elements is changes, don't increase the index j
j--;
}
}
}
/* display an array after deletion or removing of the duplicate elements */
printf (" \n Array elements after deletion of the duplicate elements: ");
// for loop to print the array

Page 32 of 50
BCA PRACTICAL QUESTION BANK,OSMANIA UNIVERSITY -2023

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


{
printf (" %d \t", arr[i]);
}
return 0;
}
9. Matrix addition, multiplication and transpose of a square matrix.
using functions
#include <stdio.h>
int rows, columns;
/* adds two matrices and stores the output in third matrix */
void matrixAddition(int mat1[][10], int mat2[][10], int mat3[][10]) {
int i, j;
for (i = 0; i < rows; i++) {
for (j = 0; j < columns; j++) {
mat3[i][j] = mat1[i][j] + mat2[i][j];
}
}
return;
}
int main() {
int matrix1[10][10], matrix2[10][10];
int matrix3[10][10], i, j;

Page 33 of 50
BCA PRACTICAL QUESTION BANK,OSMANIA UNIVERSITY -2023

/* get the number of rows and columns from user */


printf("Enter the no of rows and columns(<=10):");
scanf("%d%d", &rows, &columns);
if (rows > 10 || columns > 10) {
printf("No of rows/columns is greater than 10\n");
return 0;
}
/* input first matrix */
printf("Enter the input for first matrix:");
for (i = 0; i < rows; i++) {
for (j = 0; j < columns; j++) {
scanf("%d", &matrix1[i][j]);
}
}
/* input second matrix */
printf("Enter the input for second matrix:");
for (i = 0; i < rows; i++) {
for (j = 0; j < columns; j++) {
scanf("%d", &matrix2[i][j]);
}
}
/* matrix addtion */
matrixAddition(matrix1, matrix2, matrix3);

Page 34 of 50
BCA PRACTICAL QUESTION BANK,OSMANIA UNIVERSITY -2023

/* print the results */


printf("\nResult of Matrix Addition:\n");
for (i = 0; i < rows; i++) {
for (j = 0; j < columns; j++) {
printf("%5d", matrix3[i][j]);
}
printf("\n");
}
return 0;
}
OUTPUT:
Enter the no of rows and columns(<=10):
3 3
Enter the input for first matrix:
10 20 30
40 54 60
70 80 90

Enter the input for second matrix:


100 110 120
130 140 150
160 170 180

Page 35 of 50
BCA PRACTICAL QUESTION BANK,OSMANIA UNIVERSITY -2023

Result of Matrix Addition:


110 130 150
170 194 210
230 250 270

#include<stdio.h>
void multiply(int mat1[12][12],int mat2[12][12],int ,int ,int );
void main()
{
int mat1[12][12],mat2[12][12];
int i,j,k,m,n,p;
printf("Enter the number of rows and columns for 1st matrix\n");
scanf("%d%d",&m,&n);
printf("Enter the elements of the 1st matrix\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&mat1[i][j]);
}
}
//no of col of 1st mat = no of rows of 2nd mat
printf("Enter the number of columns for 2nd matrix\n");

Page 36 of 50
BCA PRACTICAL QUESTION BANK,OSMANIA UNIVERSITY -2023

scanf("%d",&p);
printf("Enter the elements of the 2nd matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<p;j++)
{
scanf("%d",&mat2[i][j]);
}
}
printf("The 1st matrix\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",mat1[i][j]);
}
printf("\n");
}
printf("The 2nd matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<p;j++)
{
printf("%d\t",mat2[i][j]);

Page 37 of 50
BCA PRACTICAL QUESTION BANK,OSMANIA UNIVERSITY -2023

}
printf("\n");
}
multiply(mat1,mat2,m,n,p);
}
void multiply(int mat1[12][12],int mat2[12][12],int m,int n,int p)
{
int mul[12][12],i,j,k;
for(i=0;i<m;i++)
{
for(j=0;j<p;j++)
{
mul[i][j]=0;
for(k=0;k<n;k++)
{
mul[i][j]=mul[i][j]+mat1[i][k]*mat2[k][j];
}
}
}
printf("The resultant matrix formed on multiplying the two matrices\n");
for(i=0;i<m;i++)
{
for(j=0;j<p;j++)
{

Page 38 of 50
BCA PRACTICAL QUESTION BANK,OSMANIA UNIVERSITY -2023

printf("%d\t",mul[i][j]);
}
printf("\n");
}
}

Program 2: Find the Transpose of a Matrix


In the below program, we will call a function to find the transpose of the matrix.

#include <stdio.h>
void transpose(int arr[10][10], int m, int n, int brr[10][10]) //Function
Definition
{
for(int i=0;i<m;i++) //Transpose Matrix initialization
{
for(int j=0;j<n;j++)
{
brr[j][i]=arr[i][j]; //Store elements in the transpose matrix
}
}
printf("\nAfter transpose the elements are...\n");
for(int i=0;i<m;i++) //Print the transpose matrix
{
for(int j=0;j<n;j++)
{
Page 39 of 50
BCA PRACTICAL QUESTION BANK,OSMANIA UNIVERSITY -2023

printf("%d ",brr[i][j]);
}
printf("\n");
}
}
int main()
{
int m,n; //Matrix Size Declaration
printf("Enter the number of rows and column: \n");
scanf("%d %d",&m,&n); //Matrix Size Initialization
int arr[10][10]; //Matrix Size Declaration
printf("\nEnter the elements of the matrix: \n");
for(int i=0;i<m;i++) //Matrix Initialization
{
for(int j=0;j<n;j++)
{
scanf("%d",&arr[i][j]);
}
}
printf("\nThe elements in the matrix are: \n");
for(int i=0;i<m;i++) //Print the matrix
{
for(int j=0;j<n;j++)
{

Page 40 of 50
BCA PRACTICAL QUESTION BANK,OSMANIA UNIVERSITY -2023

printf("%d ",arr[i][j]);
}
printf("\n");
}
int brr[10][10]; //Transpose Matrix Declaration
transpose(arr,m,n,brr); //Function Call
return 0;
}

Enter the number of rows and column: 3 3

Enter the elements of the matrix: 1 4 9 7 8 5 2 9 8


The elements in the matrix are:
149
785
298
After transpose the elements are...
172
489
958

10.Functions of string manipulation: inputting and outputting string,

Page 41 of 50
BCA PRACTICAL QUESTION BANK,OSMANIA UNIVERSITY -2023

using string functions such as strlen( ),strcat( ),strcpy( )………etc.


#include <stdio.h>
#include <string.h>
int main ()
{
char str1[12] = "Hello";
char str2[12] = "World";
char str3[12];
int len ;
/* copy str1 into str3 */
strcpy(str3, str1);
printf("strcpy( str3, str1) : %s\n", str3 );
/* concatenates str1 and str2 */
strcat( str1, str2);
printf("strcat( str1, str2): %s\n", str1 );
/* total lenghth of str1 after concatenation */
len = strlen(str1);
printf("strlen(str1) : %d\n", len );
return 0;
}
When the above code is compiled and executed, it produces result something as
follows:
strcpy( str3, str1) : Hello
strcat( str1, str2): HelloWorld
strlen(str1) : 10
Page 42 of 50
BCA PRACTICAL QUESTION BANK,OSMANIA UNIVERSITY -2023

11.Writing simple programs for strings without using string


functions.
11A.
#include <stdio.h>
int main ()
{
char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
printf("Greeting message: %s\n", greeting );
return 0;
}

OUTPUT:

Greeting message: Hello


11B. simple C-Program to Find the Frequency of a Character
#include <stdio.h>
int main() {
char str[1000], ch;
int count = 0;
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
printf("Enter a character to find its frequency: ");
scanf("%c", &ch);

Page 43 of 50
BCA PRACTICAL QUESTION BANK,OSMANIA UNIVERSITY -2023

for (int i = 0; str[i] != '\0'; ++i) {


if (ch == str[i])
++count;
}
printf("Frequency of %c = %d", ch, count);
return 0;
}
12.Finding the No. of characters, words and lines of given text file
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE * file;
char path[100];
char ch;
int characters, words, lines;
/* Input path of files to merge to third file */
printf("Enter source file path: ");
scanf("%s", path);
/* Open source files in 'r' mode */
file = fopen(path, "r");
/* Check if file opened successfully */
if (file == NULL)
{
Page 44 of 50
BCA PRACTICAL QUESTION BANK,OSMANIA UNIVERSITY -2023

printf("\nUnable to open file.\n");


printf("Please check if file exists and you have read privilege.\n");
exit(EXIT_FAILURE);
}
//Logic to count characters, words and lines.
characters = words = lines = 0;
while ((ch = fgetc(file)) != EOF)
{
characters++;
/* Check new line */
if (ch == '\n' || ch == '\0')
lines++;
/* Check words */
if (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\0')
words++;
}
/* Increment words and lines for last word */
if (characters > 0)
{
words++;
lines++;
}
/* Print file statistics */
printf("\n");

Page 45 of 50
BCA PRACTICAL QUESTION BANK,OSMANIA UNIVERSITY -2023

printf("Total characters = %d\n", characters);


printf("Total words = %d\n", words);
printf("Total lines = %d\n", lines);
/* Close files to release resources */
fclose(file);
return 0;
}
13.File handling programs : student memo printing.
#include<stdio.h>
#include<conio.h>
int k=0;
struct stud
{ int rn;
char name[30];
int m1,m2,m3,total;
float avg;
char grade,result;
} s[30];
void main()
{
int no,roll=101,i;
clrscr();
printf("Enter No of Students : ");
scanf("%d",&no);

Page 46 of 50
BCA PRACTICAL QUESTION BANK,OSMANIA UNIVERSITY -2023

for(i=0;i<no;i++)
{
clrscr();
s[k].rn=roll;
printf("\nEnter the Student Roll Number : %d ",s[k].rn);
printf("\nEnter the Student Name :");
fflush(stdin);
gets(s[k].name);
printf("\nEnter the Three Marks : ");
scanf("%d",&s[k].m1);
scanf("%d",&s[k].m2);
scanf("%d",&s[k].m3);
if(s[k].m1>=35 && s[k].m2>=35 && s[k].m3>=35)
{
s[k].result='P';
}
else
{
s[k].result = 'F';
}
s[k].total = s[k].m1+s[k].m2+s[k].m3;
printf("The Total is : %d",s[k].total);
s[k].avg=s[k].total/3;
if(s[k].avg>=60)

Page 47 of 50
BCA PRACTICAL QUESTION BANK,OSMANIA UNIVERSITY -2023

{
if(s[k].result == 'P')
{
s[k].grade = 'A';
}
else
{
s[k].grade = 'N';
}
}
else if(s[k].avg>=50)
{
if(s[k].result == 'P')
{
s[k].grade = 'B';
}
else
{
s[k].grade = 'N';
}
}
else if(s[k].avg>=35)
{
if(s[k].result == 'P')

Page 48 of 50
BCA PRACTICAL QUESTION BANK,OSMANIA UNIVERSITY -2023

{
s[k].grade = 'C';
}
else
{
s[k].grade = 'N';
}
}
getch();
k++;
roll++;
}

printf("\n*******************************************************");
printf("\n STUDENT MARKLIST ");

printf("\n*******************************************************");
printf("\nROLL \tNAME MARK1 MARK2 MARK3 TOTAL RESULT
Average GRADE");
for(i=0;i<no;i++)
{
printf("\n%d\t%s %d %d %d %d %c %0.1f
%c",s[i].rn,s[i].name,s[i].m1,s[i].m2,s[i].m3,s[i].total,s[i].result,s[i].avg,s[i].grade
);
}
getch();

Page 49 of 50
BCA PRACTICAL QUESTION BANK,OSMANIA UNIVERSITY -2023

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

KISHORE KUMAR G,
Department of Computer Science,
RKP Academy & OBS Degree College, Contact: +91 8019662404

Page 50 of 50

You might also like