0% found this document useful (0 votes)
15 views14 pages

Pps Lab

The document contains multiple C programming examples, including swapping two integers, finding the largest element in an array, concatenating strings, storing and displaying student information using structures, performing matrix multiplication, finding character frequency in a string, and file operations using fputc, fgetc, fputs, and fgets. Each example is accompanied by code snippets and sample outputs. The programs demonstrate fundamental programming concepts and operations in C.

Uploaded by

rinkigaya1983
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)
15 views14 pages

Pps Lab

The document contains multiple C programming examples, including swapping two integers, finding the largest element in an array, concatenating strings, storing and displaying student information using structures, performing matrix multiplication, finding character frequency in a string, and file operations using fputc, fgetc, fputs, and fgets. Each example is accompanied by code snippets and sample outputs. The programs demonstrate fundamental programming concepts and operations in C.

Uploaded by

rinkigaya1983
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/ 14

1. Write a C program to perform swapping using function.

Program :

#include<stdio.h>

#include<conio.h>

void swap(int,int);

int main()

int a,b;

printf("enter value for a&b: ");

scanf("%d%d",&a,&b);

swap(a,b);

getch();

void swap(int a,int b)

int temp;

temp=a;

a=b;

b=temp;

printf("after swapping the value for a & b is : %d %d",a,b);

--------------------------------------------------------------------------------------------------
Output:

---------------------------------------------------------------------------------------------------

enter value for a&b: 4

after swapping the value for a & b : 5 4

2. Write a program in C to get the largest element of an array using function.

Program:

#include<stdio.h>

#include<stdlib.h>

int main()

int array[100], maximum, size, c, location = 1;

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

scanf("%d", &size);

printf("Enter %d integers\n", size);

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

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

maximum = array[0];

for (c = 1; c < size; c++)

if (array[c] > maximum)

maximum = array[c];

location = c+1;
}

printf("Maximum element is present at location %d and it's value is %d.\n", location,

maximum);

return 0;

}
-----------------------------------------------------------------------------------------------------------------------

Output:

--------------------------------------------------------------------------------------------------------------------------

Enter the number of elements in array

Enter 5 integers

Maximum element present at location 4 and its value is 8

3. Write a C program to concatenate two strings.

Program:

#include <stdio.h>

int main()

char s1[100], s2[100], i, j;

printf("Enter first string: ");

scanf("%s", s1);
printf("Enter second string: ");

scanf("%s", s2);

// calculate the length of string s1 and store it in i

for(i = 0; s1[i] != '\0'; ++i);

for(j = 0; s2[j] != '\0'; ++j, ++i)

s1[i] = s2[j];

s1[i] = '\0';

printf("After concatenation: %s", s1);

return 0;

-------------------------------------------------------------------------------------------------------------

Output

--------------------------------------------------------------------------------------------------------------

Enter first string: hi

Enter second string:welcome

After concatenation: Hi welcome

4. Write a C program to Store Student Information in Structure and Display it.

Program

printf("Displaying Information:\n\n");

// displaying information

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

{
printf("\nRoll number: %d\n",i+1);

printf("Name: ");

puts(s[i].name);

printf("Marks: %.1f",s[i].marks);

printf("\n");

return 0;

Output

--------------------------------------------------------------------------------------------------------------------------
Enter information of students:

For roll number1,

Enter name: Tom

Enter marks: 98

For roll number2,

Enter name: Jerry

Enter marks: 89

..

Displaying Information:

Roll number: 1

Name: Tom

Marks: 98

For roll number2,

Enter name: Jerry

Enter marks: 89
5. Write a program to compute matrix multiplication.

Program

#include <stdio.h>
int main()
{
int m, n, p, q, c, d, k, sum = 0;
int first[10][10], second[10][10], multiply[10][10];
printf("\nEnter the number of rows and columns of first matrix:\n");
scanf("%d%d", &m, &n);
/*//Entering elements of first matrix
printf("\n Enter the elements of first matrix\n");
for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
scanf("%d", &first[c][d]);*/
printf("\nEnter the number of rows and columns of second matrix:\n");
scanf("%d%d", &p, &q);
//Checking if Matrix Multiplication is possible
if ( n != p )
{
printf("\nMatrices with entered orders can't be multiplied with each other.\n");
printf("\nThe column of first matrix should be equal to row of second.\n");
}
else
{
//Entering elements of first matrix
printf("\nEnter the elements of first matrix:\n");
for ( c = 0 ; c < m ; c++ )
for ( d = 0 ; d < n ; d++ )
scanf("%d", &first[c][d]);
//Entering elements of second matrix
printf("\nEnter the elements of second matrix:\n");
for ( c = 0 ; c < p ; c++ )
for ( d = 0 ; d < q ; d++ )
scanf("%d", &second[c][d]);
//Carrying out matrix multiplication operation
for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < q ; d++ )
{
for ( k = 0 ; k < p ; k++ )
{
56
sum = sum + first[c][k]*second[k][d];
}
multiply[c][d] = sum;
sum = 0;
}
}
//Printing the final product matrix
printf("\nThe product of entered matrices is:\n");
for ( c = 0 ; c < m ; c++ )
{
for ( d = 0 ; d < q ; d++ )
printf("%d\t", multiply[c][d]);
printf("\n");
}
}
return 0;
}

Output
_-------------------------------------------------------------------------------------------------------------------
Enter the number of rows and columns of first matrix:
22

Enter the number of rows and columns of second matrix:


22

Enter the elements of first matrix:


2345

Enter the elements of second matrix:


3456

The product of entered matrices is:


21 26
37 46

6. Find the frequency of a character in a string.

Program.

int main()
{
char str[1000], ch;
int i, frequency = 0;
printf("Enter a string: ");
gets(str);
printf("Enter a character to find the frequency: ");
scanf("%c", &ch);
for(i = 0; str[i] != '\0'; ++i)
{
if(ch == str[i])
++frequency;
}
printf("Frequency of %c = %d", ch, frequency);
return 0;
}

Output
---------------------------------------------------------------------------------------------------------------------
Enter a string: This website is awesome.
Enter a character to find the frequency: e
Frequency of e = 4

7. Write a program to write into file using fputc.


Program

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

int main( )
{
int i;
FILE *fp;
char arr[115] = "welcome to c programming sec sasaram";

fp = fopen ("sec.txt", "w");


if (fp== NULL)

{
printf ("errror while opening file");

}
else {

printf("file opened successfully\n");

for ( i=0 ; i<strlen (arr) ; i ++)

{
fputc(arr[i], fp);
printf("%c ", arr[i]);

fclose (fp);
getch();
return 0;
}

Output

8.write a program to read data from file using fgetc function.

Program.

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

int main( )
{
char ch ;
FILE *fp;
char arr[115] = "welcome to c programming sec sasaram";

fp = fopen ("sec.txt", "r");


if (fp== NULL)

{
printf ("errror while opening file");

}
else {

printf("file opened successfully\n");

}
while( ch != EOF)

{
ch = fgetc(fp);
printf(" %c ", ch);

fclose (fp);
getch();
return 0;
}

Output

9.Write a program to enter string into file using fputs function.

Program.
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>

int main ()

{
FILE *fp ;
char input [200];
fp = fopen("sasaram.txt", "w");

if ( fp == NULL)
{

printf ( " error while opening file");

}
else{

printf ("file opened successfully\n");


printf("enter the input to the file\n");
scanf("%s", input);
gets(input);

fputs(input , fp);
printf(" string written to file successfully");

fclose(fp);
getch();
return 0;

}
Output
10 Write a program to read string from file using fgets fuction.

Program

#include<stdio.h>

#include<stdlib.h>

#include<conio.h>

int main ()

FILE *fp ;

char ch [200];

fp = fopen("sasaram.txt", "r");

if ( fp == NULL)

printf ( " error while opening file");

else{
printf ("file opened successfully\n");

while(!feof(fp))

fgets(ch , 200 , fp);

printf("%s",ch);

fclose(fp);

getch();

return 0;

Output

You might also like