Index: S.No Name of Practical Remarks S.No

Download as pdf or txt
Download as pdf or txt
You are on page 1of 54

INDEX

S.NO NAME OF PRACTICAL Remarks

1 A) Introduction to C and installing Dev C++

B) Running First C program.

C) WAP to show the sum of three numbers.

D) WAP to compute the Ascii value of a character.

2 A) WAP to take multiple inputs from the user.

B) WAP to use getchar() and putchar() functions

C) WAP to use gets() and puts() functions

3 i. Write an algorithm and draw a flowchart to determine a student’s final


grade and 

indicate whether it is passing or failing. The final grade is calculated as
the average of four marks.

ii. Write an algorithm, pseudocode, and draw a flowchart that will read
the two sides of a rectangle and calculate its area.

4 WAP to nd the min or max from 3 numbers

5 WAP to nd to nd the average of marks obtained by student


and display grade/division of a student using switch statement.

6 WAP to use switch statement to print Monday to Sunday

7 WAP to print whether a given no is even or odd

8 Write a C program to input electricity unit charge and calculate


the total electricity bill 

according to the given condition:

For rst 50 units Rs. 0.50/unit

For next 100 units Rs. 0.75/unit

For next 100 units Rs. 1.20/unit

For unit above 250 Rs. 1.50/unit

An additional surcharge of 20% is added to the bill. How to calculate
electricity bill using if else in c .

9 Write a Program to Check Whether a Number is Prime or not .

10 Write a program to print positive integers from 1 to 10


11 Write a program to generate Fibonacci series.

12 Write a program to reverse a given integer.

12 Write a program to check whether a number is Palindrome or not . 


14 Write a program to calculate factorial of a number using recursion and


iteration method.
fi
fi
fi
fi
S.NO NAME OF PRACTICAL Remarks

15 Write a a program to print the triangle of stars as follows (take number


of lines from user)
*
**
* **
* ** *
16 Write a program to insert 5 elements into an array and display the
elements of the array

17 Write a program to display the reverse of an array.

18 Write a C program to Display Max Element of an array.

19 Write a Program to access an element in 2-D Array.

20 Write a program to nd the sum of the element a matrix.

21 Write a program to nd the transpose of the element a matrix.

22 Write a Program to multiply two 3 X 3 Matrices.

23 Write a program to swap two integers using call by value and call by
reference methods of passing arguments to a function.

24 Write a menu driven program to perform following operations on strings


(using String class, functions): a) Concatenate two strings b) Compare
two strings. c) Calculate the length of the string .

25 WAP to that inputs & displays the information of students using


structures.

26 Create a le that contains records of 5 employees. Each record contains


an employee’s
salary details. Print the information retrieved from the le in the following
format: Employee ID , Name ,Basic, HRA ,DA ,ExtraAllowances .



fi

fi
fi
fi
Practical - 1
A) Introduction to C and installing Dev C++
Introduction to C :-

  C is a general-purpose programming language that is extremely popular, simple, and


exible to use. It is a structured programming language that is machine-independent and
extensively used to write various applications, Operating Systems like Windows, and many
other complex programs like Oracle database, Git, Python interpreter, and more.
It is said that ‘C’ is a god’s programming language. One can say, C is a base for the
programming. If you know ‘C,’ you can easily grasp the knowledge of the other
programming languages that uses the concept of ‘C’
It is essential to have a background in computer memory mechanisms because it is an
important aspect when dealing with the C programming language.

Where is C used? Key Applications

1. ‘C’ language is widely used in embedded systems.


2. It is used for developing system applications.
3. It is widely used for developing desktop applications.
4. Most of the applications by Adobe are developed using ‘C’ programming language.
5. It is used for developing browsers and their extensions. Google’s Chromium is built
using ‘C’ programming language.
6. It is used to develop databases. MySQL is the most popular database software
which is built using ‘C’.
7. It is used in developing an operating system. Operating systems such as Apple’s
OS X, Microsoft’s Windows, and Symbian are developed using ‘C’ language. It is
used for developing desktop as well as mobile phone’s operating system.
8. It is used for compiler production.
9. It is widely used in IOT applications.
Why learn C Language?

As we studied earlier, ‘C’ is a base language for many programming languages. So,
learning ‘C’ as the main language will play an important role while studying other
programming languages. It shares the same concepts such as data types, operators,
control statements and many more. ‘C’ can be used widely in various applications. It is a
simple language and provides faster execution. There are many jobs available for a ‘C’
developer in the current market.
‘C’ is a structured programming language in which program is divided into various
modules. Each module can be written separately and together it forms a single ‘C’
program. This structure makes it easy for testing, maintaining and debugging processes.
‘C’ contains 32 keywords, various data types and a set of powerful built-in functions that
make programming very ef cient.
Another feature of ‘C’ programming is that it can extend itself. A ‘C’ program contains
various functions which are part of a library. We can add our features and functions to the
library. We can access and use these functions anytime we want in our program. This
feature makes it simple while working with complex programming.
fl

fi

Various compilers are available in the market that can be used for executing programs
written in this language.
It is a highly portable language which means programs written in ‘C’ language can run on
other machines. This feature is essential if we wish to use or execute the code on another
computer.

Installation of Dev C++


What is Dev-C++?
Dev-C++, developed by Bloodshed Software, is a fully featured graphical IDE
(Integrated Development Environment), which is able to create Windows or console-
based C/C++ programs using the MinGW compiler system. MinGW (Minimalist
GNU* for Windows) uses GCC (the GNU g++ compiler collection), which is
essentially the same compiler system that is in Cygwin (the unix environment
program for Windows) and most versions of Linux. There are, however, differences
between Cygwin and MinGW; link to Differences between Cygwin and MinGW for
more information.

Click picture to enlarge.

Getting Dev-C++
The author has released Dev-C++ as free software (under GPL) but also offers a CD
for purchase which can contain all Bloodshed software (it's customizable), including
Dev-C++ with all updates/patches.

Link to Bloodshed Dev-C++ for a list of Dev-C++ download sites.

You should let the installer put Dev-C++ in the default directory of C:\Dev-Cpp, as it
will make it easier to later install add-ons or upgrades.


Using Dev-C++
This section is probably why you are here.

All programming done for CSCI-2025 will require separate compilation projects (i.e.
class header le(s), class implementation le(s) and a main/application/client/driver
le). This process is relatively easy as long as you know what Dev-C++ requires to
do this. In this page you will be given instructions using the Project menu choice. In
another handout you will be given instructions on how to manually compile, link and
execute C++ les at the command prompt of a command window. See here.

Step 1: Con gure Dev-C++.


We need to modify one of the default settings to allow you to use the debugger with
your programs.

• Go to the "Tools" menu and select "Compiler Options".


• In the "Settings" tab, click on "Linker" in the left panel, and change
"Generate debugging information" to "Yes":

• Click "OK".

Step 2: Create a new project.


A "project" can be considered as a container that is used to store all the elements that
are required to compile a program.

• Go to the "File" menu and select "New", "Project...".


• Choose "Empty Project" and make sure "C++ project" is selected.
Here you will also give your project a name. You can give your project any
fi
fi
fi
fi



fi


valid lename, but keep in mind that the name of your project will also be the
name of your nal executable.
• Once you have entered a name for your project, click "OK".
• Dev-C++ will now ask you where to save your project.

Step 3: Create/add source le(s).


You can add empty source les one of two ways:

• Go to the "File" menu and select "New Source File" (or just press
CTRL+N) OR
• Go to the "Project" menu and select "New File".
Note that Dev-C++ will not ask for a lename for any new source le until you
attempt to:
1. Compile
2. Save the project
3. Save the source le
4. Exit Dev-C++

You can add pre-existing source les one of two ways:
• Go to the "Project" menu and select "Add to Project" OR
• Right-click on the project name in the left-hand panel and select "Add to
Project".
EXAMPLE: Multiple source files

In this example, more than 3 files are required to compile the program; The "driver.cpp" file references "Deque.h" (which
requires "Deque.cpp") and "Deque.cpp" references "Queue.h" (which requires "Queue.cpp").
fi
fi
fi
fi
fi
fi

fi

fi
Step 4: Compile.
Once you have entered all of your source code, you are ready to compile.

• Go to the "Execute" menu and select "Compile" (or just press


CTRL+F9).It is likely that you will get some kind of compiler or linker error
the rst time you attempt to compile a project. Syntax errors will be displayed
in the "Compiler" tab at the bottom of the screen. You can double-click on
any error to take you to the place in the source code where it occurred. The
"Linker" tab will ash if there are any linker errors. Linker errors are
generally the result of syntax errors not allowing one of the les to compile.
Once your project successfully compiles, the "Compile Progress" dialog box
will have a status of "Done". At this point, you may click "Close".
Step 5: Execute.
You can now run your program.

• Go to the "Execute" menu, choose "Run".


Note: to pass command-line parameters to your program, go to the "Execute"
menu, choose "Parameters" and type in any paramaters you wish to pass.
Disappearing windows
If you execute your program (with or without parameters), you may notice something
peculiar; a console window will pop up, ash some text and disappear. The problem
is that, if directly executed, console program windows close after the program exits.
You can solve this problem one of two ways:

• Method 1 - Adding one library call:


On the line before the main's return enter:system(“Pause");


• Method 2 - Scaffolding:
Add the following code before any return statement in main() or
any exit() or abort() statement (in any function):
/* Scaffolding code for testing purposes */
• cin.ignore(256, '\n');
• cout << "Press ENTER to continue..." << endl;
• cin.get();
• /* End Scaffolding */

This will give you a chance to view any output before the program terminates
and the window closes.


fi


fl



fl

fi
• Method 3 - Command-prompt:
Alternatively, instead of using Dev-C++ to invoke your program, you can just
open an MS-DOS Prompt, go to the directory where your program was
compiled (i.e. where you saved the project) and enter the program name (along
with any parameters). The command-prompt window will not close when the
program terminates.
For what it's worth, I use the command-line method.

Step 6: Debug.
When things aren't happening the way you planned, a source-level debugger can be a
great tool in determining what really is going on. Dev-C++'s basic debugger
functions are controlled via the "Debug" tab at the bottom of the screen; more
advanced functions are available in the "Debug" menu.

Using the debugger:


The various features of the debugger are pretty obvious. Click the "Run to
cursor" icon to run your program and pause at the current source code cursor
location; Click "Next Step" to step through the code; Click "Add Watch" to
monitor variables.
Setting breakpoints is as easy as clicking in the black space next to the line in the
source code.
See the Dev-C++ help topic "Debugging Your Program" for more information.


B) Running First C program
Code :-

#include <stdio.h>

int main() {

printf("my name is aryamnn sablok and i am going to learn c ");

return 0;
}

Output :-

C) WAP to show the sum of three numbers.

Code :-
#include <stdio.h>

int main()
{

int x, y, z, sum;
scanf("%d%d%d",&x,&y,&z);
sum=x+y+z;
printf("\n Sum of Three Number is = %d", sum);
return 0;

Output :-

D) WAP to compute the Ascii value of a character

Code :-
#include <stdio.h>
int main() {
char c;
printf("Enter a character: ");
scanf("%c", &c);

printf("ASCII value of %c = %d", c, c);

return 0;
}

Output :-

Practical - 2
A) WAP to take multiple inputs from the user

Code :-
#include <stdio.h>
int main()
{
oat num1;
double num2;

printf("Enter a number: ");


scanf("%f", &num1);
printf("Enter another number: ");
scanf("%lf", &num2);

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


printf("num2 = %lf", num2);

return 0;
}

Output :-
fl

B) WAP to use getchar() and putchar() functions

Code :-
#include <stdio.h>
#include <stdlib.h>
main( )
{
char ch;
char s[10];
int i=0;
printf("Enter a number or string\n");
while((ch=getchar())!='\n')
{
s[i]=ch;
i++;
}
printf("Original Number/String: %s\n",s);
printf("Reverse Number/String: ");
while(--i>=0)
{
putchar(s[i]);
}
return 0;
}
Output :-

C). WAP to use gets() and puts() functions.

Code :-
#include<stdio.h>
#include <string.h>
 
void main()
{
char day[10];
printf("Enter current week day: \n");
gets(day);
 
printf("Today is: ");
puts(day);
}

Output :-

Practical - 3

i. Write an algorithm and draw a owchart to


determine a student’s nal grade and 

indicate whether it is passing or failing. The
nal grade is calculated as the average of four
marks.


Pseudocode :

• Input set of 4 marks

• Calculate there average by summing and dividing by 4

• If the average is below 40


Print “fail”
else print”pass”
Endif

Algorithm :

• Input S1, S2, S3, S4

• GRADE < (S1+S2+S3+S4)/4

• if ( GRADE > 40 ) THEN


PRINT “PASS”
else
PRINT “FAIL”
End if

fi
fi
fl
ii. Write an algorithm, pseudocode, and draw a
owchart that will read the two sides of a
rectangle and calculate its area.

Pseudocode :
• Input the width (W) and length (L) of a rectangle
• Calculate the area (a) by multiplying L with B
• Print A

Algorithm :
Step 1: input B,L
Step 2 : A= L X B
Step 3 : print A
fl
Practical - 4

WAP to nd the min or max from 3 number. 


Code :-
#include <stdio.h>

int main(void) {

int i,max,min,N,x;

printf("Enter N : ");

scanf("%d",&N);

max=0;

min=999;

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

printf("Enter x-%d : ",i);

scanf("%d",&x);

if(max < x)

max = x;

if(min > x)

min = x;

printf("\n max = %d",max);

printf("\n min = %d",min);

return 0;

Output :-
fi
Practical - 5

WAP to nd to nd the average of marks obtained by


student and display grade/division of a student using
switch statement .


Code :-

#include<stdio.h>
int main()
{
int score;

printf("Enter score( 0-100 ): ");


scanf("%d", &score);

switch( score / 10 )
{

case 10:
case 9:
printf("Grade: A");
break;

case 8:
printf("Grade: B");
break;

case 7:
printf("Grade: C");
break;

case 6:
printf("Grade: D");
break;
fi
fi
case 5:
printf("Grade: E");
break;

default:
printf("Grade: F");
break;

return 0;
}

Output :-
Practical - 6

WAP to use switch statement to print Monday to


Sunday 


Code :-

#include <stdio.h>

int main()

int week;

/* Input week number from user */

printf("Enter week number(1-7): ");

scanf("%d", &week);

switch(week)

case 1:

printf("Monday");

break;

case 2:

printf("Tuesday");

break;

case 3:

printf("Wednesday");

break;

case 4:

printf("Thursday");

break;

case 5:

printf("Friday");

break;

case 6:

printf("Saturday");

break;

case 7:

printf("Sunday");

break;

default:

printf("Invalid input! Please enter week number between 1-7.");

return 0;

Output :-
Practical - 7

WAP to print whether a given no is even or odd. 


Code :-

#include <stdio.h>
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);

(num % 2 == 0) ? printf("%d is even.", num) : printf("%d is odd.",


num);
return 0;
}

Output :-
Practical - 8

Write a C program to input electricity unit charge and


calculate the total electricity bill
according to the given condition:
For first 50 units Rs. 0.50/unit
For next 100 units Rs. 0.75/unit
For next 100 units Rs. 1.20/unit
For unit above 250 Rs. 1.50/unit
An additional surcharge of 20% is added to the bill. How to
calculate electricity bill using else in c

Code :-

#include <stdio.h>

int main()
{
int unit;
oat amt, total_amt, sur_charge;

/* Input unit consumed from user */


printf("Enter total units consumed: ");
scanf("%d", &unit);

/* Calculate electricity bill according to given conditions */


if(unit <= 50)
{
amt = unit * 0.50;
}
else if(unit <= 150)
{

fl







amt = 25 + ((unit-50) * 0.75);
}
else if(unit <= 250)
{
amt = 100 + ((unit-150) * 1.20);
}
else
{
amt = 220 + ((unit-250) * 1.50);
}

/*
* Calculate total electricity bill
* after adding surcharge
*/
sur_charge = amt * 0.20;
total_amt = amt + sur_charge;

printf("Electricity Bill = Rs. %.2f", total_amt);

return 0;
}

Output :-
Practical - 9
WAP to Check Whether a Number is Prime or not.

Code :-

#include <stdio.h>

int main() {

int n, i, ag = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);

if (n == 0 || n == 1)
ag = 1;

for (i = 2; i <= n / 2; ++i) {


if (n % i == 0) {
ag = 1;
break;
}
}
if ( ag == 0)
printf("%d is a prime number.", n);
else
printf("%d is not a prime number.", n);

return 0;
}

Output :-
fl
fl
fl
fl
Practical - 10
WAP to print positive integers from 1 to 10

Code :-
#include<stdio.h>
int main()
{
int Size, i, a[10];

printf("\n Please Enter the Size of an Array : ");


scanf("%d", &Size);
printf("\n Please Enter the Array Elements : ");
for(i = 0; i < Size; i++)
{
scanf("%d", &a[i]);
}

printf("\n List of Positive Numbers in this Array : ");


for(i = 0; i < Size; i ++)
{
if(a[i] >= 0)
{
printf("%d ", a[i]);
}
}
return 0;
}
Output :-
Practical - 11
WAP to generate Fibonacci series.

Code :-
#include <stdio.h>
int main() {

int i, n;

int t1 = 0, t2 = 1;

int nextTerm = t1 + t2;

printf("Enter the number of terms: ");


scanf("%d", &n);
printf("Fibonacci Series: %d, %d, ", t1, t2);

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


printf("%d, ", nextTerm);
t1 = t2;
t2 = nextTerm;
nextTerm = t1 + t2;
}

return 0;
}
Output :-
Practical - 12
WAP to reverse a given integer .


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

Output :-
Practical - 13
WAP to check whether a number is Palindrome or not


Code :-
#include <stdio.h>
int main() {
int n, reversed = 0, remainder, original;
printf("Enter an integer: ");
scanf("%d", &n);
original = n;
while (n != 0) {
remainder = n % 10;
reversed = reversed * 10 + remainder;
n /= 10;
}

if (original == reversed)
printf("%d is a palindrome.", original);
else
printf("%d is not a palindrome.", original);

return 0;
}

Output :-
Practical - 14
WAP to calculate factorial of a number using recursion
and iteration method.

Code :-

#include<stdio.h>
long int fact(int n);
long int Ifact(int n);

int main( )
{
int num;
printf("Enter a number : ");
scanf("%d", &num);

printf("\nUsing Recursion :: \n");


if(num<0)
printf("No factorial for negative number\n");
else
printf("Factorial of %d is %ld\n", num, fact(num) );

printf("\nUsing Iterative :: \n");

if(num<0)
printf("No factorial for negative number\n");
else
printf("Factorial of %d is %ld\n", num, Ifact(num) );

return 0;
}/*End of main()*/

/*Recursive*/
long int fact(int n)
{
if(n == 0)
return(1);
return(n * fact(n-1));
}/*End of fact()*/

/*Iterative*/
long int Ifact(int n)
{
long fact=1;
while(n>0)
{
fact = fact*n;
n--;
}
return fact;
}/*End of ifact()*/

Output :-
Practical - 15
WAP to print the triangle of stars as follows (take
number of lines from user)

*
**
* **
* ** *
Code :-
#include <stdio.h>
int main() {
int i, j, rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; ++i) {
for (j = 1; j <= i; ++j) {
printf("* ");
}
printf("\n");
}
return 0;
}

Output :-



Practical - 16

WAP to insert 5 elements into an array and display the


elements of the array

Code :-

#include <stdio.h>

int main()
{
int arr[100] = { 0 };
int i, x, pos, n = 10;

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


arr[i] = i + 1;

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


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

x = 50;

pos = 5;

n++;

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


arr[i] = arr[i - 1];

arr[pos - 1] = x;
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");

return 0;
}

Output :-
Practical - 17

WAP to display the reverse of an array.

Code :-
#include <stdio.h>
#include <stdlib.h>
#de ne n 6
int main(){
int arr[n] = {9, 8, 7, 2, 4, 3};
int temp;
for(int i = 0; i<n/2; i++){
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]);
}
}

Output :-
fi
Practical - 18
WAP to Display Max Element of an array.

Code :-
#include <stdio.h>

int main() {

int n;

double arr[100];

printf("Enter the number of elements (1 to 100): ");

scanf("%d", &n);

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

printf("Enter number%d: ", i + 1);

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

for (int i = 1; i < n; ++i) {

if (arr[0] < arr[i]) {

arr[0] = arr[i];

printf("Largest element = %.2lf", arr[0]);

return 0;

Output :-
Practical - 19
WAP to access an element in 2-D Array.

Code :-
#include<stdio.h> 

int main() {

   int i, j, a[3][3];

  

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

      for (j = 0; j < 3; j++) {

         printf("\nEnter the a[%d][%d] = ", i, j);

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

      }

   }

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

      for (j = 0; j < 3; j++) {

         printf("%d\t", a[i][j]);

      }

      printf("\n");

   }

   return (0);

Output :-
Practical - 20

WAP to nd the sum of the element a matrix.

Code :-

#include<stdio.h>
int main()
{
int a[10][10],r,c,sum=0,i,j;
printf("/*How Many Rows You Want To \nEnter in Matrix*/\nEnter Limit : ");
scanf("%d",&r);
printf("\n/*How Many Columns You Want To \nEnter in Matrix*/\nEnter Limit :
");
scanf("%d",&c);
printf("\nEnter Elements for Matrix of Size %d*%d:\n\n",r,c);
for(i=0;i<r;i++)
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
printf("\n%d*%d Matrix : \n\n",r,c);
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%2d ",a[i][j]);
}
printf("\n");
}
for(i=0;i<r;i++)
for(j=0;j<c;j++)
sum=sum+a[i][j];
printf("\nSum of All Elements in Matrix = %d",sum);
return 0;
fi
}

Output :-
Practical - 21

WAP to nd the transpose of the element a matrix.

Code :-

#include <stdio.h>

int main()
{
int m,n;
printf("Enter the number of rows and column: \n");
scanf("%d %d",&m,&n);
int arr[10][10];
printf("\nEnter the elements of the matrix: \n");
for(int i=0;i<m;i++)
{
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++)
{
for(int j=0;j<n;j++)
{
printf("%d ",arr[i][j]);
}
printf("\n");
}
int brr[10][10];
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
fi
{
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++)
{
printf("%d ",brr[i][j]);
}
printf("\n");
}
return 0;
}

Output :-
Practical - 22

WAP to multiply two 3 X 3 Matrices.

Code :-
#include <stdio.h>

void getMatrixElements(int matrix[][10], int row, int column) {

printf("\nEnter elements: \n");

for (int i = 0; i < row; ++i) {


for (int j = 0; j < column; ++j) {
printf("Enter a%d%d: ", i + 1, j + 1);
scanf("%d", &matrix[i][j]);
}
}
}

void multiplyMatrices(int rst[][10],


int second[][10],
int result[][10],
int r1, int c1, int r2, int c2) {

for (int i = 0; i < r1; ++i) {


for (int j = 0; j < c2; ++j) {
result[i][j] = 0;
}
}

for (int i = 0; i < r1; ++i) {


for (int j = 0; j < c2; ++j) {
for (int k = 0; k < c1; ++k) {
result[i][j] += rst[i][k] * second[k][j];
}
fi
fi

}
}
}

void display(int result[][10], int row, int column) {

printf("\nOutput Matrix:\n");
for (int i = 0; i < row; ++i) {
for (int j = 0; j < column; ++j) {
printf("%d ", result[i][j]);
if (j == column - 1)
printf("\n");
}
}
}

int main() {
int rst[10][10], second[10][10], result[10][10], r1, c1, r2, c2;
printf("Enter rows and column for the rst matrix: ");
scanf("%d %d", &r1, &c1);
printf("Enter rows and column for the second matrix: ");
scanf("%d %d", &r2, &c2);

while (c1 != r2) {


printf("Error! Enter rows and columns again.\n");
printf("Enter rows and columns for the rst matrix: ");
scanf("%d%d", &r1, &c1);
printf("Enter rows and columns for the second matrix: ");
scanf("%d%d", &r2, &c2);
}

multiplyMatrices( rst, second, result, r1, c1, r2, c2);

display(result, r1, c2);

return 0;
}
fi
fi
fi
fi
Output :-
Practical - 23

WAP to swap two integers using call by value and call


by reference methods of passing arguments to a
function.

Code :-
#include <stdio.h>

void swap(int num1, int num2)

int temp;

printf("In Function values before swapping: %d %d\n", num1, num2);

temp = num1;

num1 = num2;

num2 = temp;

printf("In Function values after swapping: %d %d\n\n", num1, num2);

int main()

int n1, n2;

printf("Enter two numbers: ");

scanf("%d%d", &n1, &n2);

printf("In Main values before swapping: %d %d\n\n", n1, n2);

swap(n1, n2);

printf("In Main values after swapping: %d %d", n1, n2);



return 0;

Output :-
Practical - 24

Write a menu driven program to perform following


operations on strings (using String class, functions): a)
Concatenate two strings b) Compare two strings. c)
Calculate the length of the string .

Code :-

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
char str1[20],str2[20];
int ch,i,j;
do
{
printf("\tMENU");
printf("\n------------------------------\n");
printf("1:Find Length of String");
printf("\n2:Concatenate Strings");
printf("\n3:Compare Strings");
printf("\n4:Exit");
printf("\n------------------------------\n");
printf("\nEnter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("Enter String: ");
scanf("%s",str1);
i=strlen(str1);
printf("Length of String : %d\n\n",i);
break;
case 2:
printf("\nEnter First String: ");
scanf("%s",str1);
printf("Enter Second string: ");
scanf("%s",str2);
strcat(str1,str2);
printf("String After Concatenation : %s\n\n",str1);
break;
case 5:
printf("Enter First String: ");
scanf("%s",str1);
printf("Enter Second String: ");
scanf("%s",str2);
j=strcmp(str1,str2);
if(j==0)
{
printf("Strings are Same\n\n");
}
else
{
printf("Strings are Not Same\n\n");
}
break;
case 4:
exit(0);
break;
default:
printf("Invalid Input. Please Enter valid Input.\n\n ");
}
}while(ch!=6);
return 0;
}
Output :-
Practical - 25

WAP to that inputs & displays the information of


students using structures.

Code :-

#include <stdio.h>
struct student {
char rstName[50];
int roll;
oat marks;
} s[5];

int main() {
int i;
printf("Enter information of students:\n");

// storing information
for (i = 0; i < 5; ++i) {
s[i].roll = i + 1;
printf("\nFor roll number%d,\n", s[i].roll);
printf("Enter rst name: ");
scanf("%s", s[i]. rstName);
printf("Enter marks: ");
scanf("%f", &s[i].marks);
}
printf("Displaying Information:\n\n");

// displaying information
for (i = 0; i < 5; ++i) {
printf("\nRoll number: %d\n", i + 1);
fl
fi
fi
fi
printf("First name: ");
puts(s[i]. rstName);
printf("Marks: %.1f", s[i].marks);
printf("\n");
}
return 0;
}

Output :-
fi
Practical - 26
Create a file that contains records of 5 employees. Each record
contains an employee’s
salary details. Print the information retrieved from the file in
the following format: Employee id Name Basic HRA DA
ExtraAllowances

Code :-

#include <stdio.h>
int main() {
char id[10];
int hour;
double value, salary;

printf("Input the Employees ID(Max. 10 chars): ");


scanf("%s", &id);

printf("\nInput the working hrs: “);

scanf("%d", &hour);
printf("\nSalary amount/hr: “);

scanf("%lf", &value);
salary = value * hour;
printf("\nEmployees ID = %s\nSalary = U$ %.2lf\n", id,salary);
return 0;
}

Output :-

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy