0% found this document useful (0 votes)
17 views16 pages

ST Lab New

The document outlines multiple programming tasks including calculating the sum of individual digits, evaluating student marks, generating prime numbers, and sorting and merging array elements. Each task includes an aim, algorithm, coding examples in C, and test cases with expected and actual results. The tests cover various valid and invalid inputs to ensure the robustness of the programs.

Uploaded by

MANI KANDAN
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views16 pages

ST Lab New

The document outlines multiple programming tasks including calculating the sum of individual digits, evaluating student marks, generating prime numbers, and sorting and merging array elements. Each task includes an aim, algorithm, coding examples in C, and test cases with expected and actual results. The tests cover various valid and invalid inputs to ensure the robustness of the programs.

Uploaded by

MANI KANDAN
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

1.

SUM OF INDIVIDUAL DIGITS

AIM: CODING :
To create a program for calculating the sum of
digits. #include<stdio.h>
int main()
ALGORITHM : {
long long n;
Step 1: Start the process. int sum = 0, rem;
printf("ENTER A NUMBER: ");
Step 2: Declare variables n, sum, and rem. scanf("%lld", &n);
Step 3: Take input for the number n.
while (n > 9)
Step 4: Repeat the following steps while n is greater than 9: {
sum = 0;
 Set sum to 0. while (n > 0) {
 Find the remainder of n when divided by 10 and add rem = n % 10;
it to sum. sum = sum + rem;
 Reduce n by dividing it by 10.
n = n / 10;
Step 5: Set n to the sum obtained in the previous step. }
n = sum;
Step 6: Display the final single digit sum. }

Step 7: Stop the process. printf("SINGLE DIGIT SUM IS = %lld", n);


return 0;
}
TEST CASES :

TEST TEST TEST EXPECTED ACTUAL


S.NO TEST NAME RESULT
ID DESCRIPTION DATA VALUE VALUE

Check sum of digits


Valid Input (Single
1 TC01 for a single-digit 5 5 5 Pass
Digit)
number

Valid Input Check sum of digits


2 TC02 23 5 5 Pass
(Multiple Digits) for a two-digit number

Valid Input (Large Check sum of digits


3 TC03 12345 15 15 Pass
Number) for a large number
Invalid Input Check behavior with Error (invalid
4 TC04 "abc123" Error Pass
(Alphanumeric) alphanumeric input input)
Invalid Input Check behavior with Error (invalid
5 TC05 "!@#123" Error Pass
(Symbols) symbols as input input)
Invalid Input (Mix Check behavior with
Error (invalid
6 TC06 of Symbols & symbols and digits in "$123abc" Error Pass
input)
Digits) the input
Invalid Input Check behavior with Error (invalid
7 TC07 "" Error Pass
(Empty) empty input input)

Invalid Input Check behavior with Error (invalid


8 TC08 -123 Error Pass
(Negative Number) negative number input)

Check sum of digits


9 TC09 Valid Input (Zero) 0 0 0 Pass
for input 0
Invalid Input Check behavior with Error (invalid
10 TC10 "" Error Pass
(Spaces) spaces as input input)
2. STUDENT MARK STATEMENT
AIM:
Test the C Program: Accept the inputs student CODING :
name, marks in five subjects and declare the result as #include <stdio.h>
PASS if the student gets minimum 40 in each subject ; #include <string.h>
other wise declare the result as FAIL.
int main() {
int regno, m1, m2, m3, m4, m5;
ALGORITHM:
char name[50], result[10];
Step 1: Start the process. float tot, avg;
Step 2: Input the register number and name.
Step 3: Input the marks of 5 subjects. printf("\n ENTER REGISTER NUMBER: ");
Step 4: Calculate the total marks as the sum of the 5 scanf("%d", &regno);
subject marks.
Step 5: Compute the average by dividing the total marks printf("\n ENTER NAME: ");
scanf("%s", &name);
by 5.
Step 6: Check if all subject marks are greater than or printf("\n ENTER THE 5 SUBJECT MARKS: "
equal to 40. If true, assign "Pass" to the result; otherwise, scanf("%d %d %d %d %d", &m1, &m2, &m3, &
assign "Fail". &m5);
Step 7: Display the register number, name, subject marks,
total, average, and result.
Step 8: Stop the process. tot = m1 + m2 + m3 + m4 + m5;
avg = tot / 5.0;
strcpy(result, (m1 >= 40 && m2 >= 40 && m3
&& m4 >= 40 && m5 >= 40) ? "Pass" : "Fail");

printf("\n REGISTER NUMBER: %d", regno);


printf("\n NAME: %s", name);
printf("\n SUBJECT-1 MARK: %d", m1);
printf("\n SUBJECT-2 MARK: %d", m2);
printf("\n SUBJECT-3 MARK: %d", m3);
printf("\n SUBJECT-4 MARK: %d", m4);
printf("\n SUBJECT-5 MARK: %d", m5);
printf("\n TOTAL: %.2f", tot);
printf("\n AVERAGE: %.2f", avg);
printf("\n RESULT: %s", result);

return 0;
}
TEST CASE :

S. TEST TEST TEST ACTUAL


TEST DATA EXPECTED VALUE RESULT
NO ID NAME DESCRIPTION VALUE
Check with valid marks Reg: 101, Name: Reg: 101, Name: John,
Valid Input As
1 TC01 where all subjects are John, Marks: 50, Total: 350.00, Average: SUCCESS
(All Pass) Expected
passed 60, 70, 80, 90 70.00, Result: Pass
Check with valid marks Reg: 102, Name: Reg: 102, Name: Jane,
Valid Input As
2 TC02 where one subject is Jane, Marks: 50, Total: 325.00, Average: SUCCESS
(One Fail) Expected
failed 35, 70, 80, 90 65.00, Result: Fail
Reg: 103, Name: Reg: 103, Name: Mike,
Borderline Check behavior at pass As
3 TC03 Mike, Marks: 40, Total: 200.00, Average: SUCCESS
Pass mark boundary Expected
40, 40, 40, 40 40.00, Result: Pass
Reg: 104, Name: Reg: 104, Name: Anna,
Borderline Check behavior at fail As
4 TC04 Anna, Marks: 40, Total: 259.00, Average: SUCCESS
Fail mark boundary Expected
39, 50, 60, 70 51.80, Result: Fail
Check behavior with Reg: 105, Name:
Missing
5 TC05 missing marks for one Lily, Marks: 50, , Error (Invalid input) Error SUCCESS
Marks
subject 70, 80, 90
Reg: ABCD,
Check behavior with a
Non-Numeric Name: Tom,
6 TC06 non-numeric register Error (Invalid input) Error SUCCESS
Register Marks: 50, 60, 70,
number
80, 90
Reg: 107, Name:
Check behavior with an
7 TC07 Empty Name (empty), Marks: Error (Invalid input) Error SUCCESS
empty name
50, 60, 70, 80, 90
Reg: 108, Name: Reg: 108, Name: Sam,
Valid Input Check with zero marks As
8 TC08 Sam, Marks: 0, 0, Total: 0.00, Average: SUCCESS
(Zero Marks) for all subjects Expected
0, 0, 0 0.00, Result: Fail
Reg: 999999999, Name:
Check behavior with Reg: 999999999,
Zoe, Total: 350.00, As
9 TC09 Large Input very large register Name: Zoe, Marks: SUCCESS
Average: 70.00, Result: Expected
number 50, 60, 70, 80, 90
Pass
Reg: 110, Name:
Invalid Input Check behavior with
10 TC10 Max, Marks: A, B, Error (Invalid input) Error SUCCESS
(Alphabets) alphabetical marks
C, D, E
3. PRIME NUMBERS

AIM :
CODING:
Program for generating n prime numbers #include <stdio.h>
int main() {
ALGORITHM : int n, num = 2, count = 0;
printf("ENTER THE NUMBER OF PRIMES TO
Step 1: Start the process. GENERATE :");
scanf("%d", &n);
Step 2: Initialize n, num = 2, and count = 0. printf("GENERATING %d PRIME NUMBERS:\n", n);
while (count < n)
Step 3: Get input for the number of primes to generate (n).
{
Step 4: Loop while count is less than n and check if num int prime = 1;
is divisible by any number from 2 to √num. for (int i = 2; i * i <= num; i++)
if (num % i == 0)
Step 5: If num is not divisible (prime), print num and {
increment count. prime = 0;
break;
Step 6: Increment num to check the next number.
}
Step 7: Stop the process. if (prime)
{
printf("%d ", num);
count++;
}
num++;
}
return 0;
}

TEST CASE :
TEST TEST EXPECTED ACTUAL
S.NO TEST NAME TEST DESCRIPTION RESULT
ID DATA VALUE VALUE
Check behavior for
Valid Input (Single
1 TC01 generating 5 prime 5 2 3 5 7 11 2 3 5 7 11 SUCCESS
Digit)
numbers.
Check behavior for
Valid Input (Double 2 3 5 7 11 13 17 2 3 5 7 11 13
2 TC02 generating 10 prime 10 SUCCESS
Digits) 19 23 29 17 19 23 29
numbers.
Valid Input (Large Check behavior for
3 TC03 1 2 2 SUCCESS
Number) generating 1 prime number.
Invalid Input Check behavior when input Error (invalid
4 TC04 abc123 Error SUCCESS
(Alphanumeric) is alphanumeric. input)
Invalid Input Check behavior when input Error (invalid
5 TC05 !@#123 Error SUCCESS
(Symbols) contains symbols. input)
Invalid Input (Mix of Check behavior with a mix Error (invalid
6 TC06 $123abc Error SUCCESS
Symbols) of symbols and numbers. input)
Invalid Input (Empty Check behavior when no Error (invalid
7 TC07 (empty) Error SUCCESS
Input) input is provided. input)
8 TC08 Valid Input (Zero) Check behavior for input 0. 0 No output No output SUCCESS
Invalid Input Check behavior when input Error (invalid
9 TC09 -5 Error SUCCESS
(Negative Number) is a negative number. input)
Invalid Input Check behavior when input Error (invalid
10 TC10 (spaces) Error SUCCESS
(Spaces Only) contains only spaces. input)

4. SORT AND MERGE ARRAY ELEMENTS


CODING:

AIM : #include <stdio.h>


int main()
Sort and store the elements of two arrays of integers into the third {
list. int a[3], b[3], c[6];
printf("Enter 3 values for the first array:\n");
ALGORITHM : for (int i = 0; i < 3; i++)
{
Step 1: Start the process. scanf("%d", &a[i]);
Step 2: Declare three integer arrays: a[3], b[3], and c[6]. }
Step 3: Get input values for array a[3] from the user. printf("Enter 3 values for the second array:\
Step 4: Get input values for array b[3] from the user.
Step 5: Merge the values of arrays a[3] and b[3] into array c[6]. n");
Step 6: Perform sorting on array c[6] using a nested loop to compare for (int i = 0; i < 3; i++)
and swap values. {
Step 7: Display the sorted merged array c[6]. scanf("%d", &b[i]);
Step 8: Test the program with different input values. }
Step 9: Stop the process. for (int i = 0; i < 3; i++) c[i] = a[i];
for (int i = 0; i < 3; i++) c[3 + i] = b[i];

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


for (int j = i + 1; j < 6; j++) {
if (c[i] > c[j]) {
int temp = c[i];
c[i] = c[j];
c[j] = temp;
}
}
}
printf("Sorted merged array: ");
for (int i = 0; i < 6; i++)
{
printf("%d ", c[i]);
}
return 0;
}
TEST CASE :
TEST TEST EXPECTED ACTUAL
S.NO TEST NAME TEST DESCRIPTION RESULT
ID DATA VALUE VALUE
Check behavior with Sorted merged
Valid Input (Positive Sorted merged
1 TC01 positive integers as 1 2 3, 4 5 6 array: 1 2 3 4 5 SUCCESS
Integers) array: 1 2 3 4 5 6
input 6
Check behavior with Sorted merged
Valid Input (Same Sorted merged
2 TC02 identical values in both 5 5 5, 5 5 5 array: 5 5 5 5 5 SUCCESS
Values) array: 5 5 5 5 5 5
arrays 5
Sorted merged Sorted merged
Valid Input Check behavior with 1 3 5, 10
3 TC03 array: 1 3 5 10 15 array: 1 3 5 10 SUCCESS
(Different Ranges) input of different ranges 15 20
20 15 20
Invalid Input Check behavior with a1 b2 c3, Error (invalid Error (invalid
4 TC04 SUCCESS
(Alphanumeric) alphanumeric input d4 e5 f6 input) input)
Invalid Input Check behavior with @ # $, *, Error (invalid Error (invalid
5 TC05 SUCCESS
(Symbols) symbols as input &, % input) input)
Check behavior with
Invalid Input (Mixed 123$abc, Error (invalid Error (invalid
6 TC06 mixed symbols and SUCCESS
Symbols & Digits) 789!xyz input) input)
digits
Invalid Input Check behavior with Error (invalid Error (invalid
7 TC07 (empty) SUCCESS
(Empty) empty input input) input)
Sorted merged
Check behavior when 0 Sorted merged
8 TC08 Valid Input (Zero) 0 0 0, 0 0 0 array: 0 0 0 0 0 SUCCESS
is entered array: 0 0 0 0 0 0
0
Sorted merged Sorted merged
Valid Input Check behavior with -1 -2 -3, -4
9 TC09 array: -6 -5 -4 -3 - array: -6 -5 -4 - SUCCESS
(Negative Numbers) negative numbers -5 -6
2 -1 3 -2 -1

Invalid Input Check behavior with Error (invalid Error (invalid


10 TC10 (spaces) SUCCESS
(Spaces) spaces as input input) input)

5. STACK OPERTION
AIM :
Experiment the operations of Stack using array implementation.

ALGORITHM :

Step 1: Start the process.


Step 2: Declare a constant MAX for the maximum stack size and variables:
An array stack of size MAX.
An integer top initialized to -1.
Step 3: Define the operations:
Push: Add a value to the stack if it’s not full; otherwise, display "Stack Overflow".
Pop: Remove the top value from the stack if it’s not empty; otherwise, display "Stack
Underflow".
Peek: Display the top value if the stack is not empty; otherwise, display "Stack is Empty".
Display: Show all stack elements if not empty; otherwise, display "Stack is Empty".
Step 4: Display a menu with options:
1. Push 2. Pop 3. Peek 4. Display 5. Exit
Step 5: Take user input for the choice and perform the corresponding operation using a loop:
For Push, input a value and call the push function.
For Pop, call the pop function.
For Peek, call the peek function.
For Display, call the display function.
For Exit, terminate the program.
Step 6: Handle invalid choices by displaying "Invalid choice. Try again."
Step 7: Stop the process.

CODING : break;
// Display stack elements case 2:
#include <stdio.h> void display() { pop();
#define MAX 100 if (top >= 0) { break;
for (int i = 0; i <= top; i++) { case 3:
int stack[MAX], top = -1; printf("%d ", stack[i]); peek();
} break;
// Push operation printf("\n"); case 4:
void push(int v) { } else { display();
if (top < MAX - 1) { printf("STACK IS EMPTY\ break;
stack[++top] = v; n"); default:
printf("%d PUSHED\n", v); } printf("INVALID
} else { } CHOICE. TRY AGAIN..\n");
printf("STACK OVERFLOW\n"); }
} int main() { }
} int choice, value; return 0;
while (1) { }
// Pop operation // Menu
void pop() { printf("\n1.PUSH \n2.POP \
if (top >= 0) { n3.PEEK \n4.DISPLAY \n5.EXIT \
printf("%d POPPED\n", nENTER YOUR CHOICE: ");
stack[top--]); scanf("%d", &choice);
} else {
printf("STACK UNDERFLOW\ if (choice == 5) {
n"); break;
} }
}
switch (choice) {
// Peek operation case 1:
void peek() { printf("ENTER VALUE
if (top >= 0) { TO PUSH: ");
printf("Top: %d\n", stack[top]); scanf("%d", &value);
} else { push(value);
printf("STACK IS EMPTY\n");
}
}

TEST CASE :
TEST EXPECTED ACTUAL
S.NO TEST ID TEST NAME TEST DATA RESULT
DESCRIPTION VALUE VALUE
Push a single-
Push Single "5
1 TC01 digit value to the 5 "5 PUSHED" SUCCESS
Value PUSHED"
stack
Push multiple "10 PUSHED, 20
Push Multiple Matches
2 TC02 values 10, 20, 30 PUSHED, 30 SUCCESS
Values Expected
sequentially PUSHED"
Pop the top value Last pushed value Matches
3 TC03 Pop a Value Pop operation SUCCESS
from the stack (30) is popped Expected
Check the top
Matches
4 TC04 Peek Top Value value of the stack Peek Top value (20) SUCCESS
Expected
without popping
Display all
Display Stack Matches
5 TC05 elements in the Display "10 20" SUCCESS
Contents Expected
stack
Push until the
Overflow "STACK Matches
6 TC06 stack exceeds its 101 values SUCCESS
Condition OVERFLOW" Expected
capacity
Underflow Pop from an "STACK Matches
7 TC07 Pop operation SUCCESS
Condition empty stack UNDERFLOW" Expected
Peek on Empty Peek when the "STACK IS Matches
8 TC08 Peek SUCCESS
Stack stack is empty EMPTY" Expected
Push a zero value Matches
9 TC09 Push Zero 0 "0 PUSHED" SUCCESS
to the stack Expected
"STACK
Push Negative Push a negative OVERFLOW" Matches
10 TC10 -5 SUCCESS
Values value to the stack (Program Expected
constraint)

6. QUEUE OPERTION

AIM

Test the C program : Menu-driven option for queue operations like add,remove and display.
ALGORITHM

Step 1: Start the process.


Step 2: Initialize queue, front = -1, and rear = -1.
Step 3: Display menu with options to Enqueue, Dequeue, Display, or Exit.
Step 4:
 For Enqueue:
o If full, display "Queue is full."
o Else, increment rear, set front if -1, and add value to queue[rear].
Step 5:
 For Dequeue:
o If empty, display "Queue is empty."
o Else, display and remove value at queue[front], increment front, and reset if empty.
Step 6:
 For Display:
o If empty, display "Queue is empty."
o Else, display elements from front to rear.
Step 7: Exit if chosen, else repeat from Step 3.
Step 8: Stop the process.

printf("\n");
CODING : }
}
#include <stdio.h> int main() {
#define SIZE 5 int choice, value;

int queue[SIZE]; do {
int front = -1, rear = -1; printf("\nQueue Operations Menu:\n");
printf("1. Add (Enqueue)\n");
void enqueue(int value) { printf("2. Remove (Dequeue)\n");
if (rear == SIZE - 1) { printf("3. Display\n");
printf("Queue is full. Cannot add %d.\n", value); printf("4. Exit\n");
} else { printf("Enter your choice: ");
if (front == -1) front = 0; scanf("%d", &choice);
rear++;
queue[rear] = value; switch (choice) {
printf("%d added to the queue.\n", value); case 1:
} printf("Enter value to add: ");
} scanf("%d", &value);
void dequeue() { enqueue(value);
if (front == -1 || front > rear) { break;
printf("Queue is empty. Cannot remove.\n"); case 2:
} else { dequeue();
printf("%d removed from the queue.\n", break;
queue[front]); case 3:
front++; display();
if (front > rear) front = rear = -1; break;
} case 4:
} printf("Existing...\n");
void display() { break;
if (front == -1 || front > rear) { default:
printf("Queue is empty.\n"); printf("invalid choice! please try again.\n");
} else { break;
printf("Queue: "); }
for (int i = front; i <= rear; i++) { }
printf("%d ", queue[i]); while(choice !=4);
} return 0;
}
TEST CASE :
TEST EXPECTED ACTUAL
S.NO TEST NAME TEST DESCRIPTION TEST DATA RESULT
ID VALUE VALUE
Enqueue to Add a single digit to an 5 added to the
1 TC01 5 5 added SUCCESS
Empty Queue empty queue queue.
Attempt to add an
Enqueue to Full
2 TC02 element when the queue [1, 2, 3, 4, 5] Queue is full. Queue full SUCCESS
Queue
is full
Dequeue from
Remove the front [1, 2, 3], 1 removed from the
3 TC03 Non-Empty 1 removed SUCCESS
element from the queue dequeue queue.
Queue
Dequeue from Attempt to remove from Queue is empty.
4 TC04 Empty queue Empty error SUCCESS
Empty Queue an empty queue Cannot remove.
Display Non- Display elements in a
5 TC05 [10, 20, 30] Queue: 10 20 30 Queue: 10.. SUCCESS
Empty Queue non-empty queue
Display Empty Display elements in an Empty
6 TC06 Empty queue Queue is empty. SUCCESS
Queue empty queue queue
Enqueue Add multiple elements All values added to
7 TC07 5, 10, 15 All added SUCCESS
Multiple Values consecutively the queue.
Circular Use Add elements, dequeue [5, dequeue
8 TC08 8 added after reset. 8 added SUCCESS
After Reset all, and add again all, 8]
Add and Proper functioning
Enqueue and dequeue [1, 2,
9 TC09 Remove to Full of enqueue and Success SUCCESS
until the queue is reused dequeue, 3]
Cycle dequeue
Add: 1, 2;
Perform add, remove,
10 TC10 Mix Operations Remove: 1; Queue: 2 Queue: 2 SUCCESS
and display in sequence
Display
7. PALINDROME

AIM : CODING :

Test the C++ program : Palindrome string checking


program (using pointers) #include <iostream>
#include <cstring>
ALGORITHM : using namespace std;

Step 1: Start the process. bool isPalindrome(const char* str) {


const char *left = str, *right = str + strlen(str) - 1;
Step 2: Read a string from the user. while (left < right) {
if (*left != *right) return false;
Step 3: Set two pointers, one at the start and one at the
end of the string. left++, right--;
}
Step 4: Compare characters at the pointers; if unequal, return true;
return "not a palindrome." Move the pointers inward. }
int main() {
Step 5: If all characters match, return "is a palindrome." char str[100];
cin.getline(str, 100);
Step 6: Stop the process.
cout << (isPalindrome(str) ? "Palindrome\n" : "Not a
Palindrome\n");
return 0;
}

TEST CASE :
TEST TEST TEST EXPECTED ACTUAL
S.NO TEST NAME RESULT
ID DESCRIPTION DATA VALUE VALUE
Check if a single
Valid Palindrome The string is a The string is
1 TC01 character is a a SUCCESS
(Single Character) palindrome. a palindrome.
palindrome
Check if an odd-
Valid Palindrome (Odd The string is a The string is
2 TC02 length string is a racecar SUCCESS
Length) palindrome. a palindrome.
palindrome
Check if an even-
Valid Palindrome (Even The string is a The string is
3 TC03 length string is a abba SUCCESS
Length) palindrome. a palindrome.
palindrome
Check if a string The string is
Invalid Palindrome The string is not
4 TC04 with mixed cases Racecar not a SUCCESS
(Mixed Case) a palindrome.
fails as a palindrome palindrome.
Check if a string
Invalid Palindrome The string is
with non-matching The string is not
5 TC05 (Non-Matching hello not a SUCCESS
characters is not a a palindrome.
Characters) palindrome.
palindrome
The string is not
a man a
Check if a string a palindrome. The string is
Valid Palindrome plan a
6 TC06 with spaces is a (ignores not a SUCCESS
(Spaces) canal
palindrome case/special palindrome.
panama
handling)
Check behavior for (empty The string is a The string is
7 TC07 Empty String SUCCESS
an empty string input) palindrome. a palindrome.
Check if a string
Valid Palindrome with only special The string is a The string is
8 TC08 !!! SUCCESS
(Special Characters) characters is a palindrome. a palindrome.
palindrome
Check if a string
Invalid Palindrome with only whitespace The string is a The string is
9 TC09 "" SUCCESS
(Whitespace) is not considered a palindrome. a palindrome.
palindrome
Check if numeric
Valid Palindrome The string is a The string is
10 TC10 input is treated as a 12321 SUCCESS
(Numbers) palindrome. a palindrome.
palindrome

You might also like