You are on page 1of 59

Name: V.

Keerthika Roll No: 1975020


SRI PADMAVATI VISVAVIDYALAYAM, TIRUPATI
(Women’s University)
SCHOOL OF ENGINEERING AND TECHNOLOGY

Exp No: 01 Multiplication of two Matrices by Passing Matrix to a Date:04/02/2022


Function

AIM: To write a C Program to Multiply two Matrices by Passing Matrix to a Function.

SOURCE CODE:

#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");
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");


1
Name: V.Keerthika Roll No: 1975020
SRI PADMAVATI VISVAVIDYALAYAM, TIRUPATI
(Women’s University)
SCHOOL OF ENGINEERING AND TECHNOLOGY

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]);
}
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++)
2
Name: V.Keerthika Roll No: 1975020
SRI PADMAVATI VISVAVIDYALAYAM, TIRUPATI
(Women’s University)
SCHOOL OF ENGINEERING AND TECHNOLOGY

{
for(j=0;j<p;j++)
{
printf("%d\t",mul[i][j]);
}
printf("\n");
}
}

INPUT:
Enter the number of rows and columns for 1st matrix
2
2
Enter the elements of the 1st matrix
1234
Enter the number of columns for nd matrix
2
Enter the elements of the 2nd matrix
5 678

3
Name: V.Keerthika Roll No: 1975020
SRI PADMAVATI VISVAVIDYALAYAM, TIRUPATI
(Women’s University)
SCHOOL OF ENGINEERING AND TECHNOLOGY

OUTPUT:

Enter the number of rows and columns for 1st matrix


2
2
Enter the elements of the 1st matrix
1234
Enter the number of columns for and matrix
2
Enter the elements of the 2nd matrix
5 678

The 1st matrix


1 2
3 4
The 2nd matrix
5 6
7 8
The resultant matrix formed on multiplying the two matrices
19 22
43 50

RESULT: Hence,the c program for multiply two Matrices by Passing Matrix to a Function
is executed successfully.
4
Name: V.Keerthika Roll No: 1975020
SRI PADMAVATI VISVAVIDYALAYAM, TIRUPATI
(Women’s University)
SCHOOL OF ENGINEERING AND TECHNOLOGY

Exp No:2 Remove all Characters in a Date:04/02/2022


String Except Alphabet

AIM: To write a C Program to remove all characters in a string except alphabet.

SOURCE CODE:

#include<stdio.h>
int main()
{
char str[150];
int i, j;
printf(“\nEnter a string : “);
gets(str);
for(i = 0; str[i] != ‘\0’; ++i)
{
while (!( (str[i] >= ‘a’ && str[i] <= ‘z’) || (str[i] >= ‘A’ && str[i] <= ‘Z’) || str[i] == ‘\0’) )
{
for(j = i; str[j] != ‘\0’; ++j)
{
str[j] = str[j+1];
}
str[j] = ‘\0’;
}
}
printf(“\nResultant String : “);
puts(str);
return 0;
}

INPUT:

Enter a string : 1@k3$e&8e39)>rT/h2i2#ka@

OUTPUT:

Enter a string : 1@k3$e&8e39)>rT/k2i2#Ka@


keerThika

RESULT: Hence the C program to remove all charcaters in a string except alphabet is
executed successfuly.
5
Name: V.Keerthika Roll No: 1975020
SRI PADMAVATI VISVAVIDYALAYAM, TIRUPATI
(Women’s University)
SCHOOL OF ENGINEERING AND TECHNOLOGY

Exp No:3 Add Two Complex Numbers by Passing Structure to a Date:04/02/2022


Function

AIM: To write a C Program to Add Two Complex Numbers by Passing Structure to a Function.

SOURCECODE:
#include <stdio.h>
typedef struct complex{
float real;
float imag;
} complex;
complex addition(complex num1, complex num2);
int main(){
complex num1, num2, value;
printf("entering real and imag parts of first complex no:\n ");
scanf("%f %f", &num1.real, &num1.imag);
printf("entering real and imag parts of second complex no:\n ");
scanf("%f %f", &num2.real, &num2.imag);
value= addition(num1, num2);
printf("result = %.1f + %.1fi", value.real, value.imag);
return 0;
}
complex addition(complex num1, complex num2){
complex temp;
temp.real = num1.real + num2.real;
temp.imag = num1.imag + num2.imag;
return (temp);
}

INPUT:
entering real and imag parts of first complex no:2.0 3.0
entering real and imag parts of second complex no:4.0 6.0

OUTPUT:
entering real and imag parts of first complex no:2.0 3.0
entering real and imag parts of second complex no:4.0 6.0
result = 6.0 + 9.0i

RESULT: Hence, the C program to add two complex number by passing structure to
function is executed successfully.
6
Name: V.Keerthika Roll No: 1975020
SRI PADMAVATI VISVAVIDYALAYAM, TIRUPATI
(Women’s University)
SCHOOL OF ENGINEERING AND TECHNOLOGY

Exp No:4 Storing student records as structures and sort them by Date:08/02/2022
name

AIM: To write a C program to store student records as structures and sort them by name

SOURCE CODE:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Student {
int student_id;
char* student_name;
int student_percentage;
};
int comparator(const void* s1, const void* s2){
return strcmp(((struct Student*)s1)->student_name,((struct Student*)s2)->student_name);
}
int main() {
int n = 5;
struct Student arr[n];
//student 1
arr[0].student_id = 1;
arr[0].student_name = "Uday";
arr[0].student_percentage = 98;
//student 2
arr[1].student_id = 2;
arr[1].student_name = "Manasa";
arr[1].student_percentage = 75;
//student 3
arr[2].student_id = 3;
arr[2].student_name = "Thanuja";
arr[2].student_percentage = 62;
//student 4
arr[3].student_id = 4;
arr[3].student_name = "Mounish";
arr[3].student_percentage = 87;
//student 5
arr[4].student_id = 5;
arr[4].student_name = "Manognal";
arr[4].student_percentage = 80;
printf("Unsorted Student Record:\n");
7
Name: V.Keerthika Roll No: 1975020
SRI PADMAVATI VISVAVIDYALAYAM, TIRUPATI
(Women’s University)
SCHOOL OF ENGINEERING AND TECHNOLOGY

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


printf("Id = %d, Name = %s, Age = %d \n", arr[i].student_id, arr[i].student_name,
arr[i].student_percentage);
}
qsort(arr, n, sizeof(struct Student), comparator);
printf("\n\nStudent Records sorted by Name:\n");
for (int i = 0; i < n; i++) {
printf("Id = %d, Name = %s, Age = %d \n", arr[i].student_id, arr[i].student_name,
arr[i].student_percentage);
}
return 0;
}

INPUT:

student_id = 1, student_name =Uday, student_percentage = 98


student_id = 2, student_name = Manasa, student_percentage = 75
student_id = 3, student_name = Thanuja, student_percentage = 62
student_id = 4, student_name = Mounish, student_percentage = 87
student_id = 5, student_name = Manogna, student_percentage = 80

OUTPUT:

student_id = 2, student_name = Manasa, student_percentage = 75


student_id = 4, student_name = Manogna, student_percentage = 80
student_id = 1, student_name = Mounish, student_percentage = 87
student_id = 5, student_name = Thanuja, student_percentage = 62
student_id = 3, student_name = Uday, student_percentage = 98

RESULT: Hence, the C program to store student records as structures and sort them by
name is executed successfully.
8
Name: V.Keerthika Roll No: 1975020
SRI PADMAVATI VISVAVIDYALAYAM, TIRUPATI
(Women’s University)
SCHOOL OF ENGINEERING AND TECHNOLOGY

Exp No:5 Finding the Number of Non Repeated Elements in Date:08/02/2022


an Array

AIM: To write a C program to find the number of non repeated elements in an array.

SOURCE CODE:

#include < stdio.h>


int main()
{
int array[50];
int *ptr;
int i, j, k, size, n;

printf("\n Enter size of the array: " );


scanf("%d" , &n);
printf("\n Enter %d elements of an array: " , n);
for (i = 0; i < n; i++)
scanf("%d" , &array[i]);
size = n;
ptr = array;
for (i = 0; i < size; i++)
{
for (j = 0; j < size; j++)
{
if (i == j)
{
continue;
}
else if (*(ptr + i) == *(ptr + j))
{
k = j;
size--;
while (k < size)
{
*(ptr + k) = *(ptr + k + 1);
k++;
}
j = 0;
}
}
9
Name: V.Keerthika Roll No: 1975020
SRI PADMAVATI VISVAVIDYALAYAM, TIRUPATI
(Women’s University)
SCHOOL OF ENGINEERING AND TECHNOLOGY

}
printf("\n The array after removing duplicates is: " );
for (i = 0; i < size; i++)
{
printf(" %d" , array[i]);
}
return 0;
}

INPUT:
Enter size of the array: 6
Enter 6 elements of an array: 12
10
4
10
12
56

OUTPUT:

Enter size of the array: 6


Enter 6 elements of an array: 12
10
4
10
12
56
The array after removing duplicates is: 12 10 4 56

RESULT: Hence, the C program to find the number of non repeated elements in an array is
executed successfully.

10
Name: V.Keerthika Roll No: 1975020
SRI PADMAVATI VISVAVIDYALAYAM, TIRUPATI
(Women’s University)
SCHOOL OF ENGINEERING AND TECHNOLOGY

Exp No:6 Finding two Elements in the Array such that Date:08/02/2022
Difference between them is Largest

AIM: To write a C program to find two elements in the array such that difference between
then is largest.

SOURCE CODE:

#include<stdio.h>

#define N 6

int main()
{
int num[N], i, big, small;

printf("Enter %d integer numbers\n", N);


for(i = 0; i < N; i++)
scanf("%d", &num[i]);

big = small = num[0];

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


{
if(num[i] > big)
big = num[i];

if(num[i] < small)


small = num[i];
}

printf("The largest difference is %d, ", (big - small));


printf("and its between %d and %d.\n", big, small);

return 0;
}

11
Name: V.Keerthika Roll No: 1975020
SRI PADMAVATI VISVAVIDYALAYAM, TIRUPATI
(Women’s University)
SCHOOL OF ENGINEERING AND TECHNOLOGY

INPUT:
Enter 6 integer numbers
8
10
6
7
14
1

OUTPUT:

Enter 6 integer numberrs


8
10
6
7
14
1
The largest difference is 13, and its between 14and 1.

RESULT: Hence, the C program to find two Elements in the array such that difference
between them is larges.t
12
Name: V.Keerthika Roll No: 1975020
SRI PADMAVATI VISVAVIDYALAYAM, TIRUPATI
(Women’s University)
SCHOOL OF ENGINEERING AND TECHNOLOGY

Exp No:7 Finding the two Elements such that their Sum is Date:15/02/2022
Closest to Zero

AIM: TO write a C program to find the two elements such that their sum is closest to zero.

SOURCE CODE:

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

void minabsvaluepair(int array[], int array_size)


{
int count = 0;
int l, r, min_sum, sum, min_l, min_r;

/* Array should have at least two elements*/


if (array_size < 2)
{
printf("Invalid Input");
return;
}

/* Initialization of values */
min_l = 0;
min_r = 1;
min_sum = array[0] + array[1];
for (l = 0; l < array_size - 1; l++)
{
for (r = l + 1; r < array_size; r++)
{
sum = array[l] + array[r];
if (abs(min_sum) > abs(sum))
{
min_sum = sum;
min_l = l;
min_r = r;
}
}
}
printf(" The two elements whose sum is minimum are %d and %d", array[min_l],
array[min_r]);
13
Name: V.Keerthika Roll No: 1975020
SRI PADMAVATI VISVAVIDYALAYAM, TIRUPATI
(Women’s University)
SCHOOL OF ENGINEERING AND TECHNOLOGY

int main()
{
int array[] = {42, 15, -25, 30, -10, 35};
minabsvaluepair(array, 6);
getchar();
return 0;
}

Output:

The two elements whose sum is minimum are 15 and -10

RESULT: Hence, the C program to find the two elements in an array whose sum is closest
to zero is executed successfully.
14
Name: V.Keerthika Roll No: 1975020
SRI PADMAVATI VISVAVIDYALAYAM, TIRUPATI
(Women’s University)
SCHOOL OF ENGINEERING AND TECHNOLOGY

Exp No:8 Date:15/02/2022


Finding Union & Intersection of 2 Arrays

AIM: To find Union & Intersection of 2 Arrays

SOURCE CODE:

include<stdio.h>
void printUnion(int arr1[], int arr2[], int len1, int len2)
{
int f, i, j, k = 0;
int arr3[100];

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


arr3[k] = arr1[i];
k++;
}

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


{
f = 0;
for (j = 0; j < len1; j++)
{
if (arr2[i] == arr1[j])
{
f = 1;
}
}

if (f == 0) {
arr3[k] = arr2[i];
k++;
}
}
printf("Union of two array is:");
for (i = 0; i < k; i++)
{
printf("%d ", arr3[i]);
}
}

void printIntersection(int arr1[], int arr2[], int len1, int len2)


{
15
Name: V.Keerthika Roll No: 1975020
SRI PADMAVATI VISVAVIDYALAYAM, TIRUPATI
(Women’s University)
SCHOOL OF ENGINEERING AND TECHNOLOGY

int arr3[100];
int i, j, k = 0;

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


{
for (j = 0; j < len2; j++)
{
if (arr1[i] == arr2[j])
{
arr3[k] = arr1[i];
k++;
}
}
}
printf("\nIntersection of two array is:");
for (i = 0; i < k; i++) {
printf("%d ", arr3[i]);
}
}

int main() {

int arr1[100];
int arr2[100];
int arr3[100];
int i, j, len1, len2;

printf("Enter size of 1st array:");


scanf("%d", &len1);

printf("Enter 1st array elements:");


for (i = 0; i < len1; i++)
{
scanf("%d", &arr1[i]);
}

printf("Enter size of 2nd array:");


scanf("%d", &len2);

printf("Enter 2nd array elements:");


for (i = 0; i < len2; i++) {
scanf("%d", &arr2[i]);
16
Name: V.Keerthika Roll No: 1975020
SRI PADMAVATI VISVAVIDYALAYAM, TIRUPATI
(Women’s University)
SCHOOL OF ENGINEERING AND TECHNOLOGY

printUnion(arr1, arr2, len1, len2);


printIntersection(arr1, arr2, len1, len2);

return 0;
}

INPUT:

Enter size of 1st array:4


Enter 1st array elements:2 3 4 5
Enter size of 2nd array:4
Enter 2nd array elements:3 4 5 6

Output:

Enter size of 1st array:4


Enter 1st array elements:2 3 4 5
Enter size of 2nd array:4
Enter 2nd array elements:3 4 5 6
Union of two array is:2 3 4 5 6
Intersection of two array is:3 4 5

RESULT: Hence, the C program to find the Union and Intersection of 2 arrays is executed
successfully.
17
Name: V.Keerthika Roll No: 1975020
SRI PADMAVATI VISVAVIDYALAYAM, TIRUPATI
(Women’s University)
SCHOOL OF ENGINEERING AND TECHNOLOGY

Exp No:9 Interchange any two Rows & Columns in the given Date:15/02/2022
Matrix

AIM: To write a C Program to Interchange any two Rows & Columns in the given Matrix

SOURCE CODE:

#include <stdio.h>
void main()
{
static int array1[10][10], array2[10][10];
int i, j, m, n, a, b, c, p, q, r;

printf("Enter the order of the matrix \n");


scanf("%d %d", &m, &n);
printf("Enter the co-efficents of the matrix \n");
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
scanf("%d,", &array1[i][j]);
array2[i][j] = array1[i][j];
}
}
printf("Enter the numbers of two rows to be exchanged \n");
scanf("%d %d", &a, &b);
for (i = 0; i < m; ++i)
{
c = array1[a - 1][i];
array1[a - 1][i] = array1[b - 1][i];
array1[b - 1][i] = c;
}
printf("Enter the numbers of two columns to be exchanged \n");
scanf("%d %d", &p, &q);
printf("The given matrix is \n");
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
printf(" %d", array2[i][j]);
printf("\n");
}
for (i = 0; i < n; ++i)
18
Name: V.Keerthika Roll No: 1975020
SRI PADMAVATI VISVAVIDYALAYAM, TIRUPATI
(Women’s University)
SCHOOL OF ENGINEERING AND TECHNOLOGY

{
r = array2[i][p - 1];
array2[i][p - 1] = array2[i][q - 1];
array2[i][q - 1] = r;
}
printf("The matix after interchanging the two rows(in the original matrix) \n");
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
printf(" %d", array1[i][j]);
}
printf("\n");
}
printf("The matix after interchanging the two columns(in the original matrix) \n");
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
printf(" %d", array2[i][j]);
printf("\n");
}
}

INPUT:

Enter the order of the matrix


22
Enter the co-efficents of the matrix
34 70
45 90
Enter the numbers of two rows to be exchanged
12
Enter the numbers of two columns to be exchanged
12

OUTPUT:

Enter the order of the matrix


22
Enter the co-efficents of the matrix
34 70
45 90
19
Name: V.Keerthika Roll No: 1975020
SRI PADMAVATI VISVAVIDYALAYAM, TIRUPATI
(Women’s University)
SCHOOL OF ENGINEERING AND TECHNOLOGY

Enter the numbers of two rows to be exchanged


12
Enter the numbers of two columns to be exchanged
12
The given matrix is
34 70
45 90
The matix after interchanging the two rows(in the original matrix)
45 90
34 70
The matix after interchanging the two columns(in the original matrix)
70 34
90 45

RESULT: Hence, the C Program to Interchange any two Rows & Columns in the given Matrix is
exectuted successfully.
20
Name: V.Keerthika Roll No: 1975020
SRI PADMAVATI VISVAVIDYALAYAM, TIRUPATI
(Women’s University)
SCHOOL OF ENGINEERING AND TECHNOLOGY

Exp.No: 10 Sort Rows of the Matrix in Ascending & Columns in Date19/02/2022


Descending Order

AIM: To write a C program to sort rows of the matrix in Ascending & Columns in Descending Order.

SOURCE CODE:

#include <stdio.h>

void main()
{
static int array1[10][10], array2[10][10];
int i, j, k, a, m, n;

printf("Enter the order of the matrix \n");


scanf("%d %d", &m, &n);
printf("Enter co-efficients of the matrix \n");
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
scanf("%d", &array1[i][j]);
array2[i][j] = array1[i][j];
}
}
printf("The given matrix is \n");
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
printf(" %d", array1[i][j]);
}
printf("\n");
}
printf("After arranging rows in ascending order\n");
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
for (k =(j + 1); k < n; ++k)
{
if (array1[i][j] > array1[i][k])
{
21
Name: V.Keerthika Roll No: 1975020
SRI PADMAVATI VISVAVIDYALAYAM, TIRUPATI
(Women’s University)
SCHOOL OF ENGINEERING AND TECHNOLOGY

a = array1[i][j];
array1[i][j] = array1[i][k];
array1[i][k] = a;
}
}
}
}
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
printf(" %d", array1[i][j]);
}
printf("\n");
}
printf("After arranging the columns in descending order \n");
for (j = 0; j < n; ++j)
{
for (i = 0; i < m; ++i)
{
for (k = i + 1; k < m; ++k)
{
if (array2[i][j] < array2[k][j])
{
a = array2[i][j];
array2[i][j] = array2[k][j];
array2[k][j] = a;
}
}
}
}
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
printf(" %d", array2[i][j]);
}
printf("\n");
}
}

22
Name: V.Keerthika Roll No: 1975020
SRI PADMAVATI VISVAVIDYALAYAM, TIRUPATI
(Women’s University)
SCHOOL OF ENGINEERING AND TECHNOLOGY

INPUT:

Enter the order of the matrix


33
Enter co-efficients of the matrix
379
248
526

OUTPUT:
Enter the order of the matrix
33
Enter co-efficients of the matrix
379
248
526
The given matrix is
379
248
526
After arranging rows in ascending order
379
248
256
After arranging the columns in descending order
579
348
226

RESULT: Hence, the C program to sort rows of the matrix in ascending & columns in descending order.

23
Name: V.Keerthika Roll No: 1975020
SRI PADMAVATI VISVAVIDYALAYAM, TIRUPATI
(Women’s University)
SCHOOL OF ENGINEERING AND TECHNOLOGY

Exp No:11 Creating a file, open it, type-in some characters and Date:19/02/2022
count the number of characters in a file

AIM: To create a file, open it, type-in some characters and count the number of characters in a file using C
program.

SOURCE CODE:

#include <stdio.h>
#define MAX_FILE_NAME 100

int main()
{
FILE* fp;

// Character counter (result)


int count = 0;

char filename[MAX_FILE_NAME];

// To store a character read from file


char c;

// Get file name from user.


// The file should be either in current folder
// or complete path should be provided
printf("Enter file name: ");
scanf("%s", filename);

// Open the file


fp = fopen(filename, "r");

// Check if file exists


if (fp == NULL) {
printf("Could not open file %s",
filename);
return 0;
}

// Extract characters from file


// and store in character c
for (c = getc(fp); c != EOF; c = getc(fp))

// Increment count for this character


count = count + 1;

// Close the file


fclose(fp);

24
Name: V.Keerthika Roll No: 1975020
SRI PADMAVATI VISVAVIDYALAYAM, TIRUPATI
(Women’s University)
SCHOOL OF ENGINEERING AND TECHNOLOGY

// Print the count of characters


printf("The file %s has %d characters\n ",
filename, count);

return 0;
}

INPUT:

Enter the file name: tentimesname.txt


The file tentimesname.txt has 124 characters

RESULT: Hence, by using C program to create a file, open it, type-in some characters and
count the number of characters in a file is successfully executed.
25
Name: V.Keerthika Roll No: 1975020
SRI PADMAVATI VISVAVIDYALAYAM, TIRUPATI
(Women’s University)
SCHOOL OF ENGINEERING AND TECHNOLOGY

Exp NO:12 Date:19/02/2022


Display Upper Triangular Matrix

AIM: To write a C program to display upper triangular matrix.

SOURCE CODE:

#include <stdio.h>
void main()
{

int i, j, r, c, array[10][10];
printf("Enter the r and c value:");

scanf("%d%d", &r, &c);


for (i = 1; i <= r; i++)
{
for (j = 1; j <= c; j++)
{
printf("array[%d][%d] = ", i, j);
scanf("%d", &array[i][j]);
}
}

printf("matrix is");
for (i = 1; i <= r; i++)
{
for (j = 1; j <= c; j++)
{
printf("%d", array[i][j]);
}
printf("\n");
}

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


{
printf("\n");
for (j = 1; j <= c; j++)
{
if (i >= j)
{
printf("%d", array[i][j]);
}
26
Name: V.Keerthika Roll No: 1975020
SRI PADMAVATI VISVAVIDYALAYAM, TIRUPATI
(Women’s University)
SCHOOL OF ENGINEERING AND TECHNOLOGY

else
{
printf("\t");
}
}

printf("\n\n");
for (i = 1; i <= r; i++)
{
printf("\n");
for (j = 1; j <= c; j++)
{
if (j >= i)
{
printf("%d", array[i][j]);
}
else
{
//printf("\t");
}
// printf("\n");

}
}

INPUT:
Enter the r and c value:3 3
array[1][1] = 1 1 1
array[1][2] = array[1][3] = array[2][1] = 1 1 0
array[2][2] = array[2][3] = array[3][1] = 2 0 0

27
Name: V.Keerthika Roll No: 1975020
SRI PADMAVATI VISVAVIDYALAYAM, TIRUPATI
(Women’s University)
SCHOOL OF ENGINEERING AND TECHNOLOGY

OUTPUT:
Enter the r and c value:3 3
array[1][1] = 1 1 1
array[1][2] = array[1][3] = array[2][1] = 1 1 0
array[2][2] = array[2][3] = array[3][1] = 2 0 0
array[3][2] = array[3][3] = matrix is
111
110
200

1
11
200

RESULT: Hence, to write a C program to display the upper triangle of a given matrix is
executed successfully

28
Name: V.Keerthika Roll No: 1975020
SRI PADMAVATI VISVAVIDYALAYAM, TIRUPATI
(Women’s University)
SCHOOL OF ENGINEERING AND TECHNOLOGY

Exp No:13 Replacing Lowercase Characters by Uppercase & Date:22/02/2022


Vice-Versa

AIM: To write a C program to replace Lowercase characters by Uppercase and Vice-versa.

SOURCE CODE:

#include <stdio.h>
#include <ctype.h>
void main()

char sentence[100];

int count, ch, i;

printf("Enter a sentence \n");

for (i = 0;(sentence[i] = getchar()) != '\n'; i++)

sentence[i] = '\0';

count = i;

printf("The given sentence is : %s", sentence);

printf("\n Case changed sentence is: ");

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

ch = islower(sentence[i])? toupper(sentence[i]) : tolower(sentence[i]);

putchar(ch);
29
Name: V.Keerthika Roll No: 1975020
SRI PADMAVATI VISVAVIDYALAYAM, TIRUPATI
(Women’s University)
SCHOOL OF ENGINEERING AND TECHNOLOGY

}
printf("\n\n");
}

INPUT:
Enter the sentence: KeeRtHikA

OUTPUT:
Enter the sentence : KeeRtHikA
Case changed sentence is:kEErThIKa

Result: Hence,to write a C program to replace lowercase characters by uppercase & vice-
versa is executed successfully.

30
Name: V.Keerthika Roll No: 1975020
SRI PADMAVATI VISVAVIDYALAYAM, TIRUPATI
(Women’s University)
SCHOOL OF ENGINEERING AND TECHNOLOGY

Exp No:14 Date:22/02/2022


Reversing every Word of given String

AIM: To write a C program to reverse every word of given string.

SOURCE CODE:
#include <stdio.h>
#include <string.h>

void main()
{
int i, j = 0, k = 0, x, len;
char str[100], str1[10][20], temp;

printf("enter the string :");


scanf("%[^\n]s", str);

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


{
if (str[i] == ' ')
{
str1[k][j]='\0';
k++;
j=0;
}
else
{
str1[k][j]=str[i];
j++;
}
}
str1[k][j] = '\0';

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


{
len = strlen(str1[i]);
for (j = 0, x = len - 1;j < x;j++,x--)
{
temp = str1[i][j];
str1[i][j] = str1[i][x];
31
Name: V.Keerthika Roll No: 1975020
SRI PADMAVATI VISVAVIDYALAYAM, TIRUPATI
(Women’s University)
SCHOOL OF ENGINEERING AND TECHNOLOGY

str1[i][x] = temp;
}

INPUT:

enter the string: keerthika

OUTPUT:

enter the string: keerthika


Akihtreek

RESULT: Hence, the C program to reverse every word of the string is executed successfully.

32
Name: V.Keerthika Roll No: 1975020
SRI PADMAVATI VISVAVIDYALAYAM, TIRUPATI
(Women’s University)
SCHOOL OF ENGINEERING AND TECHNOLOGY

Exp No:15 Deleting All Repeated Date:22/02/2022


Words in String
AIM: To write a C program to delete all repeated words from a string.

SOURCE CODE:

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

int main()
{
char s[1000],temp=1,c='*';
int i,j,k=0,n;

printf("Enter the string : ");


gets(s);
for(i=0;s[i];i++)
{
if(!(s[i]==c))
{
for(j=i+1;s[j];j++)
{
if(s[i]==s[j])
s[j]=c;

}
}

for(i=0;s[i];i++)
{
s[i]=s[i+k];

if(s[i]==c)
{
k++;
i--;
}

}
33
Name: V.Keerthika Roll No: 1975020
SRI PADMAVATI VISVAVIDYALAYAM, TIRUPATI
(Women’s University)
SCHOOL OF ENGINEERING AND TECHNOLOGY

printf("string after removing all duplicates:");

printf("%s",s);

return 0;
}

INPUT:

Enter the string: do good be good

OUTPUT:
Enter the string: do good be good
String after removing all duplicates: do be

Result: Hence to write a C program to delete all repeated words from a string is successfully
executed.
34
Name: V.Keerthika Roll No: 1975020
SRI PADMAVATI VISVAVIDYALAYAM, TIRUPATI
(Women’s University)
SCHOOL OF ENGINEERING AND TECHNOLOGY

Exp No:16 Deleting All Repeated Words in Date:12/02/2022


String

AIM: To write a C program to find highest frequency character in a string.

SOURCE CODE:

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

int main()
{
char s[1000];
int a[1000],i,j,k=0,count=0,n;

printf("Enter the string : ");


gets(s);

for(j=0;s[j];j++);
n=j;

for(i=0;i<n;i++)
{
a[i]=0;
count=1;
if(s[i])
{

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

if(s[i]==s[j])
{
count++;
s[j]='\0';
}
}
}
a[i]=count;
if(count>=k)
k=count;

35
Name: V.Keerthika Roll No: 1975020
SRI PADMAVATI VISVAVIDYALAYAM, TIRUPATI
(Women’s University)
SCHOOL OF ENGINEERING AND TECHNOLOGY

}
printf("maximum occuring characters ");
for(j=0;j<n;j++)
{

if(a[j]==k)
{
printf(" '%c',",s[j]);
}
}

printf("\b=%d times \n ",k);


return 0;
}

INPUT:

Enter the string: do good be good

OUTPUT:
Enter the string: do good be good
maximum occurring characters 'o'=5 times

RESULT:
Hence the C program to find highest frequency character in a string is successfully executed.
36
Name: V.Keerthika Roll No: 1975020
SRI PADMAVATI VISVAVIDYALAYAM, TIRUPATI
(Women’s University)
SCHOOL OF ENGINEERING AND TECHNOLOGY

Exp No:17 Displaying every possible Combination of two Date:12/04/2022


characters from the input word without Repeated
Combination

AIM: To write a C program to display every possible combination of two characters from
the input word without repeated combinations.

SOURCE CODE:

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

void main()
{
int i, j = 0, k, k1 = 0, k2 = 0, row = 0;
char temp[50];
char str[100], str2[100], str1[5][20], str3[6][20], str4[60][40];

printf("enter the string :");


scanf(" %[^\n]s", &str);
printf("enter string:");
scanf(" %[^\n]s", &str2);

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


{
if (str[i] == ' ')
{
str1[k1][j] = '\0';
k1++;
j = 0;
}
else
{
str1[k1][j] = str[i];
j++;
}
}
str1[k1][j] = '\0';
j = 0;
for (i = 0;str2[i] != '\0';i++)
{
37
Name: V.Keerthika Roll No: 1975020
SRI PADMAVATI VISVAVIDYALAYAM, TIRUPATI
(Women’s University)
SCHOOL OF ENGINEERING AND TECHNOLOGY

if (str2[i] == ' ')


{
str3[k2][j] = '\0';
k2++;
j = 0;
}
else
{
str3[k2][j] = str2[i];
j++;
}
}
str3[k2][j] = '\0';

row = 0;
for (i = 0;i <= k1;i++)
{
for (j = 0;j <= k2;j++)
{
strcpy(temp, str1[i]);
strcat(temp, str3[j]);
strcpy(str4[row], temp);
row++;
}
}
for (i = 0;i <= k2;i++)
{
for (j = 0;j <= k1;j++)
{
strcpy(temp, str3[i]);
strcat(temp, str1[j]);
strcpy(str4[row], temp);
row++;
}
}

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


{
for (j = i + 1;j < row;j++)
{
if (strcmp(str4[i], str4[j]) == 0)
{
for (k = j;k <= row;k++)
38
Name: V.Keerthika Roll No: 1975020
SRI PADMAVATI VISVAVIDYALAYAM, TIRUPATI
(Women’s University)
SCHOOL OF ENGINEERING AND TECHNOLOGY

{
strcpy(str4[k], str4[k + 1]);
}
row--;
}
}
}

/* displays the output */


for (i = 0;i < row;i++)
{
printf("\n%s", str4[i]);
}
}

INPUT:
Enter the string: do good
Enter the string: be good

OUTPUT:
Enter the string: do good
Enter the string: be good

do be
do good
good be
good

RESULT: Hence, to write a C program to display every possible combination of two


characters from the input word without repeated combinations is successfully executed
39
Name: V.Keerthika Roll No: 1975020
SRI PADMAVATI VISVAVIDYALAYAM, TIRUPATI
(Women’s University)
SCHOOL OF ENGINEERING AND TECHNOLOGY

Exp No:18 Forming new number that is greater than the Date:12/04/2022
given number but should have the same digits

AIM: To write a C program that takes input as 2323 and gives output as 2332. ie. the new
number should be greater than the previous number but should have the same digits.

SOURCE CODE:

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

int evaluate(int [], int);


int find(int);

int main()
{
int num, result;

printf("Enter a number: ");


scanf("%d", &num);
result = find(num);
if (result)
{
printf("The number greater than %d and made of same digits is %d.\n", num, result);
}
else
{
printf("No higher value possible. Either all numbers are same or the digits of the
numbers entered are in decreasing order.\n");
}

return 0;
}

int find(int num)


{
int digit[20];
int i = 0, len = 0, n, temp;

n = num;
while (n != 0)
{
40
Name: V.Keerthika Roll No: 1975020
SRI PADMAVATI VISVAVIDYALAYAM, TIRUPATI
(Women’s University)
SCHOOL OF ENGINEERING AND TECHNOLOGY

digit[i] = n % 10;
n = n / 10;
i++;
}
len = i;
for (i = 0; i < len - 1; i++)
{
if (digit[i] > digit[i + 1])
{
temp = digit[i];
digit[i] = digit[i + 1];
digit[i + 1] = temp;

return (evaluate(digit, len));


}
}

return 0;
}

int evaluate(int digit[], int len)


{
int i, num = 0;
for (i = 0; i < len; i++)
{
num += digit[i] * pow(10, i);
}

return num;
}

INPUT:
Enter a number: 56732

OUTPUT:
Enter a number: 56732
The number greater than 56732 and made of same digits is 57632.

RESULT: Hence, to write a C program that takes input as 2323 and gives output as 2332. ie.
the new number should be greater than the previous number but should have the same digits
is successfully executed.
41
Name: V.Keerthika Roll No: 1975020
SRI PADMAVATI VISVAVIDYALAYAM, TIRUPATI
(Women’s University)
SCHOOL OF ENGINEERING AND TECHNOLOGY

Exp No:19 Determining if One String is a Circular Permutation Date:19/04/2022


of Another String

AIM: To write a C program for determining if one string is a circular permutation of another
string.

SOURCE CODE:

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#define CHAR_SIZE 26

void alphacheck(char *, int []);


void create(char [], char [], int[]);

int main()
{
char str1[50], str2[50];
int a1[CHAR_SIZE] = {0};
char str2_rem[50];

printf("Enter string1: ");


scanf("%s", str1);
printf("Enter string2: ");
scanf("%s", str2);
alphacheck(str1, a1);
create(str2_rem, str2, a1);
printf("On removing characters from second string we get: %s\n", str2_rem);

return 0;
}

void alphacheck(char *str, int a[])


{
int i, index;

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


{
str[i] = tolower(str[i]);
42
Name: V.Keerthika Roll No: 1975020
SRI PADMAVATI VISVAVIDYALAYAM, TIRUPATI
(Women’s University)
SCHOOL OF ENGINEERING AND TECHNOLOGY

index = str[i] - 'a';


if (!a[index])
{
a[index] = 1;
}
}
printf("\n");
}

void create(char str_rem[], char str[], int list[])


{
int i, j = 0, index;

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


{
index = str[i] - 'a';
if (!list[index])
{
str_rem[j++] = str[i];
}
}
str_rem[j] = '\0';
}

INPUT:
Enter string 1: abcd
Enter string 2: dabc

OUTPUT:
Enter string 1: abcd
Enter string 2: dabc
abcd & dabc are circular permutation of each other.

RESULT: Hence, to write a C program for determining if one string is a circular


permutation of another string.
43
Name: V.Keerthika Roll No: 1975020
SRI PADMAVATI VISVAVIDYALAYAM, TIRUPATI
(Women’s University)
SCHOOL OF ENGINEERING AND TECHNOLOGY

Exp No:20 Finding the Consecutive Occurrence of any Date:19/04/2022


Vowel in a String

AIM: To write a c program to find the consecutive occurrence of any vowel in a string

SOURCE CODE :

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

struct detail
{
char word[20];
};

int update(struct detail [], const char [], int);


int vowelcheck(char);

int main()
{
struct detail s[10];
char string[100], unit[20], c;
int i = 0, j = 0, count = 0;

printf("Enter string: ");


i = 0;
do
{
fflush(stdin);
c = getchar();
string[i++] = c;

} while (c != '\n');
string[i - 1] = '\0';
printf("The string entered is: %s\n", string);
for (i = 0; i < strlen(string); i++)
{
while (i < strlen(string) && string[i] != ' ' && isalnum(string[i]))
{
unit[j++] = string[i++];
}
44
Name: V.Keerthika Roll No: 1975020
SRI PADMAVATI VISVAVIDYALAYAM, TIRUPATI
(Women’s University)
SCHOOL OF ENGINEERING AND TECHNOLOGY

if (j != 0)
{
unit[j] = '\0';
count = update(s, unit, count);
j = 0;
}
}

printf("*Words with consecutive vowel*\n");


for (i = 0; i < count; i++)
{
printf("%s\n", s[i].word);
}

return 0;
}

int update(struct detail s[], const char unit[], int count)


{
int i, j = 0;

for (i = 0; i < strlen(unit) - 1; i++)


{
if (vowelcheck(unit[i]))
{
if (vowelcheck(unit[i+ 1]))
{
/To avoid duplicate strings/
while (j < count && strcmp(s[j].word, unit))
{
j++;
}
if (j == count)
{
strcpy(s[j].word, unit);

return (count + 1);


}
}
}
}

return count;
45
Name: V.Keerthika Roll No: 1975020
SRI PADMAVATI VISVAVIDYALAYAM, TIRUPATI
(Women’s University)
SCHOOL OF ENGINEERING AND TECHNOLOGY

int vowelcheck(char c)
{
char vowel[5] = {'a', 'e', 'i', 'o', 'u'};
int i;

c = tolower(c);
for (i = 0; i < 5; i++)
{
if (c == vowel[i])
{
return 1;
}
}

return 0;
}

INPUT:

Enter string: Who will lead his team to victory

OUTPUT:

Enter string: Who will lead his team to victory


The string entered is : who will lead his team to victory
*words with consecutive vowels *
lead
Team

RESULT: Hence to write a C program to find the consecutive occurrence of any vowel in a
string.
46
Name: V.Keerthika Roll No: 1975020
SRI PADMAVATI VISVAVIDYALAYAM, TIRUPATI
(Women’s University)
SCHOOL OF ENGINEERING AND TECHNOLOGY

Exp No:21 Accepingt 2 String & check whether all Date:19/04/2022


Characters in first String is Present in second
String & Print

AIM: To write a C program to accept two strings and check whether all characters in first
string is present in second string and print.

SOURCE CODE:

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#define CHAR_SIZE 26

void alphacheck(char *, int []);


void create(char *, int[]);

int main()
{
char str1[50], str2[50];
int a1[CHAR_SIZE] = {0}, a2[CHAR_SIZE] = {0}, i;
char str1_alpha[CHAR_SIZE], str2_alpha[CHAR_SIZE];

printf("Enter string1: ");


scanf("%s", str1);
printf("Enter string2: ");
scanf("%s", str2);
alphacheck(str1, a1);
alphacheck(str2, a2);
create(str1_alpha, a1);
create(str2_alpha, a2);
if (strcmp(str1_alpha, str2_alpha) == 0)
{
printf("All characters match in %s and %s.\n", str1, str2);
printf("The characters that match are: ");
for (i = 0; i < strlen(str1_alpha); i++)
{
printf("%c, ", str1_alpha[i]);
}
printf("\n");
47
Name: V.Keerthika Roll No: 1975020
SRI PADMAVATI VISVAVIDYALAYAM, TIRUPATI
(Women’s University)
SCHOOL OF ENGINEERING AND TECHNOLOGY

}
else
{
printf("All characters do not match in %s and %s.\n", str1, str2);
}

return 0;
}

void alphacheck(char *str, int a[])


{
int i, index;

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


{
str[i] = tolower(str[i]);
index = str[i] - 'a';
if (!a[index])
{
a[index] = 1;
}
}
}

void create(char *str, int a[])


{
int i, j = 0;

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


{
if (a[i])
{
str[j++] = i + 'a';
}
}
str[j] = '\0';
}

INPUT:
Enter string1: aspired
Enter string2: despair

48
Name: V.Keerthika Roll No: 1975020
SRI PADMAVATI VISVAVIDYALAYAM, TIRUPATI
(Women’s University)
SCHOOL OF ENGINEERING AND TECHNOLOGY

OUTPUT:
Enter string1: aspired
Enter string2: despair
All characters match in aspired and despair.
The characters that match are: a, d, e, i, p, r, s,

RESULT: Hence, the C program to accept 2 string & check whether all characters in first
string is present in second string & print.
49
Name: V.Keerthika Roll No: 1975020
SRI PADMAVATI VISVAVIDYALAYAM, TIRUPATI
(Women’s University)
SCHOOL OF ENGINEERING AND TECHNOLOGY

Exp No:22 counting the number of Date:26/04/2022


students belonging to each
groups of marks

AIM: To write a C program for given below list of marks obtained by a class of 50 students
in an annual examination.
43,65,51,27,79,11,56,61,82,09,25,36,07,49,55,63,74,81,49,37,40,49,16,75,87,91,33,24,58,78
,65,56,76,67,45,54,36,63,12,21,73,49,51,19,39,49,68,93,85,59. Write a C program to count
the number of students belonging to each of the following groups of marks:
0-9,10-19,20-29,-----100.

SOURCE CODE:

#include <stdio.h>
#define MAXVAL 50
#define COUNTER 11
Void main()
{
Float value[MAXVAL];
Int I, low, high;
Int group[COUNTER] = {0,0,0,0,0,0,0,0,0,0,0};
/* . . . . . . . .READING AND COUNTING . . . . . .*/
For( I = 0 ; I < MAXVAL ; i++ )
{
/*. . . . . . . .READING OF VALUES . . . . . . . . */
Scanf(“%f”, &value[i]) ;
/*. . . . . .COUNTING FREQUENCY OF GROUPS. . . . . */
++ group[ (int) ( value[i] + 0.5 ) / 10] ;
}
/* . . . .PRINTING OF FREQUENCY TABLE . . . . . . .*/
Printf(“\n”);
Printf(“ GROUP RANGE FREQUENCY\n\n”) ;
For( I = 0 ; I < COUNTER ; i++ )
{
Low = I * 10 ;
If(I == 10)
High = 100 ;
Else
High = low + 9 ;
Printf(“ %2d %3d to %3d %d\n”,
I+1, low, high, group[i] ) ;
}
50
Name: V.Keerthika Roll No: 1975020
SRI PADMAVATI VISVAVIDYALAYAM, TIRUPATI
(Women’s University)
SCHOOL OF ENGINEERING AND TECHNOLOGY

Getch();
}

INPUT:

43 65 51 27 79 11 56 61 52 09 25 36 07 49 55 63 74 81 49 37
40 49 16 75 87 91 33 24 58 78 65 56 76 67 45 54 35 63 12 21
73 49 51 19 39 49 68 93 85 59

Output:

43 65 51 27 79 11 56 61 52 09 25 36 07 49 55 63 74 81 49 37
40 49 16 75 87 91 33 24 58 78 65 56 76 67 45 54 35 63 12 21
73 49 51 19 39 49 68 93 85 59

GROUP RANGE FREQUENCY

1 0 to 9 2
2 10 to 19 4
3. 20 to 29 4
4. 30 to 39 5
5 40 to 49 8
6 50 to 59 9
7 60 to 69 7
8 70 to 79 6
9 80 to 89 3
10 90 to 99 2
11 100 to 100 0

RESULT:Hence, the C program for given list of marks obtained by a class of 50 students
in an annual examination is executed successfully.

51
Name: V.Keerthika Roll No: 1975020
SRI PADMAVATI VISVAVIDYALAYAM, TIRUPATI
(Women’s University)
SCHOOL OF ENGINEERING AND TECHNOLOGY

Exp No:23 Finding the distance travelled at regular Date:26/04/2022


intervals of time given the values of 'u' and
'a'

AIM: The total distance travelled by vehicle in 't' seconds is given by distance = ut+1/2at2
where 'u' and 'a' are the initial velocity (m/sec.) and acceleration (m/sec2). Write C
program to find the distance travelled at regular intervals of time given the values of
'u' and 'a'. The program should provide the flexibility to the user to select his own
time intervals and repeat the calculations for different values of 'u' and 'a'.

SOURCE CODE:

#include<stdio.h>
main()
{
int a,u,t,t1,t2,i;
float s;
clrscr();
printf("ENTER THE VALUES OF a,u,t,t1,t2:");
scanf("%d%d%d%d%d",&a,&u,&t,&t1,&t2);
for(i=t1;i<=t2;i=i+t) // performing the looping operation for time intervals
{
s=(u*i)+(0.5*a*i*i); // calculate the total distance
printf("\n\nthe distance travelled in %d seconds is %f ",i,s);
}
getch();
}

INPUT:

ENTER THE VALUES OF a,u,t,t1,t2:


2
3
4
5
6

52
Name: V.Keerthika Roll No: 1975020
SRI PADMAVATI VISVAVIDYALAYAM, TIRUPATI
(Women’s University)
SCHOOL OF ENGINEERING AND TECHNOLOGY

OUTPUT:

2
3
4
5
6

The distance travelled in 5 seconds is 40.000000

RESULT: Hence, to write C program to find the distance travelled at regular intervals of
time given the values of 'u' and 'a' is successfully executed.
53
Name: V.Keerthika Roll No: 1975020
SRI PADMAVATI VISVAVIDYALAYAM, TIRUPATI
(Women’s University)
SCHOOL OF ENGINEERING AND TECHNOLOGY

Exp No:24 Getting source code as Date:26/04/2022


program output.
AIM: Write a C program to print source code as program output.

SOURCE CODE:

#include <stdio.h>
int main(void)
{
// to print the source code
char c;
// _FILE_ gets the location
// of the current C program file
FILE *file = fopen(_FILE_, "r");
Do
{
//printing the contents
//of the file
c = fgetc(file);
putchar(c);
}
while (c != EOF);
fclose(file);
return 0;
}

OUTPUT:

#include <stdio.h>
int main(void)
{
// to print the source code
char c;
// _FILE_ gets the location
// of the current C program file
FILE *file = fopen(_FILE_, "r");
Do
{
//printing the contents
//of the file
c = fgetc(file);
54
Name: V.Keerthika Roll No: 1975020
SRI PADMAVATI VISVAVIDYALAYAM, TIRUPATI
(Women’s University)
SCHOOL OF ENGINEERING AND TECHNOLOGY

putchar(c);
}
while (c != EOF);
fclose(file);
return 0;
}

RESULT:Hence, the C program to print source code as program output has been executed
Successfully.

55
Name: V.Keerthika Roll No: 1975020
SRI PADMAVATI VISVAVIDYALAYAM, TIRUPATI
(Women’s University)
SCHOOL OF ENGINEERING AND TECHNOLOGY

Exp No:25 To write a program using switch and if statements to Date:30/04/2022


compute net amount of a cloth show which has
announced seasonal discounts on purchase of items.

AIM: To write a program using switch and if statements to compute net amount of a cloth
show which has announced seasonal discounts on purchase of items.

SOURCE CODE:

#include<stdio.h>
#include<conio.h>
void main()
{
int ch,pa;
float net;
clrscr();
printf("enter 1.if it is millcloth\n 2. if it is handloom");
scanf("%d",&ch);
printf("\n enter purchased amount");
scanf("%d",&pa);
switch(ch)
{
case 1:
if(pa>=1&&pa<=100)
net=pa;
if(pa>=101&&pa<=200)
net=pa-(5.00/100.00)*pa;
if(pa>=201&&pa<=300)
net=pa-(7.5/100.00)*pa;
if(pa>300)
net=pa-(10.00/100.00)*pa;
break;
case 2:
if(pa>=1&pa<=100)
net=pa-(5.00/100.00)*pa;
if(pa>=101&&pa<=200)
net=pa-(7.5/100.00)*pa;
if(pa>=201&&pa<=300)
net=pa-(10.00/100.00)*pa;
if(pa>300)
net=pa-(15.00/100.00)*pa;
56
Name: V.Keerthika Roll No: 1975020
SRI PADMAVATI VISVAVIDYALAYAM, TIRUPATI
(Women’s University)
SCHOOL OF ENGINEERING AND TECHNOLOGY

break;
}
printf("\n the net amount to be paid is%f",net);
getch();
}

INPUT:
enter 1. if it is millcloth
2. if it is handloome
enter the purchased amount1000

OUTPUT:
enter 1. if it is millcloth
2.if it is handloome
enter the purchased amount1000
The net amount to be paid is 900.000000

RESULT: Hence, the C program using switch and if statements to compute net amount of a
cloth show which has announced seasonal discounts on purchase of items is executed
successfully.
57
Name: V.Keerthika Roll No: 1975020
SRI PADMAVATI VISVAVIDYALAYAM, TIRUPATI
(Women’s University)
SCHOOL OF ENGINEERING AND TECHNOLOGY

Exp No:26 Check whether a given number is EVEN or ODD, Date:30/04/2022


without using any arithmetic or relational
operators.

AIM: To write a C program to check whether a given number is EVEN or ODD, without
using any arithmetic or relational operators.

SOURCE CODE:

#include <stdio.h>

int main()
{
int number;

//input an integer number


printf("Please enter an integer number: ");
scanf("%d",&number);
(number & 0x01) ?
printf("%d is an ODD Number.", number) : printf("%d is an EVEN Number.",number) ;

printf("\n");
return 0;
}

INPUT:

Please enter an integer number: 12

OUTPUT:

Please enter an integer number: 12


12 is an EVEN Number.

RESULT: Hence, the C program to check whether a given number is EVEN or ODD,
without using any arithmetic or relational operators.
58
Name: V.Keerthika Roll No: 1975020
SRI PADMAVATI VISVAVIDYALAYAM, TIRUPATI
(Women’s University)
SCHOOL OF ENGINEERING AND TECHNOLOGY

Exp No:27 Printing your name 10 times without using any Date:30/04/2022
loop or goto statement.

AIM: To write a C program to Print your name 10 times without using any loop or goto
statement.

SOURCE CODE:

#include <stdio.h>
void rec(int i)
{
if(i<=10)
{
printf("%02d Keerthika/n",i);
rec(i+1);
}
}
int main()
{
int i=1;
rec(i);
}

OUTPUT:

01 keerthika
02 keerthika
03 Keerthika
04 Keerthika
05 Keerthika
06 Keerthika
07 Keerthika
08 Keerthika
09 Keerthika
10 Keerthika

RESULT: Hence, to write a C program to print your name 10 times without using loop or
goto statement is successfully executed.
59

You might also like