100% found this document useful (1 vote)
7K views134 pages

CodeTantra C

solutions in c

Uploaded by

Abm
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
100% found this document useful (1 vote)
7K views134 pages

CodeTantra C

solutions in c

Uploaded by

Abm
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/ 134

Date: 2023-12-

Page No: 1
S.No: 1 Exp. Name: Arithmetic operations
22

Aim:

ID: 23CSC25
Write a C program to perform arithmetic operations like +,-,*,/,% on two input variables
num1 and num2.

Input Format:
• The first line of input is an integer representing the value for first number num1
• The second line of input is an integer representing the value of second number
num2

Output Format:
• The program prints the integers that represents the results of addition,
subtraction, multiplication, division, and modulus each on a new line.

2023-27-CSE-C
Note : For Division and Modulo operation, the value of num2 must be greater than 0

Add new line char \n at the end of output.


Source Code:

arithmeticOperations.c

BNM Institute Of Technology


#include<stdio.h>

Page No: 2
#include<stdlib.h>

void main(){
int a, b, s, d, p, q, r;

ID: 23CSC25
printf("num1: ");
scanf("%d", &a);
printf("num2: ");
scanf("%d", &b);

if(b == 0){
s = a+b;
d = a-b;
p = a*b;
printf("Sum: %d\n", s);
printf("Difference: %d\n", d);

2023-27-CSE-C
printf("Product: %d\n", p);
printf("Infinity\n");
printf("Modulo by zero is not allowed\n");
}else{
s = a+b;
d = a-b;

BNM Institute Of Technology


p = a*b;
q = a/b;
r = a%b;
printf("Sum: %d\n", s);
printf("Difference: %d\n", d);
printf("Product: %d\n", p);
printf("Division: %d\n", q);
printf("Modulus: %d\n", r);
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
9
8
17
0
1
1
1

2
72

500
998
2000
1002
1000
User Output
Test Case - 2

BNM Institute Of Technology 2023-27-CSE-C ID: 23CSC25 Page No: 3


Exp. Name: Greatest of three numbers Date: 2023-12-
S.No: 2

Page No: 4
using a conditional operator 17

Aim:
Write a C program to display the greatest of three numbers using a conditional operator

ID: 23CSC25
(ternary operator).

Input Format
The program prompts the user to enter three integers.

Output Format
The program prints the greatest of the three integers.

Source Code:

greatest.c

2023-27-CSE-C
#include<stdio.h>
#include<stdlib.h>

void main(){
int a, b, c;

BNM Institute Of Technology


printf("num1: ");
scanf("%d", &a);
printf("num2: ");
scanf("%d", &b);
printf("num3: ");
scanf("%d", &c);

if(a>=b&&a>=c){
printf("Greatest number: %d\n", a);
}else if(b>=a&&b>=c){
printf("Greatest number: %d\n", b);
}else{
printf("Greatest number: %d\n", c);
}

Execution Results - All test cases have succeeded!


Test Case - 1
User Output

Page No: 5
num1:
8
num2:
9

ID: 23CSC25
num3:
90
Greatest number: 90

Test Case - 2

User Output
num1:
5

2023-27-CSE-C
num2:
45
num3:
6
Greatest number: 45

BNM Institute Of Technology


Date: 2023-12-
S.No: 3 Exp. Name: Relational Operators

Page No: 6
17

Aim:
Write a C program that demonstrates the use of relational operators (<, <=, >, >=, ==,

ID: 23CSC25
!=). Prompt the user to enter two integer values and then use relational operators to
compare the numbers and print the results.

Input Format
The program prompts the user to enter two integers separated by spaces.

Output Format
The program prints the result of various comparison operations between the two
integers.

Note:

2023-27-CSE-C
Refer to the displayed test cases for better understanding.
Source Code:

relationalOperators.c

BNM Institute Of Technology


#include<stdio.h>

Page No: 7
#include<stdlib.h>

void main(){
int a, b;
printf("Enter two numbers: ");

ID: 23CSC25
scanf("%d%d", &a, &b);

if(a<b){
printf("%d < %d: True\n", a, b);
}else{
printf("%d < %d: False\n", a, b);
}

if(a<=b){
printf("%d <= %d: True\n", a, b);
}else{

2023-27-CSE-C
printf("%d <= %d: False\n", a, b);
}

if(a>b){
printf("%d > %d: True\n", a, b);
}else{

BNM Institute Of Technology


printf("%d > %d: False\n", a, b);
}

if(a>=b){
printf("%d >= %d: True\n", a, b);
}else{
printf("%d >= %d: False\n", a, b);
}

if(a==b){
printf("%d == %d: True\n", a, b);
}else{
printf("%d == %d: False\n", a, b);
}

if(a!=b){
printf("%d != %d: True\n", a, b);
}else{
printf("%d != %d: False\n", a, b);
}
}
Execution Results - All test cases have succeeded!

Page No: 8
Test Case - 1

User Output
Enter two numbers:

ID: 23CSC25
5 10
5 < 10: True
5 <= 10: True
5 > 10: False
5 >= 10: False
5 == 10: False
5 != 10: True

Test Case - 2

2023-27-CSE-C
User Output
Enter two numbers:
10 5
10 < 5: False
10 <= 5: False

BNM Institute Of Technology


10 > 5: True
10 >= 5: True
10 == 5: False
10 != 5: True
Exp. Name: Write a C program to find Date: 2023-12-
S.No: 4

Page No: 9
Area and Circumference of a Circle 17

Aim:
Write a program to find the area and circumference of a circle.

ID: 23CSC25
Note: Use the printf() function with a newline character ( \n ) at the end.

area = PI * radius * radius


circumference = 2 * PI * radius

Define PI value as 3.14 using #define and consider float data type for radius
Source Code:

2023-27-CSE-C
circle.c

#include<stdio.h>
#include<stdlib.h>
#define PI 3.14

void main(){

BNM Institute Of Technology


float radius, area, circumference;
printf("radius : ");
scanf("%f", &radius);

area = PI*radius*radius;
circumference = 2*PI*radius;

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


printf("circumference = %f\n", circumference);
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
radius :
2.67
area = 22.384747
circumference = 16.767601

Page No: 10
Test Case - 2

User Output
radius :

ID: 23CSC25
30.05
area = 2835.427734
circumference = 188.713989

Test Case - 3

User Output
radius :
14.69

2023-27-CSE-C
area = 677.599731
circumference = 92.253197

BNM Institute Of Technology


Exp. Name: C program to calculate area Date: 2023-12-
S.No: 5

Page No: 11
of a square. 17

Aim:
Write a C program to calculate the area of a square by taking the side of the square as an

ID: 23CSC25
input.
Source Code:

Area.c
#include<stdio.h>
#include<stdlib.h>

void main(){
int s;
printf("Side: ");

2023-27-CSE-C
scanf("%d", &s);

printf("Area of square: %d\n", (s*s));


}

BNM Institute Of Technology


Execution Results - All test cases have succeeded!
Test Case - 1

User Output
Side:
7
Area of square: 49

Test Case - 2

User Output
Side:
6
Area of square: 36
Exp. Name: Write a C program to
Date: 2023-12-

Page No: 12
S.No: 6 convert the given temperature from
17
Celsius to Fahrenheit

Aim:

ID: 23CSC25
Write a program to convert the given temperature from celsius(C) to fahrenheit(F) .

Note: Use the printf() function with a newline character ( \n ) at the end.

The formula to find fahrenheit is fahrenheit = celsius * (9.0 / 5.0) + 32.0 .


Source Code:

Degrees.c

#include<stdio.h>

2023-27-CSE-C
#include<stdlib.h>

void main(){
float c, f;
printf("C: ");
scanf("%f", &c);

BNM Institute Of Technology


f = ((c*9)/5)+32;

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

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
C:
0
F= 32.000000

Test Case - 2

User Output
C:
32
F= 89.599998

BNM Institute Of Technology 2023-27-CSE-C ID: 23CSC25 Page No: 13


Date: 2023-12-
S.No: 7 Exp. Name: Logical Operators

Page No: 14
17

Aim:
Write a C program that demonstrates the use of logical operators (&&, ||, !) with dynamic

ID: 23CSC25
input.
Prompt the user to enter two integer values and then use logical operators to determine
the following:
• If both numbers are positive or not.
• If at least one number is equal to 5 or not.
• If the first number is not greater than 10 or not.
Input Format:
• The program prompts the user to enter two integers.
Output format:
• The program prints whether both numbers are positive.
• The program prints whether either of the numbers is equal to 5.

2023-27-CSE-C
• The program prints whether the first number is less than or equal to 10.
Source Code:

logicalOperators.c

BNM Institute Of Technology


#include<stdio.h>

Page No: 15
#include<stdlib.h>

void main(){
int a, b;
printf("Enter numbers: ");

ID: 23CSC25
scanf("%d%d", &a, &b);

if(a>0&&b>0){
printf("Both are positive\n");
}else{
printf("Both are not positive\n");
}

if(a==5 || b==5){
printf("%d or %d = 5\n", a, b);
}else{

2023-27-CSE-C
printf("%d,%d != 5\n", a, b);
}

if(a>10){
printf("%d > 10\n", a);
}else{

BNM Institute Of Technology


printf("%d <= 10\n", a);
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
Enter numbers:
15 6
Both are positive
15,6 != 5
15 > 10

Test Case - 2

User Output
5 10

5 <= 10
5 or 10 = 5
Enter numbers:

Both are positive

BNM Institute Of Technology 2023-27-CSE-C ID: 23CSC25 Page No: 16


Exp. Name: Scan all data type variables Date: 2023-12-
S.No: 8

Page No: 17
and display them 17

Aim:
Write a C program to scan all data type variables(int, float, char, double) as input and

ID: 23CSC25
print them as output.

Input Format:
• First Line: An integer, entered after the prompt "integer: ".
• Second Line: A floating-point number, entered after the prompt "floating-point
number: ".
• Third Line: A character, entered after the prompt "character: ".
• Fourth Line: A double-precision floating-point number, entered after the prompt
"double: ".

Output Format:

2023-27-CSE-C
• First Line: A message "You entered:".
• Second Line: The integer entered, in the format "Integer: [integerVar]".
• Third Line: The floating-point number entered, formatted to six decimal places, in
the format "Float: [floatVar]".
• Fourth Line: The character entered, in the format "Character: [charVar]".
• Fifth Line: The double-precision floating-point number entered, formatted to six

BNM Institute Of Technology


decimal places, in the format "Double: [doubleVariable]".

Note: Please add Space before %c which removes any white space (blanks, tabs, or
newlines).
Source Code:

scan.c
#include<stdio.h>

Page No: 18
#include<stdlib.h>

void main(){
int a;
float b;

ID: 23CSC25
char c;
double d;

printf("integer: ");
scanf("%d", &a);
printf("floating-point number: ");
scanf("%f", &b);
printf("character: ");
scanf(" %c", &c);
printf("double: ");
scanf("%lf", &d);

2023-27-CSE-C
printf("You entered:\nInteger: %d\nFloat: %f\nCharacter:
%c\nDouble: %lf", a, b, c, d);

BNM Institute Of Technology


Execution Results - All test cases have succeeded!
Test Case - 1

User Output
integer:
9
floating-point number:
12.0254
character:
C
double:
12.02543124
You entered:
Integer: 9
Float: 12.025400
Character: C
Double: 12.025431
Test Case - 2

Page No: 19
User Output
integer:
-10
floating-point number:

ID: 23CSC25
12.2546
character:
T
double:
12.6789678
You entered:
Integer: -10
Float: 12.254600
Character: T

2023-27-CSE-C
Double: 12.678968

BNM Institute Of Technology


Exp. Name: Write a C program to find Date: 2023-12-
S.No: 9

Page No: 20
Sum and Average of three numbers 17

Aim:
Write a program to find the sum and average of the three given integers.

ID: 23CSC25
Note: Use the printf() function with a newline character ( \n ) at the end.
Source Code:

Program314.c
#include<stdio.h>
#include<stdlib.h>

void main(){
int a, b, c;

2023-27-CSE-C
float sum, avg;
printf("Enter three integers : ");
scanf("%d%d%d", &a, &b, &c);

sum = a+b+c;
printf("Sum of %d, %d and %d : %.0f\n", a, b, c, sum);

BNM Institute Of Technology


avg = (sum/3);
printf("Average of %d, %d and %d : %f\n", a, b, c, avg);
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
Enter three integers :
121 34 56
Sum of 121, 34 and 56 : 211
Average of 121, 34 and 56 : 70.333336

Test Case - 2

User Output
Enter three integers :
583
Sum of 5, 8 and 3 : 16

Page No: 21
Average of 5, 8 and 3 : 5.333333

Test Case - 3

ID: 23CSC25
User Output
Enter three integers :
-1 5 -6
Sum of -1, 5 and -6 : -2
Average of -1, 5 and -6 : -0.666667

2023-27-CSE-C
BNM Institute Of Technology
Exp. Name: C program to convert the
Date: 2023-12-

Page No: 22
S.No: 10 given distance from miles to
17
kilometers.

Aim:

ID: 23CSC25
Write a program to convert the given distance from miles to kilometers .

Note: Note that 1.609 kilometers is equal to 1 mile . Use the printf() function with a
newline character ( \n ) at the end.
Source Code:

Distance.c

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

2023-27-CSE-C
void main(){
float d;
printf("Enter the distance in miles : ");
scanf("%f", &d);
float k = d*1.609;

BNM Institute Of Technology


printf("Distance in kilometers = %f\n", k);
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
Enter the distance in miles :
11.5
Distance in kilometers = 18.503500

Test Case - 2

User Output
Enter the distance in miles :
25
Distance in kilometers = 40.224998
Exp. Name: Extract the last two digits of Date: 2023-12-
S.No: 11

Page No: 23
a given integer n 17

Aim:
Write a C program to extract the last two digits of a given integer n, where the number of

ID: 23CSC25
digits should be greater than 2.

Source Code:

extract.c

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

void main(){
int a, r, b, i;

2023-27-CSE-C
printf("Integer: ");
scanf("%d", &a);

if(a>99){
for(i=1; i<=2; i++){
r = a%100;

BNM Institute Of Technology


}
if(r>=0 && r<=9){
printf("Last two digits: 0%d\n", r);
}else{
printf("Last two digits: %d\n", r);
}
}else{
printf("The number should be greater than 99\n");
}
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
Integer:
99
The number should be greater than 99
Test Case - 2

Page No: 24
User Output
Integer:
1000
Last two digits: 00

ID: 23CSC25
Test Case - 3

User Output
Integer:
456987
Last two digits: 87

2023-27-CSE-C
BNM Institute Of Technology
Exp. Name: Factors of a given number Date: 2023-12-
S.No: 12

Page No: 25
and check whether it is a prime or not. 17

Aim:
Write a C program to display the factors of a given number and check whether it is a

ID: 23CSC25
prime or not.
Source Code:

factorsAndIsPrime.c

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

void main(){
int a, i, c;
printf("Integer: ");

2023-27-CSE-C
scanf("%d", &a);

printf("Factors: ");
for(i=1; i<=a; i++){
if(a%i == 0){
printf("%d ", i);

BNM Institute Of Technology


c++;
}
}
if(c>2){
printf("\nNot a prime number\n");
}else{
printf("\nPrime number\n");
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
Integer:
16
Factors: 1 2 4 8 16
Not a prime number

Page No: 26
Test Case - 2

User Output
Integer:

ID: 23CSC25
13
Factors: 1 13
Prime number

2023-27-CSE-C
BNM Institute Of Technology
Exp. Name: C program for finding the Date: 2023-12-
S.No: 13

Page No: 27
largest of three given numbers. 17

Aim:
Write a C program for finding the largest of three given numbers.

ID: 23CSC25
Source Code:

max.c
#include<stdio.h>
#include<stdlib.h>

void main(){
int a, b, c;
//printf("Enter the numbers: ");
scanf("%d%d%d", &a, &b, &c);

2023-27-CSE-C
if(a>b && a>c){
printf("%d", a);
}else if(b>a && b>c){
printf("%d", b);
}else{

BNM Institute Of Technology


printf("%d", c);
}
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
15 12 17
17

Test Case - 2

User Output
105 21 86
105
Exp. Name: Convert upper case Date: 2023-12-
S.No: 14

Page No: 28
character to lowercase and vice versa 17

Aim:
Write a C program to convert upper case characters to lowercase and vice versa.

ID: 23CSC25
Source Code:

changeCharCase.c

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void main(){
char s[100];
int len, i;
printf("Enter a string: ");

2023-27-CSE-C
scanf("%s", s);
len = strlen(s);

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


if(s[i]>='A' && s[i]<='Z'){
s[i] += 32;

BNM Institute Of Technology


}else if(s[i]>='a' && s[i]<='z'){
s[i] -= 32;
}
}
printf("Result: %s\n", s);

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
Enter a string:
CodeTantra
Result: cODEtANTRA
User Output

Page No: 29
Enter a string:
aabbccDDEEFFGG
Result: AABBCCddeeffgg

ID: 23CSC25
2023-27-CSE-C
BNM Institute Of Technology
Exp. Name: Check string is palindrome Date: 2023-12-
S.No: 15

Page No: 30
or not 17

Aim:
Write a C program to check whether a given string is Palindrome or not by passing

ID: 23CSC25
parameters to Function & returning value.
Source Code:

stringPalindrome.c

2023-27-CSE-C
BNM Institute Of Technology
#include<stdio.h>

Page No: 31
#include<stdlib.h>
#include<string.h>

int toLower(char ch);


int isPalindrome(char str[], int length);

ID: 23CSC25
void main(){
char a[100];
int len;
printf("string: ");
scanf("%s", a);

len = strlen(a);

if(isPalindrome(a, len)){

2023-27-CSE-C
printf("palindrome\n");
}else{
printf("not a palindrome\n");
}

BNM Institute Of Technology


int toLower(char ch){
if(ch>='A' && ch<='Z'){
return ch+32;
}
return ch;
}

int isPalindrome(char str[], int length){


int left = 0;
int right = length - 1;

while(left < right){


if(toLower(str[left]) != toLower(str[right])){
return 0;
}
left++;
right--;
}
return 1;
}
Execution Results - All test cases have succeeded!

Page No: 32
Test Case - 1

User Output
string:

ID: 23CSC25
mam
palindrome

Test Case - 2

User Output
string:
CodeTantra
not a palindrome

2023-27-CSE-C
BNM Institute Of Technology
Exp. Name: Check whether a given input
Date: 2023-12-

Page No: 33
S.No: 16 integer is in between two values x and
17
y.

Aim:

ID: 23CSC25
Write a C program to check whether a given input integer is in between two values x and
y.
Source Code:

between.c

#include<stdio.h>
#include<stdlib.h>
void main(){
int x, y, z;
printf("lower bound(x): ");

2023-27-CSE-C
scanf("%d", &x);
printf("upper bound(y): ");
scanf("%d", &y);
printf("number to check: ");
scanf("%d", &z);

BNM Institute Of Technology


if(z>x && z<y){
printf("%d is in between %d and %d\n", z, x, y);
}else{
printf("%d is not in between %d and %d\n", z, x, y);
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
lower bound(x):
8
upper bound(y):
100
number to check:
16
16 is in between 8 and 100

Page No: 34
Test Case - 2

User Output
lower bound(x):

ID: 23CSC25
1
upper bound(y):
15
number to check:
30
30 is not in between 1 and 15

2023-27-CSE-C
BNM Institute Of Technology
Exp. Name: Demonstrate the usage of Date: 2023-12-
S.No: 17

Page No: 35
unconditional control statements 17

Aim:
Write a C Program to demonstrate the usage of unconditional control statements by

ID: 23CSC25
printing the square of numbers upto n.
Source Code:

unconditional.c

#include <stdio.h>
int main() {
int choice, num, i;

label:
printf("Choose an option:\n");

2023-27-CSE-C
printf("1.Print squares upto n\n");
printf("2.Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:

BNM Institute Of Technology


printf("n: ");
scanf("%d", &num);
for(i=1; i<=num; i++){
printf("%d ", (i*i));
}
printf("\n");
goto label;

case 2:
printf("Exited\n");
break;
default:
printf("Invalid choice\n");
goto label;
}

return 0;
}

Execution Results - All test cases have succeeded!


Test Case - 1

Page No: 36
User Output
Choose an option:
1.Print squares upto n
2.Exit

ID: 23CSC25
Enter your choice:
1
n:
9
1 4 9 16 25 36 49 64 81
Choose an option:
1.Print squares upto n
2.Exit
Enter your choice:

2023-27-CSE-C
2
Exited

Test Case - 2

BNM Institute Of Technology


User Output
Choose an option:
1.Print squares upto n
2.Exit
Enter your choice:
15
Invalid choice
Choose an option:
1.Print squares upto n
2.Exit
Enter your choice:
1
n:
5
1 4 9 16 25
Choose an option:
1.Print squares upto n
2.Exit
Enter your choice:
2
Test Case - 3

Page No: 37
User Output
Choose an option:
1.Print squares upto n
2.Exit

ID: 23CSC25
Enter your choice:
1
n:
1
1
Choose an option:
1.Print squares upto n
2.Exit
Enter your choice:

2023-27-CSE-C
10
Invalid choice
Choose an option:
1.Print squares upto n
2.Exit
Enter your choice:

BNM Institute Of Technology


2
Exited
Exp. Name: Input month number and
Date: 2023-12-

Page No: 38
S.No: 18 print number of days in that month
17
using switch.

Aim:

ID: 23CSC25
Write the C program to Input month number and print number of days in that month
using switch.
Source Code:

Divisibility.c
#include<stdio.h>
#include<stdlib.h>
void main(){
int m;
printf("Enter month number: ");

2023-27-CSE-C
scanf("%d", &m);

switch(m){
case 1:
case 3:
case 5:

BNM Institute Of Technology


case 7:
case 8:
case 10:
case 12:
printf("31 days\n");
break;
case 4:
case 6:
case 9:
case 11:
printf("30 days\n");
break;
case 2:
printf("28 or 29 days\n");
break;
default:
printf("Invalid month number\n");
break;
}
}
Execution Results - All test cases have succeeded!

Page No: 39
Test Case - 1

User Output
Enter month number:

ID: 23CSC25
9
30 days

Test Case - 2

User Output
Enter month number:
2
28 or 29 days

2023-27-CSE-C
Test Case - 3

User Output
Enter month number:

BNM Institute Of Technology


13
Invalid month number
Exp. Name: Print odd numbers between Date: 2023-12-
S.No: 19

Page No: 40
specified range. 17

Aim:
Write a C program to print odd numbers between a specified range.

ID: 23CSC25
Source Code:

oddNumbers.c

#include<stdio.h>
#include<stdlib.h>
void main(){
int a, b;
printf("Enter the range: ");
scanf("%d%d", &a, &b);

2023-27-CSE-C
if(a>b){
printf("Invalid range\n");
}else{
for(;a<=b; a++){
if(a%2!=0){
printf("%d ", a);

BNM Institute Of Technology


}
}
}
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
Enter the range:
19
1 3 5 7 9

Test Case - 2

User Output
Enter the range:
Invalid range

BNM Institute Of Technology 2023-27-CSE-C ID: 23CSC25 Page No: 41


Exp. Name: Converting years using Date: 2023-12-
S.No: 20

Page No: 42
switch 17

Aim:
Write a program to convert years into seconds, minutes, hours, days and months using a

ID: 23CSC25
switch statement
Note:
• Consider no.of days in a year as 365
Source Code:

convertYears.c

2023-27-CSE-C
BNM Institute Of Technology
#include<stdio.h>

Page No: 43
#include<stdlib.h>

void main(){
int n, ch, a;

ID: 23CSC25
printf("Enter number of years:");
scanf("%d", &n);
printf("1.seconds\n2.minutes\n3.hours\n4.days\n5.months\nSelect
an option:");
scanf("%d", &ch);

switch(ch){
case 1:
a = n*365*24*60*60;
printf("Years to seconds:%d\n", a);

2023-27-CSE-C
break;
case 2:
a = n*365*24*60;
printf("Years to minutes:%d\n", a);
break;
case 3:

BNM Institute Of Technology


a = n*365*24;
printf("Years to hours:%d\n", a);
break;
case 4:
a = n*365;
printf("Years to days:%d\n", a);
break;
case 5:
a = n*12;
printf("Years to months:%d\n", a);
break;
default:
printf("Invalid choice\n");
break;
}
}

Execution Results - All test cases have succeeded!


Test Case - 1
User Output

Page No: 44
Enter number of years:
8
1.seconds
2.minutes
3.hours

ID: 23CSC25
4.days
5.months
Select an option:
1
Years to seconds:252288000

Test Case - 2

User Output

2023-27-CSE-C
Enter number of years:
9
1.seconds
2.minutes
3.hours

BNM Institute Of Technology


4.days
5.months
Select an option:
2
Years to minutes:4730400

Test Case - 3

User Output
Enter number of years:
5
1.seconds
2.minutes
3.hours
4.days
5.months
Select an option:
7
Invalid choice
Test Case - 4

Page No: 45
User Output
Enter number of years:
2
1.seconds

ID: 23CSC25
2.minutes
3.hours
4.days
5.months
Select an option:
3
Years to hours:17520

Test Case - 5

2023-27-CSE-C
User Output
Enter number of years:
2
1.seconds

BNM Institute Of Technology


2.minutes
3.hours
4.days
5.months
Select an option:
6
Invalid choice

Test Case - 6

User Output
Enter number of years:
3
1.seconds
2.minutes
3.hours
4.days
5.months
Select an option:
Years to days:1095

BNM Institute Of Technology 2023-27-CSE-C ID: 23CSC25 Page No: 46


Exp. Name: C program to find sum of Date: 2023-12-
S.No: 21

Page No: 47
digits of given number. 17

Aim:
Write a program to find the sum of digits of a given number using recursion process.

ID: 23CSC25
Note: Write the recursive function sumOfDigits() in Sum1.c .
Source Code:

Sum.c

#include <stdio.h>
#include "Sum1.c"
void main() {
int n;
printf("Enter an integer value : ");

2023-27-CSE-C
scanf("%d", &n);
printf("Sum of digits of given number %d = %d\n", n,
sumOfDigits(n));
}

BNM Institute Of Technology


Sum1.c
#include<stdio.h>
#include<stdlib.h>

int sumOfDigits(int n);

int sumOfDigits(int n){


if(n == 0){
return 0;
}else{
return ((n%10)+(sumOfDigits(n/10)));
}
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
Enter an integer value :
1234

Page No: 48
Sum of digits of given number 1234 = 10

Test Case - 2

ID: 23CSC25
User Output
Enter an integer value :
5638
Sum of digits of given number 5638 = 22

2023-27-CSE-C
BNM Institute Of Technology
Exp. Name: Write a C program to find
Date: 2023-12-

Page No: 49
S.No: 22 the Addition of Two matrices using
17
Functions

Aim:

ID: 23CSC25
Write a program to find the addition of two matrices using functions.

 ote: Write the functions read1(), display() and additionOfTwoMatrices() in


N
matricesAddition.c
Source Code:

readMatrices.c

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

2023-27-CSE-C
int a[10][10], b[10][10], m, n, p, q;
printf("Enter the size of the first matrix : ");
scanf("%d%d", &m, &n);
read1(a, m, n);
printf("Enter the size of the second matrix : ");
scanf("%d%d", &p, &q);

BNM Institute Of Technology


read1(b, p, q);
printf("The first matrix is\n");
display(a, m, n);
printf("The second matrix is\n");
display(b, p, q);
if (m == p && n == q) {
additionOfTwoMatrices(a, b, m, n);
} else {
printf("Addition is not possible\n");
}
}

matricesAddition.c
#include<stdio.h>

Page No: 50
#include<stdlib.h>

void read1(int arr[10][10], int a, int b);


void display(int arr[10][10], int a, int b);
void additionOfTwoMatrices(int L[10][10], int M[10][10], int a, int b);

ID: 23CSC25
void read1(int arr[10][10], int a, int b){
int i, j;
printf("Enter %d elements : ", (a*b));
for(i=0; i<a; i++){
for(j=0; j<b; j++){
scanf("%d", &arr[i][j]);
}
}
}

2023-27-CSE-C
void display(int arr[10][10], int a, int b){
int i, j;
for(i=0; i<a; i++){
for(j=0; j<b; j++){
printf("%d ", arr[i][j]);
}

BNM Institute Of Technology


printf("\n");
}

void additionOfTwoMatrices(int L[10][10], int M[10][10], int a, int b){


printf("The Addition Matrix is\n");
//int S[a][b];
int i, j;
for(i=0; i<a; i++){
for(j=0; j<b; j++){
//S[i][j] = L[i][j] + M[i][j];
printf("%d ", (L[i][j]+M[i][j]));
}
printf("\n");
}

}
Execution Results - All test cases have succeeded!

Page No: 51
Test Case - 1

User Output
Enter the size of the first matrix :

ID: 23CSC25
23
Enter 6 elements :
123456
Enter the size of the second matrix :
23
Enter 6 elements :
965847
The first matrix is
1 2 3

2023-27-CSE-C
4 5 6
The second matrix is
9 6 5
8 4 7
The Addition Matrix is
10 8 8

BNM Institute Of Technology


12 9 13

Test Case - 2

User Output
Enter the size of the first matrix :
22
Enter 4 elements :
45 15 35 62
Enter the size of the second matrix :
23
Enter 6 elements :
63 95 84 72 35 62
The first matrix is
45 15
35 62
The second matrix is
63 95 84
72 35 62
Exp. Name: Write a program to find the Date: 2023-12-
S.No: 23

Page No: 52
Sum of Series using Recursion 17

Aim:
Write a program to find the sum of series 11 + 22 + 33 + ....... using recursion

ID: 23CSC25
process.

Source Code:

SumOfSeries.c

#include <stdio.h>
#include "SumOfSeries1.c"
void main() {
long int n;
printf("Enter value of n : ");

2023-27-CSE-C
scanf("%li", &n);
printf("Sum of the series = %li\n", sumOfSeries(n));
}

SumOfSeries1.c

BNM Institute Of Technology


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

long int sumOfSeries(long int n);

long int sumOfSeries(long int n){


if(n==0){
return 0;
}else{
return ((pow(n,n))+(sumOfSeries(n-1)));
}
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
Enter value of n :
4

Page No: 53
Sum of the series = 288

Test Case - 2

ID: 23CSC25
User Output
Enter value of n :
3
Sum of the series = 32

Test Case - 3

User Output

2023-27-CSE-C
Enter value of n :
2
Sum of the series = 5

Test Case - 4

BNM Institute Of Technology


User Output
Enter value of n :
7
Sum of the series = 873612

Test Case - 5

User Output
Enter value of n :
1
Sum of the series = 1
Date: 2023-12-
S.No: 24 Exp. Name: Transpose using functions.

Page No: 54
30

Aim:
Write a C program to print the transpose of a matrix using functions.

ID: 23CSC25
Input Format
• First Line: The user will input the number of rows for the matrix.
• Second Line: The user will input the number of columns for the matrix.
• Subsequent Lines: The user will input the matrix elements row by row.

Output Format
• First Line: The program will print the matrix in its original form.
• Second Line: The program will print the transpose of the matrix.
Source Code:

2023-27-CSE-C
transpose.c

BNM Institute Of Technology


#include <stdio.h>

Page No: 55
#include <stdlib.h>

int rows=0, cols=0;

void readMatrix(int A[100][100]);

ID: 23CSC25
void printMatrix(int A[100][100]);
void transposeMatrix(int A[100][100]);

int main() {
printf("rows: ");
scanf("%d", &rows);
printf("columns: ");
scanf("%d", &cols);
int matrix[rows][cols];

// Input: Read the matrix elements

2023-27-CSE-C
readMatrix(matrix);

// Print the original matrix


printMatrix(matrix);

// Print the transpose of the matrix

BNM Institute Of Technology


transposeMatrix(matrix);

return 0;
}
void readMatrix(int A[100][100]){
int i, j;
printf("Elements:\n");
for(i=0; i<rows; i++){
for(j=0; j<cols; j++){
scanf("%d", &A[i][j]);
}
}
}

void printMatrix(int A[100][100]){


int i, j;
printf("Matrix:\n");
for(i=0; i<rows; ++i){
for(j=0; j<cols; ++j){
printf("%d ", A[i][j]);
}
printf("\n");
}
}

Page No: 56
void transposeMatrix(int A[100][100]){

ID: 23CSC25
int i,j, B[cols][rows];
for(i=0; i<rows; ++i){
for(j=0; j<cols; ++j){
B[j][i] = A[i][j];
}
}
printf("Transpose:\n");

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


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

2023-27-CSE-C
printf("%d ", B[i][j]);
}
printf("\n");
}
}

BNM Institute Of Technology


Execution Results - All test cases have succeeded!
Test Case - 1

User Output
rows:
2
columns:
2
Elements:
89
65
Matrix:

Page No: 57
8 9
6 5
Transpose:
8 6

ID: 23CSC25
9 5

Test Case - 2

User Output
rows:
1
columns:
2

2023-27-CSE-C
Elements:
69
Matrix:
6 9
Transpose:

BNM Institute Of Technology


6
9
Exp. Name: Check whether the given

Page No: 58
integer is Armstrong or not and display Date: 2023-12-
S.No: 25
the sum of individual digits raised to 17
power of n.

ID: 23CSC25
Aim:
Write a C program to check whether the given integer is Armstrong or not and display
the sum of individual digits of a given integer raised to the power of n.
Source Code:

SumAndArmstrong.c

2023-27-CSE-C
BNM Institute Of Technology
#include <stdio.h>

Page No: 59
#include <math.h>
#include <stdlib.h>

int arm(int num, int size);

ID: 23CSC25
void main(){
int m, n, a, b, i, c;
printf("Integer: ");
scanf("%d", &m);
printf("Power(n): ");
scanf("%d", &n);

a = m;

c = 0;
while(a!=0){

2023-27-CSE-C
c++;
a/=10;
}

i = arm(m, c);

BNM Institute Of Technology


if(m==i){
printf("Armstrong number\n");
}else{
printf("Not an Armstrong number\n");
}

printf("Sum: %d\n", arm(m, n));

int arm(int num, int size){


if(num == 0){
return 0;
}else{
return ((pow(num%10, size))+(arm(num/10, size)));
}
}
Execution Results - All test cases have succeeded!

Page No: 60
Test Case - 1

User Output
Integer:

ID: 23CSC25
153
Power(n):
2
Armstrong number
Sum: 35

Test Case - 2

User Output

2023-27-CSE-C
Integer:
121
Power(n):
2
Not an Armstrong number

BNM Institute Of Technology


Sum: 6
Exp. Name: Write a C program to
Date: 2023-12-

Page No: 61
S.No: 26 display the Fibonacci series up to the
17
given number of terms using Recursion

Aim:

ID: 23CSC25
Write a program to display the fibonacci series up to the given number of terms
using recursion process.

The fibonacci series is 0 1 1 2 3 5 8 13 21 34...... .

Note: Write the recursive function fib() in fibonacci1.c .


Source Code:

fibonacci.c

2023-27-CSE-C
#include <stdio.h>
#include "fibonacci1.c"
void main() {
int n, i;
printf("Enter value of n : ");
scanf("%d", &n);

BNM Institute Of Technology


printf("The fibonacci series of %d terms are : ", n);
for (i = 0; i < n; i++) {
printf(" %d ", fib(i));
}
}

fibonacci1.c
#include<stdio.h>
#include<stdlib.h>

int fib(int n);

int fib(int n){


if(n<=1){
return n;
}else{
return ((fib(n-1))+(fib(n-2)));
}
}
Execution Results - All test cases have succeeded!

Page No: 62
Test Case - 1

User Output
Enter value of n :

ID: 23CSC25
4
The fibonacci series of 4 terms are : 0 1 1 2

Test Case - 2

User Output
Enter value of n :
8
The fibonacci series of 8 terms are : 0 1 1 2 3 5 8 13

2023-27-CSE-C
Test Case - 3

User Output
Enter value of n :

BNM Institute Of Technology


14
The fibonacci series of 14 terms are : 0 1 1 2 3 5 8 13 21
34 55 89 144 233

Test Case - 4

User Output
Enter value of n :
3
The fibonacci series of 3 terms are : 0 1 1
Exp. Name: Write a program to compute Date: 2023-12-
S.No: 27

Page No: 63
the following equation. 17

Aim:
Write a C program to compute the binomial coefficient ncr.

ID: 23CSC25
The recursive formula for factorial is:
n! = n * (n - 1)!; if n > 0 (Recursive criteria)
n! = 1; if n = 0 (Base criteria)

Refer to the displayed sample test cases to strictly match the input and output layout.
Source Code:

FindingNcr.c

#include <stdio.h>

2023-27-CSE-C
#include "RecursiveFactorial.c"
void main() {
long int n, r;
printf("Enter the value of n and r : ");
scanf("%ld%ld", &n, &r);
printf("The value of %ldc%ld = %ld\n", n, r, FindNcr(n, r));
}

BNM Institute Of Technology


RecursiveFactorial.c
long int factorial(long int n) {

Page No: 64
if(n<=1){
return 1;
}else{
return ((n)*(factorial(n-1)));
}

ID: 23CSC25
}
long int FindNcr1(long int n, long int r) {
if(n==0){
return 1;
}else{
return factorial(n)/((factorial(n-r))*(factorial(r)));
}
}

long int FindNcr(long int n, long int r){


if(r==0 || r==n){

2023-27-CSE-C
return 1;
}else{
return ((FindNcr(n-1, r-1))+(FindNcr1(n-1, r)));
}

BNM Institute Of Technology


Execution Results - All test cases have succeeded!
Test Case - 1

User Output
Enter the value of n and r :
62
The value of 6c2 = 15

Test Case - 2

User Output
Enter the value of n and r :
53
The value of 5c3 = 10
Test Case - 3

Page No: 65
User Output
Enter the value of n and r :
94
The value of 9c4 = 126

ID: 23CSC25
Test Case - 4

User Output
Enter the value of n and r :
14 9
The value of 14c9 = 2002

2023-27-CSE-C
BNM Institute Of Technology
Exp. Name: Write a C program to find
Date: 2023-12-

Page No: 66
S.No: 28 the Multiplication of two matrices
24
using Functions.

Aim:

ID: 23CSC25
Write a program to find the multiplication of two matrices using functions.

Note: Write the functions read1(), display() and mulitiplicationOfTwoMatrices() in


matrixMul.c
Source Code:

readMatrices.c

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

2023-27-CSE-C
int a[10][10], b[10][10], m, n, p, q;
printf("Enter the size of the first matrix : ");
scanf("%d%d", &m, &n);
read1(a, m, n);
printf("Enter the size of the second matrix : ");
scanf("%d%d", &p, &q);

BNM Institute Of Technology


read1(b, p, q);
printf("The first matrix is\n");
display(a, m, n);
printf("The second matrix is\n");
display(b, p, q);
if (n == p) {
multiplicationOfTwoMatrices(a, b, m, n, q);
} else {
printf("Multiplication is not possible\n");
}
}

matrixMul.c
#include<stdio.h>

Page No: 67
#include<stdlib.h>

void read1(int arr[10][10], int a, int b);


void display(int arr[10][10], int a, int b);
void multiplicationOfTwoMatrices(int L[10][10], int M[10][10], int a,

ID: 23CSC25
int b, int c);

void read1(int arr[10][10], int a, int b){


int i, j;
printf("Enter %d elements : ", (a*b));
for(i=0; i<a; i++){
for(j=0; j<b; j++){
scanf("%d", &arr[i][j]);
}
}

2023-27-CSE-C
}

void display(int arr[10][10], int a, int b){


int i, j;
for(i=0; i<a; i++){
for(j=0; j<b; j++){

BNM Institute Of Technology


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

void multiplicationOfTwoMatrices(int L[10][10], int M[10][10], int a,


int b, int c){
int i, j, k;
printf("The resultant matrix is\n");
int R[10][10];
for(i=0; i<a; i++){
for(j=0; j<c; j++){
R[i][j] = 0;
for(k=0; k<b; k++){
R[i][j] += L[i][k] * M[k][j];
}
}
}
display(R, a, c);
}

Page No: 68
Execution Results - All test cases have succeeded!

ID: 23CSC25
Test Case - 1

User Output
Enter the size of the first matrix :
23
Enter 6 elements :
869547
Enter the size of the second matrix :
32

2023-27-CSE-C
Enter 6 elements :
659872
The first matrix is
8 6 9
5 4 7

BNM Institute Of Technology


The second matrix is
6 5
9 8
7 2
The resultant matrix is
165 106
115 71

Test Case - 2

User Output
Enter the size of the first matrix :
23
Enter 6 elements :
65 95 85 45 25 35
Enter the size of the second matrix :
23
Enter 6 elements :
96 85 74 25 36 14
65 95 85
45 25 35

Page No: 69
The second matrix is
96 85 74
25 36 14
Multiplication is not possible

ID: 23CSC25
2023-27-CSE-C
BNM Institute Of Technology
Exp. Name: Program to find the LCM of Date: 2023-12-
S.No: 29

Page No: 70
the given numbers 22

Aim:
Write a Program to find the LCM of the given numbers

ID: 23CSC25
Use the concept of functions to write the code
Source Code:

lcm.c

#include <stdio.h>
int gcd(int, int);
void main() {
int a, b, c, d;
printf("Enter two integer values: ");

2023-27-CSE-C
scanf("%d%d", &a, &b);
c = a*b;
d = gcd(a, b);
printf("The lcm of two numbers %d and %d: %d\n", a, b, (c/d));
}

BNM Institute Of Technology


int gcd(int a, int b) {
int r = a%b;
if(r==0){
return b;
}else{
return (gcd(b, r));
}
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
Enter two integer values:
32
The lcm of two numbers 3 and 2: 6
Test Case - 2

Page No: 71
User Output
Enter two integer values:
12 144
The lcm of two numbers 12 and 144: 144

ID: 23CSC25
2023-27-CSE-C
BNM Institute Of Technology
Exp. Name: Write a C Program to find
Date: 2023-12-

Page No: 72
S.No: 30 the Power of a given number using
22
Recursion

Aim:

ID: 23CSC25
Write a program to find the power of a given number using recursion process.

Note: Write the recursive function power() in Power1.c .


Source Code:

Power.c

#include <stdio.h>
#include "Power1.c"
void main() {
int m, n;

2023-27-CSE-C
printf("Enter a number : ");
scanf("%d", &m);
printf("Enter power : ");
scanf("%d", &n);
printf("%d to the power of %d is : %d\n", m, n, power(m, n));
}

BNM Institute Of Technology


Power1.c

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

int power(int m, int n);

int power(int m, int n){


if(n==0){
return 1;
}else{
return m * power(m, (n-1));
}
}

Execution Results - All test cases have succeeded!


Test Case - 1
User Output

Page No: 73
Enter a number :
3
Enter power :
4

ID: 23CSC25
3 to the power of 4 is : 81

Test Case - 2

User Output
Enter a number :
4
Enter power :
3

2023-27-CSE-C
4 to the power of 3 is : 64

BNM Institute Of Technology


Exp. Name: Create an array and
Date: 2023-12-

Page No: 74
S.No: 31 perform display, insert at location and
25
delete at location operations.

Aim:

ID: 23CSC25
Write a C program that initially stores user-provided numbers in an array, displays them.
And perform below mentioned operations on the array:
1. Insert a number at a specified location: The program should allow the user to
insert a number at any location in the array, shifting the other elements as
necessary.
2. Delete a number from a specified location: The program should allow the user
to delete a number from a given location in the array, shifting the remaining
elements to fill the gap.
3. Display the array contents: After each operation (insert or delete), the program
should display the current elements of the array.
Ensure that the location provided by the user is valid (i.e., it is within the bounds of the

2023-27-CSE-C
array).

Note:
• Refer to the displayed test cases for better understanding.
• The driver function is already given in the editor.
Source Code:

BNM Institute Of Technology


array.c
#include <stdio.h>

Page No: 75
int size, arr[10];
void displayArray();
void insertNumber(int a, int b);
void deleteNumber(int b);

ID: 23CSC25
void displayArray(){
int i;
printf("Array elements: ");
for(i=0; i<size; i++){
printf("%d ", arr[i]);
}
printf("\n");
}

void insertNumber(int a, int b){


int i;

2023-27-CSE-C
if(a>size){
printf("Invalid location\n");
}else{
size++;
printf("%d inserted at location %d\n", b, a);
for(i=a; i<size; i++){

BNM Institute Of Technology


if(i<size-1){
arr[i+1] = arr[i];
}else{
arr[i] = b;
}
}
arr[a] = b;
}
}

void deleteNumber(int a){


int i;
if(a>=size){
printf("Invalid location\n");
}else{
printf("%d deleted from location %d\n", arr[a], a);
for(i=a; i<size+1; i++){
if(i<size-1){
arr[i] = arr[i+1];
}else{
size--;
}
}
}

Page No: 76
}

ID: 23CSC25
int main() {
printf("Enter size: ");
scanf("%d", &size);

printf("Enter %d elements: ", size);


for (int i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}

displayArray();

2023-27-CSE-C
int choice, location, number;

printf("1. Insert a number\n2. Delete a number\n3. Exit\nEnter your


choice: ");
scanf("%d", &choice);

BNM Institute Of Technology


while (choice == 1 || choice == 2) {
if (choice == 1) {
printf("Location: ");
scanf("%d", &location);
printf("Number: ");
scanf("%d", &number);
insertNumber(location, number);
} else if (choice == 2) {
printf("Location: ");
scanf("%d", &location);
deleteNumber(location);
}

displayArray();

printf("1. Insert a number\n2. Delete a number\n3. Exit\nEnter


your choice: ");
scanf("%d", &choice);
}

return 0;
}
Execution Results - All test cases have succeeded!

Page No: 77
Test Case - 1

User Output
Enter size:

ID: 23CSC25
5
Enter 5 elements:
12 15 16 14 18
Array elements: 12 15 16 14 18
1. Insert a number
2. Delete a number
3. Exit
Enter your choice:
2
Location:

2023-27-CSE-C
4
18 deleted from location 4
Array elements: 12 15 16 14
1. Insert a number
2. Delete a number

BNM Institute Of Technology


3. Exit
Enter your choice:
2
Location:
4
Invalid location
Array elements: 12 15 16 14
1. Insert a number
2. Delete a number
3. Exit
Enter your choice:
1
Location:
4
Number:
10
10 inserted at location 4
Array elements: 12 15 16 14 10
1. Insert a number
2. Delete a number
Enter your choice:
1

Page No: 78
Location:
7
Number:
150

ID: 23CSC25
Invalid location
Array elements: 12 15 16 14 10
1. Insert a number
2. Delete a number
3. Exit
Enter your choice:
3

Test Case - 2

2023-27-CSE-C
User Output
Enter size:
1
Enter 1 elements:

BNM Institute Of Technology


1
Array elements: 1
1. Insert a number
2. Delete a number
3. Exit
Enter your choice:
2
Location:
0
1 deleted from location 0
Array elements:
1. Insert a number
2. Delete a number
3. Exit
Enter your choice:
3
Exp. Name: Write a C program to Copy
Date: 2023-12-

Page No: 79
S.No: 32 one string into another string without
29
using strcpy()

Aim:

ID: 23CSC25
Write and execute a C program that implements string copy operation
stringcopy(str1, str2) that copies a string str1 to another string str2 without using
library function.

At the time of execution, the program should print the message on the console as:

Enter the source string :

For example, if the user gives the input as:

Enter the source string : Narmada

2023-27-CSE-C
then the program should print the result as:

Destination string is : Narmada

Source Code:

BNM Institute Of Technology


Lab10.c

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

void main(){
char str1[100], str2[100];
int i;
printf("Enter the source string : ");
fgets(str1, sizeof(str1), stdin);

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


str2[i] = str1[i];
}
str2[i] = '\0';

printf("Destination string is : %s\n", str2);

}
Execution Results - All test cases have succeeded!

Page No: 80
Test Case - 1

User Output
Enter the source string :

ID: 23CSC25
CSE and ISE
Destination string is : CSE and ISE

Test Case - 2

User Output
Enter the source string :
Narmada
Destination string is : Narmada

2023-27-CSE-C
BNM Institute Of Technology
Exp. Name: Write the code to replace
Date: 2023-12-

Page No: 81
S.No: 33 first occurrence of character to another
25
character

Aim:

ID: 23CSC25
Write a C program to replace the first occurrence of a character with another character in
a string using loop.

Input Format:
4. The String character
5. The Character that needs to be replaced in the given string
6. The Character to be replaced in the string

Output Format:
7. The output is a single-line representing the resultant string.
Source Code:

2023-27-CSE-C
replace.c

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

BNM Institute Of Technology


void main(){
char str1[100], str2[100];
char a, b;
scanf("%s %c %c", str1, &a, &b);

int i;
int j=1;

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


if(str1[i]==a && j){
str2[i]=b;
j = 0;
}else{
str2[i] = str1[i];
}
}
printf("%s\n", str2);

}
Execution Results - All test cases have succeeded!

Page No: 82
Test Case - 1

User Output
lian

ID: 23CSC25
a
o
lion

Test Case - 2

User Output
Univarsity
a

2023-27-CSE-C
e
University

BNM Institute Of Technology


Exp. Name: User-defined string- Date: 2024-01-
S.No: 34

Page No: 83
handling functions. 01

Aim:
Write a C program that implements the following user-defined string-handling functions:

ID: 23CSC25
i. To find the length of the given string.
ii. To copy the contents of one string to another.
iii. To reverse the contents of a string.
iv. To compare two strings.
v. To concatenate two strings.
Source Code:

userDefined.c

2023-27-CSE-C
BNM Institute Of Technology
#include <stdio.h>

Page No: 84
#include <string.h>
// Function to find the length of a string
int stringLength(const char *str) {
int len, i;
for(i=0; str[i]!='\0'; i++){

ID: 23CSC25
len++;
}
return len;

}
void stringCopy(char *str1, char *str2){
strcpy(str1, str2);
}
// Function to reverse the contents of a string
void stringReverse(char *str) {
/*int len = stringLength(str);

2023-27-CSE-C
int start = 0, end = (len-1);
while(start<end){

char temp = str[start];


str[start] = str[end];

BNM Institute Of Technology


str[end] = temp;

start++; end--;
}

for(int i=0; i<len/2; i++){


char temp = str[i];
str[i] = str[len-i-1];
str[len-i-1] = temp;
}*/
int len = strlen(str);
int start = 0, end = len-1;
for(int i=0; i<len/2; i++){
char temp = str[start];
str[start] = str[end];
str[end] = temp;
start++; end--;
}

// Function to compare two strings


int stringCompare(const char *str1, const char *str2) {
/*

Page No: 85
int i;
while(str1[i]!='\0' && str2[i]!='\0'){
if(str1[i] < str2[i]){
return -1;
}else if(str1[i] > str2[i]){

ID: 23CSC25
return 1;
}
i++;
}

if(str1[i]=='\0' && str2[i]=='\0'){


return 0;
}else if(str1[i]=='\0'){
return -1;
}else{
return 1;

2023-27-CSE-C
}*/
return strcmp(str1, str2);
}
// Function to concatenate two strings
void stringConcatenate(char *dest, const char *src) {
/*int i = stringLength(dest);

BNM Institute Of Technology


int j = 0;

while(src[j]!='\0'){
dest[i] = src[j];
i++;
j++;
}
dest[i]='\0';*/
strcat(dest, src);
}

int main() {
char str1[100], str2[50], result[150];

printf("string1: ");
scanf("%s", str1);
printf("string2: ");
scanf("%s", str2);

int length = stringLength(str1);


printf("Length of str1: %d\n", length);

stringCopy(result, str1);
printf("Copy of str1: %s\n", result);

Page No: 86
stringReverse(str2);
printf("Reverse of str2: %s\n", str2);

int cmp = stringCompare(str1, str2);

ID: 23CSC25
if (cmp == 0) {
printf("str1 and str2 are equal\n");
} else if (cmp < 0) {
printf("str1 is less than str2\n");
} else {
printf("str1 is greater than str2\n");
}

stringCopy(result, str1);
stringReverse(str2);
stringConcatenate(result, str2);

2023-27-CSE-C
printf("Concatenation of str1 and str2: %s\n", result);

return 0;
}

BNM Institute Of Technology


Execution Results - All test cases have succeeded!
Test Case - 1

User Output
string1:
Code
string2:
Tantra
Length of str1: 4
Copy of str1: Code
Reverse of str2: artnaT
str1 is less than str2
Concatenation of str1 and str2: CodeTantra

Test Case - 2

User Output
string1:
Hello
string2:

Page No: 87
World
Length of str1: 5
Copy of str1: Hello
Reverse of str2: dlroW

ID: 23CSC25
str1 is less than str2
Concatenation of str1 and str2: HelloWorld

2023-27-CSE-C
BNM Institute Of Technology
Exp. Name: Write the code to count the
Date: 2023-12-

Page No: 88
S.No: 35 frequency of each character in a string
25
using a loop

Aim:

ID: 23CSC25
Write a C program to count the frequency of each character in a string.
Source Code:

frequency.c

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

void main(){
char str[100];
printf("Enter the string: ");

2023-27-CSE-C
scanf("%s", str);
int i;
int count[256] = {0};
char unique[256];
int uniquecount = 0;

BNM Institute Of Technology


for(i=0; str[i]!='\0'; i++){
if(count[(int)str[i]]==0){
unique[uniquecount++] = str[i];
}
count[(int)str[i]]++;
}

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


printf("%c = %d\n", unique[i], count[(int)unique[i]]);
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
Enter the string:
helloworld
h = 1
e = 1

Page No: 89
l = 3
o = 2
w = 1
r = 1

ID: 23CSC25
d = 1

Test Case - 2

User Output
Enter the string:
welcometocodetantra
w = 1
e = 3

2023-27-CSE-C
l = 1
c = 2
o = 3
m = 1
t = 3
d = 1

BNM Institute Of Technology


a = 2
n = 1
r = 1
Date: 2023-12-
S.No: 36 Exp. Name: Upper triangular matrix

Page No: 90
26

Aim:
Write a C program to find whether a given matrix isan Upper triangular matrix or not.

ID: 23CSC25
Example:
Enter no of rows, columns: 3 3
Enter elements of matrix:
123
026
006
Upper triangular matrix

Source Code:

2023-27-CSE-C
upperTriangleMatrix.c

BNM Institute Of Technology


#include<stdio.h>

Page No: 91
#include<stdlib.h>

void main(){
int a, b, i, j;
printf("Enter no of rows, columns: ");

ID: 23CSC25
scanf("%d%d", &a, &b);
int mat[a][b];

printf("Enter elements of matrix:");

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


for(j=0; j<b; j++){
scanf("%d", &mat[i][j]);
}
}

2023-27-CSE-C
int F=1;
for(i=a-1; i>=0; i--){
for(j=i-1; j>=0; j--){
if(mat[i][j]!=0){
F =0;
}

BNM Institute Of Technology


}
}

if(F){
printf("Upper triangular matrix\n");
}else{
printf("Not an upper triangular matrix\n");
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
Enter no of rows, columns:
33
Enter elements of matrix:
123
026
006

Page No: 92
Upper triangular matrix

Test Case - 2

ID: 23CSC25
User Output
Enter no of rows, columns:
22
Enter elements of matrix:
12 13
14 15
Not an upper triangular matrix

2023-27-CSE-C
BNM Institute Of Technology
Exp. Name: Search an Element in an Date: 2023-12-
S.No: 37

Page No: 93
Array 25

Aim:
Write a C program to search for an element in an array. Return the position of the user-

ID: 23CSC25
given element if it is found in the array otherwise display that it is not found.

Input Format:
The first line contains an integer n representing the size of the array
The second line contains n space separated integers representing the elements of the
array
The third line contains an integer s, representing the element to be searched

Output Format:
If the search element is found, print "{search_element} found at position {position}",
otherwise print "{search_element} not found"

2023-27-CSE-C
Source Code:

searchEle.c

BNM Institute Of Technology


#include<stdio.h>

Page No: 94
#include<stdlib.h>

int linear(int arr[], int search, int size);

void main(){

ID: 23CSC25
int a, i, n, L[100];
printf("Enter size: ");
scanf("%d", &n);
printf("Enter elements: ");
for(i=0; i<n; i++){
scanf("%d", &L[i]);
}
printf("Enter search key: ");
scanf("%d", &a);

if(linear(L, a, n)){

2023-27-CSE-C
printf("%d found at position %d\n", a, linear(L, a,
n));
}else{
printf("%d not found\n", a);
}

BNM Institute Of Technology


}

int linear(int arr[], int search, int size){


int i, F, f=0;
for(i=0; i<size; i++){
if(arr[i]==search){
f=1;
F=i+1;
}
}

if(f){
return F;
}else{
return 0;
}

}
Execution Results - All test cases have succeeded!

Page No: 95
Test Case - 1

User Output
Enter size:

ID: 23CSC25
6
Enter elements:
11 22 33 44 55 66
Enter search key:
33
33 found at position 3

Test Case - 2

2023-27-CSE-C
User Output
Enter size:
6
Enter elements:
11 22 33 44 55 66

BNM Institute Of Technology


Enter search key:
75
75 not found
Exp. Name: Write a C program to find
Date: 2023-12-

Page No: 96
S.No: 38 Reverse of a String without changing
26
Special characters

Aim:

ID: 23CSC25
Write a C program to find the reverse of a string without changing the position of
special characters (reverse only the alphabets).

At the time of execution, the program should print the message on the console as:

Enter a string :

For example, if the user gives the input as:

Enter a string : a$b,c&ef@

2023-27-CSE-C
then the program should print the result as:

The given string : a$b,c&ef@


The output string : f$e,c&ba@

Source Code:

BNM Institute Of Technology


StringRevOnlyAlpha.c
#include<stdio.h>

Page No: 97
#include<stdlib.h>
#include<string.h>

int isAlpha(char ch);


void reverse(char S[]);

ID: 23CSC25
void main(){
char str[100];
printf("Enter a string : ");
scanf("%s", str);
printf("The given string : %s\n", str);

reverse(str);

printf("The output string : %s\n", str);


}

2023-27-CSE-C
int isAlpha(char ch){
return ((ch>='A' && ch<='Z')||(ch>='a' && ch<='z'));
}

void reverse(char S[]){

BNM Institute Of Technology


int start = 0;
int end = strlen(S)-1;

while(start<end){
while(!isAlpha(S[start]) && start<end){
start++;
}
while(!isAlpha(S[end]) && start<end){
end--;
}

if(start<end){
char temp = S[start];
S[start] = S[end];
S[end] = temp;

start++;
end--;
}
}

}
Execution Results - All test cases have succeeded!

Page No: 98
Test Case - 1

User Output
Enter a string :

ID: 23CSC25
a@#$gh^gd*
The given string : a@#$gh^gd*
The output string : d@#$gh^ga*

Test Case - 2

User Output
Enter a string :
a$bcd./fg|

2023-27-CSE-C
The given string : a$bcd./fg|
The output string : g$fdc./ba|

BNM Institute Of Technology


Date: 2023-12-
S.No: 39 Exp. Name: Scalar matrix

Page No: 99
25

Aim:
Write a C program to find whether a given matrix isa Scalar matrix or not.

ID: 23CSC25
Example:
Enter no of rows, columns: 3 3
Enter the elements:
100
010
001
Scalar matrix
Source Code:

scalarMatrix.c

2023-27-CSE-C
BNM Institute Of Technology
#include<stdio.h>

Page No: 100


#include<stdlib.h>

void main(){
int a, b;
printf("Enter no of rows, columns: ");
scanf("%d%d", &a, &b);

ID: 23CSC25
int mat[a][b];
printf("Enter the elements:");
int i, j;
for(i=0; i<a; i++){
for(j=0; j<b; j++){
scanf("%d", &mat[i][j]);
}
}

int F=1;

2023-27-CSE-C
for(i=0; i<a; i++){
for(j=0; j<b; j++){
if(i==j){
if(mat[i][j]!=mat[0][0]){
F=0;
break;

BNM Institute Of Technology


}
}else{
if(mat[i][j]!=0){
F=0;
break;
}
}
}
}

if(F){
printf("Scalar matrix\n");
}else{
printf("Not a scalar matrix\n");
}

Execution Results - All test cases have succeeded!


Test Case - 1

Page No: 101


User Output
Enter no of rows, columns:
33
Enter the elements:

ID: 23CSC25
100
101
110
Not a scalar matrix

Test Case - 2

User Output

2023-27-CSE-C
Enter no of rows, columns:
33
Enter the elements:
100
010
001

BNM Institute Of Technology


Scalar matrix
Date: 2023-12-
S.No: 40 Exp. Name: Lower triangular matrix

Page No: 102


25

Aim:
Write a C program to find whether a given matrix is Lower triangular matrix or not.

ID: 23CSC25
Example:
Enter no of rows, columns: 3 3
Enter elements of matrix:
200
120
412
Lower triangular matrix

Source Code:

2023-27-CSE-C
lowertriangularMatrix.c

BNM Institute Of Technology


#include<stdio.h>

Page No: 103


#include<stdlib.h>

void main(){
int a,b;
printf("Enter no of rows, columns: ");
scanf("%d%d", &a, &b);

ID: 23CSC25
int mat[a][b];
printf("Enter elements of matrix:");
int i, j;
for(i=0; i<a; i++){
for(j=0; j<a; j++){
scanf("%d", &mat[i][j]);
}
}

int F = 1;

2023-27-CSE-C
for(i=0; i<a; i++){
for(j=i+1; j<a; j++){
if(mat[i][j]!=0){
F = 0;
break;
}

BNM Institute Of Technology


}
}

if(F){
printf("Lower triangular matrix\n");
}else{
printf("Not a lower triangular matrix\n");
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
Enter no of rows, columns:
33
Enter elements of matrix:
200
120
412

Page No: 104


Lower triangular matrix

Test Case - 2

ID: 23CSC25
User Output
Enter no of rows, columns:
33
Enter elements of matrix:
002
450
250
Not a lower triangular matrix

2023-27-CSE-C
BNM Institute Of Technology
Exp. Name: Demonstrate pointer to Date: 2023-12-
S.No: 41

Page No: 105


array and pointers to functions 30

Aim:
Write a C program to demonstrate the following
i. Pointer to Array:

ID: 23CSC25
• Declare an integer variable n and a pointer ptr3 to an integer array. Allow the user
to input the size of the array and then input n integers to store in the array. Use
the pointer ptr3 to access and print the elements of the array.
ii. Pointers to Functions:
• Declare a pointer to a function named operation that can point to functions taking
two integers and returning an integer. Create two functions, add and subtract, that
perform addition and subtraction, respectively. Allow the user to input two
integers, and then use the operation pointer to call either the add or subtract
function based on user choice. Finally, display the result of the operation.
Source Code:

2023-27-CSE-C
pointerToArray.c

BNM Institute Of Technology


#include <stdio.h>

Page No: 106


int add(int a, int b);
int subtract(int a, int b);
// Function to add two numbers

int add(int a, int b){


return a+b;

ID: 23CSC25
}

// Function to subtract two numbers


int subtract(int a, int b){
return a-b;
}

int main() {
int n;
int(*operation)(int, int);

2023-27-CSE-C
// iii. Pointer to Array
printf("size: ");
scanf("%d", &n);
// Declare an array of the user-defined size
int ptr3[n];

BNM Institute Of Technology


// Take array inputs
printf("Elements: ");
for(int i=0; i<n; i++){
scanf("%d", &ptr3[i]);
}

int result, numA, numB;


printf("Accessing array elements using pointer:");
for (int i = 0; i < n; i++) {
printf("%d ", *(ptr3+i));
}
/*
printf("%d ", (*ptr3) );
}
*/
printf("\nEnter two integers: ");
scanf("%d%d", &numA, &numB);
// iv. Pointers to Functions

// Declare a pointer to a function

operation = add; // Assign the function 'add' to the pointer


result = operation(numA, numB);
printf("Using pointer to add: %d\n", result);

Page No: 107


operation = subtract; // Assign the function 'subtract' to the
pointer
result = operation(numA, numB);
printf("Using pointer to subtract: %d\n", result);

ID: 23CSC25
return 0;
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output

2023-27-CSE-C
size:
5
Elements:
12345
Accessing array elements using pointer:1 2 3 4 5

BNM Institute Of Technology


Enter two integers:
16 9
Using pointer to add: 25
Using pointer to subtract: 7

Test Case - 2

User Output
size:
7
Elements:
1 6 9 87 45 20 25
Accessing array elements using pointer:1 6 9 87 45 20 25
Enter two integers:
26 98
Using pointer to add: 124
Using pointer to subtract: -72
Exp. Name: Write a C program to Sort Date: 2023-12-
S.No: 42

Page No: 108


the array elements using Pointers 30

Aim:
Write a program to sort the elements of the given array in ascending order using

ID: 23CSC25
pointers.

Note: Write the functions read1(), display() and bubbleSort() in Program1005a.c .


Source Code:

Program1005.c
#include <stdio.h>
#include "Program1005a.c"
#define MAX_SIZE 100

2023-27-CSE-C
int main() {
int arr[MAX_SIZE], n;
printf("Enter value of n : ");
scanf("%d", &n);
if (n > MAX_SIZE || n <= 0) {
printf("Invalid value of n\n");

BNM Institute Of Technology


return 1; // Exit with an error code
}
read1(arr, n);
printf("Before sorting the elements are : ");
display(arr, n);
bubbleSort(arr, n);
printf("After sorting the elements are : ");
display(arr, n);
return 0;
}

Program1005a.c
#include<stdio.h>

Page No: 109


#include<stdlib.h>

void read1(int *arr, int n);


void display(int *arr, int n);
void swap(int *a, int *b);
void bubbleSort(int *arr, int n);

ID: 23CSC25
void read1(int *arr, int n){
printf("Enter %d elements : ", n);
for(int i=0; i<n; i++){
scanf("%d", (arr+i));
}
}

void display(int *arr, int n){


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

2023-27-CSE-C
printf("%d ", *(arr+i));
}
printf("\n");
}

void bubbleSort(int *arr, int n){

BNM Institute Of Technology


for(int i=0; i<n-1; i++){
for(int j=0; j<n-i-1; j++){
if(arr[j]>arr[j+1]){
swap(&arr[j], &arr[j+1]);
}
}
}
}

void swap(int *a, int *b){


int temp = *a;
*a = *b;
*b = temp;
}
Execution Results - All test cases have succeeded!

Page No: 110


Test Case - 1

User Output
Enter value of n :
4

ID: 23CSC25
Enter 4 elements :
9846
Before sorting the elements are : 9 8 4 6
After sorting the elements are : 4 6 8 9

2023-27-CSE-C
BNM Institute Of Technology
Exp. Name: Find smaller of the two Date: 2023-12-
S.No: 43

Page No: 111


integers using DMA 29

Aim:
Write a program to find the smaller of the two integers using DMA.

ID: 23CSC25
During execution, the program should print the following message on the console as:

Enter two integer values :

For example, if the user gives the following as input:

Enter two integer values : 22 11

Then the program should print the result as follows:

2023-27-CSE-C
The smaller integer of the given two integers 22 and 11 is : 11

Note: Write the functions read1() and minimum() in Program1003a.c .


Source Code:

Program1003.c

BNM Institute Of Technology


#include <stdio.h>
#include <stdlib.h>
#include "Program1003a.c"
void main() {
int *p, *q;
p = (int *) malloc(sizeof(int));
q = (int *) malloc(sizeof(int));
read1(p, q);
printf("The smallest of two numbers %d and %d is : %d\n", *p,
*q, minimum(p, q));
}

Program1003a.c
#include<stdio.h>

Page No: 112


#include<stdlib.h>

void read1(int *p, int *q);


int minimum(int *p, int *q);

void read1(int *p, int *q){

ID: 23CSC25
printf("Enter two integer values : ");
scanf("%d%d", p, q);
}

int minimum(int *p, int *q){


if(*p<*q){
return *p;
}else{
return *q;
}

2023-27-CSE-C
}

Execution Results - All test cases have succeeded!

BNM Institute Of Technology


Test Case - 1

User Output
Enter two integer values :
12 13
The smallest of two numbers 12 and 13 is : 12

Test Case - 2

User Output
Enter two integer values :
9 99
The smallest of two numbers 9 and 99 is : 9
Exp. Name: Demonstrate Pointers to Date: 2023-12-
S.No: 44

Page No: 113


Pointers and Array of Pointers 29

Aim:
Write a C program to demonstrate the following
i. Pointers to Pointers

ID: 23CSC25
• Declare an integer variable x and two pointers ptr1 and ptr2. Use ptr1 and ptr2
to display the value of x with single and double indirection, and allow the user to
update the value of x through double indirection.
ii. Array of Pointers
• Declare an integer variable n and create an array of integer pointers named arr.
Allow the user to input the size of the array and then input n integers to store in
the array using dynamic memory allocation. Finally, print the elements of the array.
Source Code:

pointersArray.c

2023-27-CSE-C
BNM Institute Of Technology
#include <stdio.h>

Page No: 114


#include <stdlib.h>
int main() {
int x;
int updatedValue;
// i. Pointers to Pointers
int *ptr1;

ID: 23CSC25
int **ptr2;
ptr1 = &x;
ptr2 = &ptr1;

printf("number: ");
scanf("%d", &x);
printf("Value of x: %d\n", x);
printf("Value pointed to by ptr1: %d\n",*ptr1);
printf("Value pointed to by ptr2 (using double indirection):
%d\n",**ptr2);

2023-27-CSE-C
// Changing the value of x through double indirection
printf("Modification value: ");
scanf("%d", &updatedValue);

**ptr2 = updatedValue;

BNM Institute Of Technology


printf("Updated value of x through double indirection: %d\n", x);

// ii. Array of Pointers


int n;
printf("size: ");
scanf("%d", &n);
int *arr[n]; // Array of integer pointers
int a[n];
int i;

printf("Elements: ");
for(i=0; i<n; i++){
scanf("%d", &a[i]);
arr[i] = &a[i];
}

printf("Array: ");

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


printf("%d ", *arr[i]);
}
return 0;

Page No: 115


}

Execution Results - All test cases have succeeded!

ID: 23CSC25
Test Case - 1

User Output
number:
8
Value of x: 8
Value pointed to by ptr1: 8
Value pointed to by ptr2 (using double indirection): 8
Modification value:

2023-27-CSE-C
16
Updated value of x through double indirection: 16
size:
5
Elements:

BNM Institute Of Technology


12345
Array: 1 2 3 4 5

Test Case - 2

User Output
number:
50
Value of x: 50
Value pointed to by ptr1: 50
Value pointed to by ptr2 (using double indirection): 50
Modification value:
45
Updated value of x through double indirection: 45
size:
3
Elements:
456
Array: 4 5 6
Exp. Name: Write a C Program to find
Date: 2023-12-

Page No: 116


S.No: 45 Sum, Mean and Standard Deviation
30
using Pointers

Aim:

ID: 23CSC25
Write a C program using pointers to compute sum, mean and standard deviation of all
elements sorted in an array of n real numbers.

At the time of execution, the program should print the message on the console as:

Enter n value :

For example, if the user gives the input as:

Enter n value : 4

2023-27-CSE-C
Now, the program should print the message on the console as:

Enter 4 elements :

For example, if the user gives the input as:

BNM Institute Of Technology


Enter 4 elements : 2.3 1.1 4.5 2.78

then the program should print the result as:

Sum = 10.680000
Mean = 2.670000
Standard deviation = 1.220942

Source Code:

Lab15.c
#include<stdio.h>

Page No: 117


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

void calc(float *arr, int n, float *sum, float *mean, float *sd);

void main(){

ID: 23CSC25
int n;
printf("Enter n value : ");
scanf("%d", &n);

float A[n];

printf("Enter %d elements : ", n);


for(int i=0; i<n; i++){
scanf("%f", &A[i]);
}

2023-27-CSE-C
float sum, mean, sd;
calc(A, n, &sum, &mean, &sd);
printf("Sum = %f\n", sum);
printf("Mean = %f\n", mean);
printf("Standard deviation = %f\n", sd);

BNM Institute Of Technology


}

void calc(float *arr, int n , float *sum, float *mean, float *sd){

*sum = 0.0;
for(int i=0; i<n; i++){
*sum += arr[i];
}

*mean = *sum/n;

*sd = 0.0;
for(int i=0; i<n; i++){
*sd += (pow((arr[i]-*mean),2));
}
*sd = sqrt((*sd)/(n));

}
Execution Results - All test cases have succeeded!

Page No: 118


Test Case - 1

User Output
Enter n value :
4

ID: 23CSC25
Enter 4 elements :
2.3 1.1 4.5 2.78
Sum = 10.680000
Mean = 2.670000
Standard deviation = 1.220942

Test Case - 2

2023-27-CSE-C
User Output
Enter n value :
6
Enter 6 elements :
2 3 4.5 6.43 2.45 1.678

BNM Institute Of Technology


Sum = 20.058001
Mean = 3.343000
Standard deviation = 1.651221
Exp. Name: Write a C program to Copy Date: 2023-12-
S.No: 46

Page No: 119


one String into another using Pointers 29

Aim:
Write a C program to copy one string into another using pointers.

ID: 23CSC25
Sample Input and Output:
Enter source string : Robotic Tool
Target string : Robotic Tool
Source Code:

CopyStringPointers.c

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

2023-27-CSE-C
char source[100], target[100];
printf("Enter source string : ");
fgets(source, sizeof(source), stdin);
copyString(target, source);
printf("Target string : %s\n", target);
}

BNM Institute Of Technology


CopyStringPointers1.c

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

void copyString(char *target, char *source);

void copyString(char *target, char *source){

int i;
for(i=0; source[i]!='\0'; i++){
target[i] = source[i];
}
target[i+1] = '\0';

Execution Results - All test cases have succeeded!


Test Case - 1

Page No: 120


User Output
Enter source string :
CodeTantra
Target string : CodeTantra

ID: 23CSC25
Test Case - 2

User Output
Enter source string :
Robotic Tool
Target string : Robotic Tool

2023-27-CSE-C
BNM Institute Of Technology
Exp. Name: Demonstrate the usage of Date: 2023-12-
S.No: 47

Page No: 121


pointers. 29

Aim:
Write a C program to demonstrate the usage of pointers.
8. Declare an integer variable "number" and an integer pointer "pointer". Allow the

ID: 23CSC25
user to input an integer, store it in "number", and then use the pointer "pointer"
to access and modify the value of "number".
9. Declare an integer variable "size", and an integer array "numbers" with a size
specified by the user. Use a pointer "arrPointer" to input integers into the array.
After input, use the same pointer to access and print the elements of the array.

Source Code:

usageOfPointers.c

2023-27-CSE-C
BNM Institute Of Technology
#include <stdio.h>

Page No: 122


int main() {
int number;
int *pointer; // Declared an integer pointer
int modificationVal;
printf("Enter number: ");
scanf("%d", &number);

ID: 23CSC25
pointer=&number;
printf("number: %d\n",*pointer);
printf("Enter modification value: ");
scanf("%d",&number);
printf("number: %d\n",*pointer);

int size;
printf("size: ");
scanf("%d", &size);

2023-27-CSE-C
int numbers[size];
int *arrPointer = numbers;
printf("Elements: ");

for(int i=0;i<size;i++){
scanf("%d",arrPointer+i);

BNM Institute Of Technology


}

printf("Array elements accessed using a pointer: ");


arrPointer = numbers;

for(int i=0;i<size;i++){
printf("%d ", *(arrPointer+i));
}

return 0;
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
Enter number:
16
number: 16
Enter modification value:
9

Page No: 123


number: 9
size:
5
Elements:

ID: 23CSC25
12345
Array elements accessed using a pointer: 1 2 3 4 5

Test Case - 2

User Output
Enter number:
98
number: 98

2023-27-CSE-C
Enter modification value:
10
number: 10
size:
3

BNM Institute Of Technology


Elements:
123
Array elements accessed using a pointer: 1 2 3
Exp. Name: Write a C program to Swap
Date: 2023-12-

Page No: 124


S.No: 48 two values by using Call-by-Address
30
method

Aim:

ID: 23CSC25
Write a program to swap two values by using call by address method.

At the time of execution, the program should print the message on the console as:

Enter two integer values :

For example, if the user gives the input as:

Enter two integer values : 12 13

then the program should print the result as:

2023-27-CSE-C
Before swapping in main : a = 12 b = 13
After swapping in swap : *p = 13 *q = 12
After swapping in main : a = 13 b = 12

Note: Write the function swap() in Program1002a.c and do use the printf() function

BNM Institute Of Technology


with a newline character ( \n ).
Source Code:

Program1002.c

#include <stdio.h>
#include "Program1002a.c"
void main() {
int a, b;
printf("Enter two integer values : ");
scanf("%d %d", &a, &b);
printf("Before swapping in main : a = %d b = %d\n", a, b);
swap(&a, &b);
printf("After swapping in main : a = %d b = %d\n", a, b);
}

Program1002a.c
#include<stdio.h>

Page No: 125


void swap(int *a, int *b);

void swap(int *a, int *b){


int temp = *a;
*a = *b;

ID: 23CSC25
*b = temp;

printf("After swapping in swap : *p = %d *q = %d\n", *a, *b);


}

Execution Results - All test cases have succeeded!


Test Case - 1

2023-27-CSE-C
User Output
Enter two integer values :
121 131
Before swapping in main : a = 121 b = 131

BNM Institute Of Technology


After swapping in swap : *p = 131 *q = 121
After swapping in main : a = 131 b = 121

Test Case - 2

User Output
Enter two integer values :
555 999
Before swapping in main : a = 555 b = 999
After swapping in swap : *p = 999 *q = 555
After swapping in main : a = 999 b = 555

Test Case - 3

User Output
Enter two integer values :
1001 101
Before swapping in main : a = 1001 b = 101
After swapping in swap : *p = 101 *q = 1001
Test Case - 4

Page No: 126


User Output
Enter two integer values :
9999 2999
Before swapping in main : a = 9999 b = 2999

ID: 23CSC25
After swapping in swap : *p = 2999 *q = 9999
After swapping in main : a = 2999 b = 9999

Test Case - 5

User Output
Enter two integer values :
10101 11010
Before swapping in main : a = 10101 b = 11010

2023-27-CSE-C
After swapping in swap : *p = 11010 *q = 10101
After swapping in main : a = 11010 b = 10101

BNM Institute Of Technology


Exp. Name: Arithmetic operations on Date: 2023-12-
S.No: 49

Page No: 127


two numbers using pointers. 30

Aim:
Write a C program to perform arithmetic operations on two numbers using pointers.

ID: 23CSC25
Note: Write the functions in "operations1.c".
Source Code:

operations.c

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

#include "operations1.c"

2023-27-CSE-C
void main() {
int *ptr1, *ptr2;
ptr1 = allocateMemoryToPointer(ptr1);
ptr2 = allocateMemoryToPointer(ptr2);
printf("Enter two integer values : ");
readP(ptr1, ptr2);

BNM Institute Of Technology


printf("Addition of %d and %d: %d\n", *ptr1, *ptr2, add(ptr1,
ptr2));
printf("Difference of %d and %d: %d\n", *ptr1, *ptr2,
diff(ptr1, ptr2));
printf("Multiplication of %d and %d: %d\n", *ptr1, *ptr2,
mul(ptr1, ptr2));
}

operations1.c
#include<stdio.h>

Page No: 128


#include<stdlib.h>

int* allocateMemoryToPointer(int *ptr);


void readP(int *p, int *q);
int add(int *p, int *q);

ID: 23CSC25
int diff(int *p, int *q);
int mul(int *p, int *q);

int* allocateMemoryToPointer(int *ptr){


ptr = (int *)malloc(sizeof(int));

return ptr;
}

void readP(int *p, int *q){

2023-27-CSE-C
scanf("%d %d", p, q);
}

int add(int *p, int *q){


return *p+*q;
}

BNM Institute Of Technology


int diff(int *p, int *q){
return *p-*q;
}

int mul(int *p, int *q){


return *p**q;
}

Execution Results - All test cases have succeeded!


Test Case - 1

User Output
Enter two integer values :
35
Addition of 3 and 5: 8
Difference of 3 and 5: -2
Multiplication of 3 and 5: 15
Test Case - 2

Page No: 129


User Output
Enter two integer values :
100 -28
Addition of 100 and -28: 72

ID: 23CSC25
Difference of 100 and -28: 128
Multiplication of 100 and -28: -2800

2023-27-CSE-C
BNM Institute Of Technology
Exp. Name: Write a C program to Count
Date: 2023-12-

Page No: 130


S.No: 50 number of Lowercase, Uppercase, digits
30
and Other Characters using Pointers

Aim:

ID: 23CSC25
Write a C program to find number of lowercase , uppercase , digits and
other characters using pointers.

Sample Input and Output:

Enter a string : Indo Pak 125 143 *.$


Number of uppercase letters = 2
Number of lowercase letters = 5
Number of digits = 6
Number of other characters = 7

2023-27-CSE-C
Source Code:

CountCharDigitOthers.c

#include <stdio.h>
#include "CountCharDigitOthers1.c"

BNM Institute Of Technology


void main() {
char str[80];
int upperCount = 0, lowerCount = 0, digitCount = 0, otherCount
= 0;
printf("Enter a string : ");
gets(str);
countCharDigitOthers(str, &upperCount, &lowerCount,
&digitCount, &otherCount);
printf("Number of uppercase letters = %d\n", upperCount);
printf("Number of lowercase letters = %d\n", lowerCount);
printf("Number of digits = %d\n", digitCount);
printf("Number of other characters = %d\n", otherCount);
}

CountCharDigitOthers1.c
#include<stdio.h>

Page No: 131


#include<stdlib.h>

void countCharDigitOthers(char *str, int *upperCount, int *lowerCount,


int *digitCount, int *otherCount);

void countCharDigitOthers(char *str, int *upperCount, int *lowerCount,

ID: 23CSC25
int *digitCount, int *otherCount){

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


if(str[i]>='A'&&str[i]<='Z'){
(*upperCount)+=1;
}else if(str[i]>='a'&&str[i]<='z'){
(*lowerCount)+=1;
}else if(str[i]>='0'&&str[i]<='9'){
(*digitCount)+=1;
}else{

2023-27-CSE-C
(*otherCount)+=1;
}
}

BNM Institute Of Technology


Execution Results - All test cases have succeeded!
Test Case - 1

User Output
Enter a string :
CodeTantra123&*@987.
Number of uppercase letters = 2
Number of lowercase letters = 8
Number of digits = 6
Number of other characters = 4

Test Case - 2

User Output
Enter a string :
Indo Pak 125 143 *.$
Number of uppercase letters = 2
Number of lowercase letters = 5
Number of digits = 6

Page No: 132


Number of other characters = 7

Test Case - 3

ID: 23CSC25
User Output
Enter a string :
12345
Number of uppercase letters = 0
Number of lowercase letters = 0
Number of digits = 5
Number of other characters = 0

2023-27-CSE-C
BNM Institute Of Technology
BNM Institute Of Technology 2023-27-CSE-C ID: 23CSC25 Page No: 133

You might also like