This document is created by Prof. Sushopti and licensed under the Creative Commons Attribution-ShareAlike 4.0 International License. You are free to use, distribute and modify it, including for commercial purposes, provided you acknowledge the source and share-alike.
EXPERIMENT NO 1
Write a ‘c’ program to demonstrate the working of the fallowing constructs:
i) do…while ii) while… iii) if …else iv) Switch v) for Loops in C language
Objective: To understand the working of do while with different range of values and test cases
Program:
#include <stdio.h>
int main( )
{
int i,j=0,n=5;
printf("enter a number");
scanf("%d",&i);
do
{
if(i%2==0)
{
printf("%d",i);
printf("is a even number\n");
i++;
j++;
}
else
{
printf("%d",i);
printf("is a odd nuumber\n");
i++;
j++;
}
}while(i>0 && j<n);
return 0;
}
Output:
Input = 56 | Actual output |
56 is even number | |
57 is odd number | |
58 is even number | |
59 is odd number | |
60 is even number |
Test cases:
Test case no: 1
Test case name: Positive values within range
Input | Expected output | Actual output | Remarks |
Input = 56 | 56 is even number | 56 is even number | Success |
57 is odd number | 57 is odd number | ||
58 is even number | 58 is even number | ||
59 is odd number | 59 is odd number | ||
60 is even number | 60 is even number |
Test case no: 2
Test case name: Negative values within a range
Input | Expected output | Actual output | Remarks |
Input = -56 | -56 is even number | -56 is even number | Fail |
-57 is odd number | |||
-58 is even number | |||
-59 is odd number | |||
-60 is even number |
Test case no: 3
Test case name: Out of range values testing
Input | Expected output | Actual output | Remarks |
Input = 54687123416345151 | 54687123416345151 is a odd number | 2147483647 is a odd number | Fail |
Objective: To understand the working of while with different range of values and test cases
Program:
#include <stdio.h>
int main( )
{
int i,j=0,n=5;
printf("enter a number\n");
scanf("%d",&i);
while(i>0 && j<n)
{
if(i%2==0)
{
printf("%d",i);
printf("is a even number\n");
i++;
j++;
}
else
{
printf("%d",i);
printf("is a odd number\n");
i++;
j++;
}
}
return 0;
}
Output:
Input | Actual output |
Input = 7 | 7 is odd number |
8 is even number | |
9 is odd number | |
10 is even number | |
11 is odd number |
Test cases:
Test case no: 1
Test case name: Positive values within range
Input | Expected output | Actual output | Remarks |
Input = 7 | 7 is odd number | 7 is odd number | Success |
8 is even number | 8 is even number | ||
9 is odd number | 9 is odd number | ||
10 is even number | 10 is even number | ||
11 is odd number | 11 is odd number |
Test case no: 2
Test case name: Negative values within a range
Input | Expected output | Actual output | Remarks |
Input = -85 | -85 is odd number | //No output While condition fails// | Fail |
-86 is even number | |||
-87 is odd number | |||
-88 is even number | |||
-89 is odd number |
Test case no: 3
Test case name: Out of range values testing
Input | Expected output | Actual output | Remarks |
Input =123456987456321023321144563200000111223333333636666666666 | 123456987456321023321144563200000111223333333636666666666 is a even number | 21474836472147 is a odd number | Fail |
Objective: To understand the working of if else with different range of values and test cases
Program:
#include <stdio.h>
int main()
{
int a;
printf("Enter a: ");
scanf("%d",&a);
if (a%2 == 0)
{
printf("The given number is EVEN");
}
else
{
printf("The given number is ODD");
}
return 0;
}
Output:
Input = 124 | Actual output |
The given number is EVEN |
Test cases:
Test case no: 1
Test case name: Positive values within range
Input | Expected output | Actual output | Remarks |
Input = 124 | The given number is EVEN | The given number is EVEN | Success |
Test case no: 2
Test case name: Negative values within a range
Input | Expected output | Actual output | Remarks |
Input = -84123 | The given number is ODD | The given number is ODD | Fails |
Test case no: 3
Test case name: Out of range values testing
Input | Expected output | Actual output | Remarks |
Input = 22369978452435235365614652 3335664561461413031303 | The given number is ODD | The given number is ODD | Fails |
Objective: To understand the working of switch with different range of values and test cases
Program:
#include<stdio.h>
int main()
{
int a,b,c,i;
printf("1.add\n2.sub\n3.mul\n4.div\n5.enter your choice\n");
scanf("%d",&i);
printf("Enter value of a and b");
scanf("%d%d",&a,&b);
switch(i)
{
case 1:
c=a+b;
printf("Sum of no is:%d",c);
break;
case 2:
c=a-b;
printf("Substraction of no is:%d",c);
break;
case 3:
c=a*b;
printf("multiplication of no is:%d",c);
break;
case 4:
c=a/b;
printf("division of no is:%d",c);
break;
default:
printf("Enter your choice");
break;
}
return 0;
}
Output:
Input | Output |
Enter Ur choice: 1 Enter a, b Values: 5,6 | Sum of no. is: 11 |
Enter Ur choice: 2 Enter a, b Values: 9,5 | Subtraction of no. is: 4 |
Enter Ur choice: 3 Enter a, b Values: 5,6 | Multiplication of no. is: 30 |
Enter Ur choice: 4 Enter a, b Values: 20,5 | Division of no. is: 4 |
Test cases:
Test case no: 1
Test case name: Positive values within range
Input | Expected output | Actual output | Remarks |
Enter Ur choice: 1 Enter a, b Values: 5,6 | Sum of no. is: 11 | Sum of no. is: 11 | Success |
Enter Ur choice: 2 Enter a, b Values: 9,5 | Subtraction of no. is: 4 | Subtraction of no. is: 4 | |
Enter Ur choice: 3 Enter a, b Values: 5,6 | Multiplication of no. is: 30 | Multiplication of no. is: 30 | |
Enter Ur choice: 4 Enter a, b Values: 20,5 | Division of no. is: 4 | Division of no. is: 4 |
Test case no:2
Test case name: Out of range values testing
Input | Expected output | Actual output | Remarks |
Enter Ur choice: 1 Enter a, b Values: 4865121355651131256456231122315, 8452312355465612321355461213545 | Sum of no. is: 24569875351868756480120 | Sum of no. is: -2 | Fails |
Test case no: 3
Test case name: Divide by zero
Input | Expected output | Actual output | Remarks |
Enter Ur choice: 4 Enter a, b Values: 25, 0 | Division of no. is: 0 | Floating point exception(core dumped) | Fails |
Objective: To understand the working of for with different range of values and test cases
Program:
#include <stdio.h>
int main()
{
int i,j;
printf("enter a number\n");
scanf("%d",&j);
for(i=1;i<=5;i++)
{
if(i%2==0)
{
printf("%d",j);
printf("is a even no\n");
j++;
}
else
{
printf("%d",j);
printf("is a odd no\n");
j++;
}
}
return 0;
}
Output:
Input | Actual output |
Input = 63 | 63 is odd no |
64 is even no | |
65 is odd no | |
66 is even no | |
67 is odd no |
Test cases:
Test case no: 1
Test case name: Positive values within range
Input | Expected output | Actual output | Remarks |
Input = 63 | 63 is odd no | 63 is odd no | Success |
64 is even no | 64 is even no | ||
65 is odd no | 65 is odd no | ||
66 is even no | 66 is even no | ||
67 is odd no | 67 is odd no |
Test case no:2
Test case name: Negative values within a range
Input | Expected output | Actual output | Remarks |
Input = -36 | -36 is even no | -36 is odd no | Fails |
-37 is odd no | -35 is even no | ||
-38 is even no | -34 is odd no | ||
-39 is odd no | -33 is even no | ||
-40 is even no | -32 is odd no |
Test case no: 3
Test case name: Out of range values testing
Input | Expected output | Actual output | Remarks |
Input = 54878845445544545454 545444445454455445545 454554554444545455454 454544 | 2147483647 is odd no | 2147483647 is odd no | Fails |
2147483648 is even no | -2147483648 is even no | ||
2147483647 is odd no | -2147483647 is odd no | ||
2147483646 is even no | -2147483646 is even no | ||
2147483645 is odd no | -2147483645 is odd no |