0% found this document useful (0 votes)
7 views6 pages

C Programming Lab Exercises Guide

Bba file

Uploaded by

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

C Programming Lab Exercises Guide

Bba file

Uploaded by

jskarangill2007
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

s

Department of
Machine Exercise-4
Computer
Applications

Department of Computer
Applications

BCA 1st Semester

SUBJECT: Problem Solving using C Laboratory


SUBJECT CODE-BCA24105

Ms Anupreet
Kaur
Assistant
Professor
Department of Computer Applications
1. WRITE A PROGRAM to find the factorial of a given number.
#include <stdio.h> // Include the standard input/output header file.
#include<conio.h>
void main()
{

int i, f = 1, num; // Declare variables 'i' for loop counter, 'f' to store factorial, 'num' for user input

printf("Input the number : "); // Print a message to prompt user input.

scanf("%d", &num); // Read the value of 'num' from the user.

for(i = 1; i <= num; i++) // Start a loop to calculate factorial.


f = f * i; // Calculate factorial.

printf("The Factorial of %d is: %d\n", num, f); // Print the calculated factorial.
getch();
}

Output:
Input the number : 4
The Factorial of 4 is: 24

2. WRITE A PROGRAM to print the sum of two matrices

#include <stdio.h>
#include<conio.h>
int main ()
{
int mat1[3][3] = { {0, 1, 2}, {3, 4, 5}, {6, 7, 8} };
int mat2[3][3] = { {9, 10, 11}, {12, 13, 14}, {15, 16, 17} };
int sum[3][3], i, j;

printf ("matrix 1 is :\n");


for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
printf ("%d ", mat1[i][j]);
if (j == 3 - 1)
{
printf ("\n\n");
}
}
}
printf ("matrix 2 is :\n");
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
printf ("%d ", mat2[i][j]);
if (j == 3 - 1)
{
printf ("\n\n");
}
}
}
// adding two matrices
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
{
sum[i][j] = mat1[i][j] + mat2[i][j];
}

// printing the sum 0f two matrices


printf ("\nSum of two matrices: \n");
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
printf ("%d ", sum[i][j]);
if (j == 3 - 1)
{
printf ("\n\n");
}
}
}
getch();
return 0;
}
Output:

matrix 1 is :
0 1 2

3 4 5

6 7 8

matrix 2 is :
9 10 11

12 13 14

15 16 17
Sum of two matrices:
9 11 13
15 17 19
21 23 25

3. WRITE A PROGRAM to Find the Length of a String


#include <stdio.h>
#include<conio.h>
#include <string.h>
void main()
{
char str[100];
printf("Enter a string: ");
scanf("%s", str);
int len = strlen(str);
printf("The length of the string is: %d\n", len);
getch();
}

Output:
Enter a string: helloworld
The length of the string is: 10

4. WRITE A PROGRAM to Copy String using strcpy().


#include<stdio.h>
#include<string.h>
int main()
{
char source[] = "C Program";
char destination[50];
strcpy(destination, source);
printf("Source string: %s\n", source);
printf("Destination string: %s\n", destination);
getch();
return 0;
}

Output:-
Source string: C Program
Destination string: C Program

5. WRITE A PROGRAM to compare a string.

#include <stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
char str1[20]; // declaration of char array
char str2[20]; // declaration of char array
int value; // declaration of integer variable
printf("Enter the first string : ");
scanf("%s",str1);
printf("Enter the second string : ");
scanf("%s",str2);
// comparing both the strings using strcmp() function
value=strcmp(str1,str2);
if(value==0)
printf("strings are same");
else
printf("strings are not same");
getch();
return 0;
}

Output
Enter the first string : hello
Enter the second string : hello
strings are same

6. WRITE A PROGRAM to reverse a string

#include <stdio.h>
#include<conio.h>
#include <string.h>
int main()
{
char s[100]; //string declaration
printf("Enter a string:");
gets(s); //input
strrev(s); //reversing string
printf("The reverse of the string: %s\n", s);
getch();
return 0;
}

Output:
Enter a string: hello
The reverse of the string:olleh

7. WRITE A PROGRAM to multiply two numbers using pointers


#include<stdio.h>
#include<conio.h>
void main()
{
int num1,num2,mul,*p1,*p2;
clrscr();
printf("Enter 1st number: ");
scanf("%d",&num1);
printf("Enter 2nd number: ");
scanf("%d",&num2);
p1=&num1;
p2=&num2;
mul=*p1**p2;
printf("\n\nProduct of %d and %d is %d",*p1,*p2,mul);
getch();
}

Output:
Enter 1st number: 4
Enter 2nd number: 5
Product of 4 and 5 is 20

You might also like