CodeTantra C
CodeTantra C
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
arithmeticOperations.c
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;
User Output
9
8
17
0
1
1
1
2
72
500
998
2000
1002
1000
User Output
Test Case - 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;
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);
}
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
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
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{
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
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.
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(){
area = PI*radius*radius;
circumference = 2*PI*radius;
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
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);
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.
Degrees.c
#include<stdio.h>
2023-27-CSE-C
#include<stdlib.h>
void main(){
float c, f;
printf("C: ");
scanf("%f", &c);
User Output
C:
0
F= 32.000000
Test Case - 2
User Output
C:
32
F= 89.599998
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
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{
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:
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
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);
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
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);
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;
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;
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);
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{
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);
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>
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");
}
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);
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:
case 2:
printf("Exited\n");
break;
default:
printf("Invalid choice\n");
goto label;
}
return 0;
}
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
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:
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:
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:
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);
User Output
Enter the range:
19
1 3 5 7 9
Test Case - 2
User Output
Enter the range:
Invalid range
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:
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
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
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
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));
}
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.
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);
matricesAddition.c
#include<stdio.h>
Page No: 50
#include<stdlib.h>
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]);
}
}
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
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
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
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
Page No: 55
#include <stdlib.h>
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];
2023-27-CSE-C
readMatrix(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]);
}
}
}
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");
2023-27-CSE-C
printf("%d ", B[i][j]);
}
printf("\n");
}
}
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:
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>
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);
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
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.
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);
fibonacci1.c
#include<stdio.h>
#include<stdlib.h>
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 :
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));
}
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)));
}
}
2023-27-CSE-C
return 1;
}else{
return ((FindNcr(n-1, r-1))+(FindNcr1(n-1, r)));
}
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.
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);
matrixMul.c
#include<stdio.h>
Page No: 67
#include<stdlib.h>
ID: 23CSC25
int b, int c);
2023-27-CSE-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
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));
}
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.
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));
}
#include<stdio.h>
#include<stdlib.h>
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
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:
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");
}
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++){
Page No: 76
}
ID: 23CSC25
int main() {
printf("Enter size: ");
scanf("%d", &size);
displayArray();
2023-27-CSE-C
int choice, location, number;
displayArray();
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
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:
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:
2023-27-CSE-C
then the program should print the result as:
Source Code:
#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);
}
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>
int i;
int j=1;
}
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
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){
start++; end--;
}
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++;
}
2023-27-CSE-C
}*/
return strcmp(str1, str2);
}
// Function to concatenate two strings
void stringConcatenate(char *dest, const char *src) {
/*int i = stringLength(dest);
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);
stringCopy(result, str1);
printf("Copy of str1: %s\n", result);
Page No: 86
stringReverse(str2);
printf("Reverse of str2: %s\n", 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;
}
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;
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
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
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];
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;
}
if(F){
printf("Upper triangular matrix\n");
}else{
printf("Not an upper triangular matrix\n");
}
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
Page No: 94
#include<stdlib.h>
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);
}
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
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 :
2023-27-CSE-C
then the program should print the result as:
Source Code:
Page No: 97
#include<stdlib.h>
#include<string.h>
ID: 23CSC25
void main(){
char str[100];
printf("Enter a string : ");
scanf("%s", str);
printf("The given string : %s\n", str);
reverse(str);
2023-27-CSE-C
int isAlpha(char ch){
return ((ch>='A' && ch<='Z')||(ch>='a' && ch<='z'));
}
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|
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>
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;
if(F){
printf("Scalar matrix\n");
}else{
printf("Not a scalar matrix\n");
}
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
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
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;
}
if(F){
printf("Lower triangular matrix\n");
}else{
printf("Not a lower triangular matrix\n");
}
User Output
Enter no of rows, columns:
33
Enter elements of matrix:
200
120
412
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
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
ID: 23CSC25
}
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];
ID: 23CSC25
return 0;
}
User Output
2023-27-CSE-C
size:
5
Elements:
12345
Accessing array elements using pointer:1 2 3 4 5
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
Aim:
Write a program to sort the elements of the given array in ascending order using
ID: 23CSC25
pointers.
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");
Program1005a.c
#include<stdio.h>
ID: 23CSC25
void read1(int *arr, int n){
printf("Enter %d elements : ", n);
for(int i=0; i<n; i++){
scanf("%d", (arr+i));
}
}
2023-27-CSE-C
printf("%d ", *(arr+i));
}
printf("\n");
}
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
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:
2023-27-CSE-C
The smaller integer of the given two integers 22 and 11 is : 11
Program1003.c
Program1003a.c
#include<stdio.h>
ID: 23CSC25
printf("Enter two integer values : ");
scanf("%d%d", p, q);
}
2023-27-CSE-C
}
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
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>
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;
printf("Elements: ");
for(i=0; i<n; i++){
scanf("%d", &a[i]);
arr[i] = &a[i];
}
printf("Array: ");
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:
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-
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 :
Enter n value : 4
2023-27-CSE-C
Now, the program should print the message on the console as:
Enter 4 elements :
Sum = 10.680000
Mean = 2.670000
Standard deviation = 1.220942
Source Code:
Lab15.c
#include<stdio.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];
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);
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!
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
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);
}
#include<stdio.h>
#include<stdlib.h>
int i;
for(i=0; source[i]!='\0'; i++){
target[i] = source[i];
}
target[i+1] = '\0';
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
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>
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);
for(int i=0;i<size;i++){
printf("%d ", *(arrPointer+i));
}
return 0;
}
User Output
Enter number:
16
number: 16
Enter modification value:
9
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
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:
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
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>
ID: 23CSC25
*b = temp;
2023-27-CSE-C
User Output
Enter two integer values :
121 131
Before swapping in main : a = 121 b = 131
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
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
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);
operations1.c
#include<stdio.h>
ID: 23CSC25
int diff(int *p, int *q);
int mul(int *p, int *q);
return ptr;
}
2023-27-CSE-C
scanf("%d %d", p, q);
}
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
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-
Aim:
ID: 23CSC25
Write a C program to find number of lowercase , uppercase , digits and
other characters using pointers.
2023-27-CSE-C
Source Code:
CountCharDigitOthers.c
#include <stdio.h>
#include "CountCharDigitOthers1.c"
CountCharDigitOthers1.c
#include<stdio.h>
ID: 23CSC25
int *digitCount, int *otherCount){
2023-27-CSE-C
(*otherCount)+=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
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