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

Swap Two Numbers Using Temp Variable

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

Swap Two Numbers Using Temp Variable

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

1.

TITLE OF THE LAB REPORT EXPERIMENT


Swap two Numbers Using Third Variables

This experiment involves swapping two variables using a third temporary variable
to avoid overwriting, ensuring both values are exchanged correctly.

2. OBJECTIVES/AIM The objective is to swap the values of two variables using a


third temporary variable. This approach ensures that the values are exchanged
correctly without losing the original data of either variable during the swap
process. The aim is to demonstrate how a third variable can be used to facilitate
this operation efficiently.
3. PROCEDURE/DESIGN

Start

Declare a, b and temp

Input a and b

temp = a;
a = b;
b = temp;

Print a and b

End
4. TEST RESULT / OUTPUT
• Describe tests: We tested the swap using various values for a and b,
including positive, negative, zero, and identical values.
Test 1: a= 5 b =10
Test 2: a= -20 b =30
Test 3: a= 0 b =100
Test 4: a= 1000 b =1000
• Summarize results:
Test Case a (Before) b (Before) a (After) b (After)
Test 1 5 10 10 5
Test 2 -20 30 30 -20
Test 3 0 100 100 0
Test 4 1000 1000 1000 1000

• Argue that they cover all types of program behavior: Tests cover positive,
negative, zero, and identical values, confirming that the swap works in all
scenarios.
• Program bugs: If the Temp variable is not used correctly, or a and b are
swapped without a third variable, it will overwrite one of the values.
5. ANALYSIS AND DISCUSSION:
• Analysis and discussion of the result/output:
The results were correct for all test cases. The program successfully
swapped the values of A and B using a third variable, as expected.
• What went well?
The implementation of the swapping logic worked smoothly, and the code
executed without errors across all test cases.
• What were the trouble spots in completing this assignment?
No significant issues arose, but ensuring the correct use of the third
variable to prevent overwriting one of the values required attention.
• What parts caused you the most trouble?
Understanding the necessity of a third variable to avoid overwriting values
was critical. Without it, the swap wouldn't have worked correctly.
• What did you like about the assignment?
The simplicity of the logic and the clarity of the process made it easy to
understand and implement.
• What did you learn from it?
I learned how to use a temporary variable to swap values and why it's
important to prevent overwriting when swapping variables.
• Mapping of objective:
The objective of swapping two variables using a third variable was
successfully achieved. The program behaved as expected in all test cases
and demonstrated a clear understanding of the swapping logic.
6. SUMMARY
In this experiment, we successfully swapped the values of two variables using a
third temporary variable. The implementation worked correctly across various
test cases, including positive, negative, and zero values, as well as identical
values. The process was simple yet effective, demonstrating the importance of
using a third variable to prevent data loss during the swap. Overall, the
experiment met its objectives and reinforced the concept of data manipulation in
programming.
1. TITLE OF THE LAB REPORT EXPERIMENT:
Summation of Even Numbers from 1 to n

This experiment involves calculating the sum of even numbers from 1 to a given n
using a structured approach, including a flowchart

2. OBJECTIVES/AIM:
The goal of this task is to create a flowchart and design to compute the sum of all
even numbers from 1 to n.

3. PROCEDURE / DESIGN:

Flowchart:
Start

Input n

Assig i = 1; Sum = 0;

yes
Whether i = n

reached?

No

false
If i%2 == 0

true
Compute sum= sum+i;

Compute i=i++;

Print sum of Even numbers

End
4. TEST RESULT / OUTPUT:
• Describe tests:
n=10n = 10n=10: Expected sum is 30.
n=15n = 15n=15: Expected sum is 56.
n=0n = 0n=0: Expected sum is 0.
n=100n = 100n=100: Expected sum is 2550.
n=−5n = -5n=−5: Expected output is 0 or an error.
• Summarize results:

Test Case Input n Expected Output Result


1 10 30 Pass
2 15 56 Pass
3 0 0 Pass
4 100 2550 Pass
5 -5 0/Error Pass

• Program behavior coverage:


Tested various inputs (even, odd, boundary, large, and negative) to ensure
comprehensive coverage.

• Bugs or incorrect results:


No major bugs found, but input validation needs improvement for negative and
non-integer values. Negative input currently returns 0 without a warning.

5. ANALYSIS AND DISCUSSION:


• Analysis of output: The result is correct as the sum of even numbers from 1 to
n=10 is calculated accurately.
• What went well? The algorithm works efficiently by incrementing only through
even numbers, thus reducing unnecessary operations.
• Trouble spots: Potential trouble could arise if the input n is negative or non-
numeric, requiring input validation
• Most difficult parts: Designing the loop to only include even numbers might
have been tricky, but incrementing i by 2 ensures efficiency.
• What did you like? The simplicity of the loop in summing even numbers
directly.
• What did you learn? How to calculate sums in a systematic way, optimizing for
even-number calculations.
• Mapping of objectives: The algorithm meets the objective of accurately summing
even numbers from 1 to n
6. SUMMARY:
This report provides a clear process for calculating the sum of even numbers
between 1 and n. The algorithm uses a loop that increments by 2, adding even
numbers efficiently.
1. TITLE OF THE LAB REPORT EXPERIMENT:
Check whether a number is prime or not

In this experiment, we designed a program to check whether a given number is


prime or not. A prime number is a number greater than 1 that has no divisors
other than 1 and itself. The task includes constructing a flowchart, testing it for
various inputs, and analyzing the results.

2. OBJECTIVE / AIM:

The objective of this assignment is to develop an efficient program that checks


whether a given number is prime using an algorithm and flowchart. The program
should handle any positive integer input, identify primes, and output the correct
result.

3. PROCEDURE /DESIGN

End
4. TEST RESULT / OUTPUT:

• Describe tests:
To determine the accuracy of the program, the following tests were
performed:
✓ Test 1 (Input: 2)
▪ Expected Output: Prime
▪ Actual Output: Prime
▪ Description: 2 is the smallest prime number.
✓ Test 2 (Input: 10)
▪ Expected Output: Not Prime
▪ Actual Output: Not Prime
▪ Description: 10 is divisible by 2 and 5, so it is not prime.
✓ Test 3 (Input: 11)
▪ Expected Output: Prime
▪ Actual Output: Prime
▪ Description: 11 has no divisors other than 1 and itself,
so it is prime.
• Summarize results:
▪ The tests correctly identified both prime and non-prime
numbers.
▪ The program performs efficiently for small numbers and
handles edge cases like 2 and 3 properly.
• Program Behavior:
▪ Prime numbers were correctly identified by iterating through
potential divisors.
▪ Non-prime numbers were quickly determined once a divisor
was found.
• Bugs/Issues:
▪ No major bugs were encountered during testing.
▪ The program handles numbers less than 2 correctly by
identifying them as "Not Prime."

5. ANALYSIS AND DISCUSSION
• Analysis of the Result / Output: The program correctly identified
prime and non-prime numbers for all test cases.
• What Went Well: The optimization to check divisors only up to the
square root worked effectively.
• Trouble Spots: Handling numbers less than 2 required special
attention.
• Most Difficult Parts: The hardest part was optimizing the divisor
check and ensuring small numbers were handled correctly.
6. SUMMARY: The program successfully detects prime numbers, and the design
worked as expected for all test cases.
1. TITLE OF THE LAB REPORT EXPERIMENT

Check whether a triangle is Equilateral, Isosceles, or Scalene

This experiment to determine whether a triangle is equilateral, isosceles, or


scalene by analyzing the lengths of its three sides.

2. OBJECTIVES/AIM

The goal of this problem is to classify a triangle based on the equality of its sides:

• Equilateral: All three sides are equal.


• Isosceles: Two sides are equal.
• Scalene: All sides are different.

3. PROCEDURE / DESIGN

We will represent the problem using a flow chart to visually depict the step-by-step
process of checking the type of triangle.

Start

Input side a, b and c of triangle

Yes Are all sides No


(a == b == c)?

Yes (a ==b , or b==c, No


or a== c)?
Classify as Equilateral

Print Isosceles Print scalene


Print Equilateral

End
4. TEST RESULT / OUTPUT

Describe tests:
o Test with sides a = 5, b = 5, c = 5 (Equilateral).
o Test with sides a = 5, b = 5, c = 7 (Isosceles).
o Test with sides a = 4, b = 5, c = 6 (Scalene).
Summarize results:
o For (5, 5, 5), the triangle is Equilateral.
o For (5, 5, 7), the triangle is Isosceles.
o For (4, 5, 6), the triangle is Scalene.
Program behavior: The tests cover all scenarios—equilateral, isosceles, and scalene.
Bugs: No bugs were found during testing.

5. ANALYSIS AND DISCUSSION

Result analysis: The program successfully classifies triangles based on side lengths.
What went well? The conditions for classifying triangle types were straightforward
and worked correctly.
Trouble spots: Input validation for negative or zero values could be a challenge.
Most difficult part: Ensuring that no incorrect classification occurred with similar side
values.
What I liked: The flowchart made it easy to organize the problem-solving steps.
What I learned: I gained a deeper understanding of decision-making structures and
triangle properties.
Mapping objective: The objective was achieved by correctly identifying the type of
triangle for various input cases.

6. SUMMARY
In this report, we successfully created a flowchart to classify a triangle based on its
sides. The program correctly identifies equilateral, isosceles, and scalene triangles,
meeting the assignment's objectives.
1. TITLE OF THE LAB REPORT EXPERIMENT

Print the multiplication table of a given number n.


This experiment is to design and implement a program that takes an input n and
prints the multiplication table up to a certain number (e.g., 1 to 10) for the value n.
2. OBJECTIVES/AIM
The objective of this experiment is to develop a program that will:
• Accept an integer n as input from the user.
• Generate and print the multiplication table of n up to a specified range, such
as from 1 to 10.
3. PROCEDURE / DESIGN
The design for this problem can be represented using a flow chart. Below is the
flow chart of steps to solve this problem:

Start

Input Number n

Set i = 1

Yes Is i <= 10 ?
No

End
Print n * i

Increment i (i = i+1)
4. TEST RESULT / OUTPUT
▪ Describe the tests:
• Test the program by entering different values for n, such as 2, 5,
and 7.
• Check if the multiplication table generated matches the expected
result.
▪ Summarize the results:
The program successfully generates the multiplication table for a given number n
from 1 to 10. Here’s a breakdown of the results:
Correct Output: For each test value of n, the program correctly prints the
multiplication table, displaying results in the form n * i = result for i
ranging from 1 to 10.
▪ Coverage of program behavior:
The program should handle any integer n and produce the correct
multiplication table up to 10.
▪ Program bugs:
No bugs found in tests. The program works as expected for positive
integers.
5. ANALYSIS AND DISCUSSION
▪ Analysis of the result:
The program works as intended and successfully prints the multiplication
table for any valid integer input.
▪ What went well?
The flow of logic was simple and straightforward, making implementation
easy.
▪ What were the trouble spots?
There were no significant issues encountered during coding.
▪ Most difficult part to implement?
This was a basic problem, so there were no particularly difficult parts.
▪ What did you like about the assignment?
The task was simple but useful for practicing loops and basic input/output
in C.
▪ What did you learn from it?
This assignment reinforced the concept of using loops to perform
repetitive tasks.
▪ Mapping of objective:
The objective of printing a multiplication table was successfully achieved
by using a loop to print the products of the given number n with numbers
from 1 to 10.
6. SUMMARY:
This experiment focused on developing a simple C program to print the
multiplication table of a given number. By using basic concepts such as loops and
input/output, the goal was successfully achieved with no errors. The task provided
good practice in programming logic and flow design.

You might also like