Basic Programming Laboratory
Basic Programming Laboratory
Sumanta Pyne
Assistant Professor
Computer Science and Engineering Department
National Institute of Technology Rourkela
[email protected]
Sumanta Pyne (NITRKL) Autumn 2021, Dry run August 25, 2023 1 / 16
Dry run
Sumanta Pyne (NITRKL) Autumn 2021, Dry run August 25, 2023 2 / 16
Swap using an extra variable
Sumanta Pyne (NITRKL) Autumn 2021, Dry run August 25, 2023 3 / 16
Start, read and display input
#include<stdio.h>
int main()
{ a= ? 9
int a,b,temp;
printf(”Enter first value:”); b= ? 7
scantf(”%d ”,&a);
printf(”First value: %d\n”,a ); temp= ?
printf(”Enter second value:”);
scantf(”%d ”,&b);
printf(”Second value: %d\n”,b);
printf(”Before swap: First value = %d, Second value = %d\n”,a,b);
temp=a;
a=b;
b=temp;
printf(”After swap: First value = %d, Second value = %d\n”,a,b);
return 0;
}
Sumanta Pyne (NITRKL) Autumn 2021, Dry run August 25, 2023 4 / 16
temp ← a; store a in temp
#include<stdio.h>
int main()
{ a= ? 9
int a,b,temp;
printf(”Enter first value:”); b= ? 7
scantf(”%d ”,&a);
printf(”First value: %d\n”,a ); temp= ? 9
printf(”Enter second value:”);
scantf(”%d ”,&b);
printf(”Second value: %d\n”,b);
printf(”Before swap: First value = %d, Second value = %d\n”,a,b);
temp=a;
a=b;
b=temp;
printf(”After swap: First value = %d, Second value = %d\n”,a,b);
return 0;
}
Sumanta Pyne (NITRKL) Autumn 2021, Dry run August 25, 2023 5 / 16
a ← b; store b in a
#include<stdio.h>
int main()
{ a= ? 9 7
int a,b,temp;
printf(”Enter first value:”); b= ? 7
scantf(”%d ”,&a);
printf(”First value: %d\n”,a ); temp= ? 9
printf(”Enter second value:”);
scantf(”%d ”,&b);
printf(”Second value: %d\n”,b);
printf(”Before swap: First value = %d, Second value = %d\n”,a,b);
temp=a;
a=b;
b=temp;
printf(”After swap: First value = %d, Second value = %d\n”,a,b);
return 0;
}
Sumanta Pyne (NITRKL) Autumn 2021, Dry run August 25, 2023 6 / 16
b ← temp; store temp in b
#include<stdio.h>
int main()
{ a= ? 9 7
int a,b,temp;
printf(”Enter first value:”); b= ? 7 9
scantf(”%d ”,&a);
printf(”First value: %d\n”,a ); temp= ? 9
printf(”Enter second value:”);
scantf(”%d ”,&b);
printf(”Second value: %d\n”,b);
printf(”Before swap: First value = %d, Second value = %d\n”,a,b);
temp=a;
a=b;
b=temp;
printf(”After swap: First value = %d, Second value = %d\n”,a,b);
return 0;
}
Sumanta Pyne (NITRKL) Autumn 2021, Dry run August 25, 2023 7 / 16
Display output and stop
#include<stdio.h>
int main()
{
int a,b,temp;
printf(”Enter first value:”);
scantf(”%d ”,&a);
printf(”First value: %d\n”,a );
printf(”Enter second value:”);
scantf(”%d ”,&b);
printf(”Second value: %d\n”,b);
printf(”Before swap: First value = %d, Second value = %d\n”,a,b);
temp=a;
a=b;
b=temp;
printf(”After swap: First value = %d, Second value = %d\n”,a,b);
return 0;
}
Sumanta Pyne (NITRKL) Autumn 2021, Dry run August 25, 2023 8 / 16
Swap using addition-subtraction
Sumanta Pyne (NITRKL) Autumn 2021, Dry run August 25, 2023 9 / 16
Start, read and display input
#include<stdio.h>
int main()
{ a= ? 9
int a,b;
printf(”Enter first value:”); b= ? 7
scantf(”%d ”,&a);
printf(”First value: %d\n”,a );
printf(”Enter second value:”);
scantf(”%d ”,&b);
printf(”Second value: %d\n”,b);
printf(”Before swap: First value = %d, Second value = %d\n”,a,b);
a=a+b;
b=a-b;
a=a-b;
printf(”After swap: First value = %d, Second value = %d\n”,a,b);
return 0;
}
Sumanta Pyne (NITRKL) Autumn 2021, Dry run August 25, 2023 10 / 16
a ← a + b; store a + b in a
#include<stdio.h>
int main()
{ a= ? 9 16
int a,b;
printf(”Enter first value:”); b= ? 7
scantf(”%d ”,&a);
printf(”First value: %d\n”,a );
printf(”Enter second value:”);
scantf(”%d ”,&b);
printf(”Second value: %d\n”,b);
printf(”Before swap: First value = %d, Second value = %d\n”,a,b);
a=a+b;
b=a-b;
a=a-b;
printf(”After swap: First value = %d, Second value = %d\n”,a,b);
return 0;
}
Sumanta Pyne (NITRKL) Autumn 2021, Dry run August 25, 2023 11 / 16
b ← a - b; store a - b in b
#include<stdio.h>
int main()
{ a= ? 9 16
int a,b;
printf(”Enter first value:”); b= ? 7 9
scantf(”%d ”,&a);
printf(”First value: %d\n”,a );
printf(”Enter second value:”);
scantf(”%d ”,&b);
printf(”Second value: %d\n”,b);
printf(”Before swap: First value = %d, Second value = %d\n”,a,b);
a=a+b;
b=a-b;
a=a-b;
printf(”After swap: First value = %d, Second value = %d\n”,a,b);
return 0;
}
Sumanta Pyne (NITRKL) Autumn 2021, Dry run August 25, 2023 12 / 16
a ← a - b; store a - b in a
#include<stdio.h>
int main()
{ a= ? 9 16 7
int a,b,temp;
printf(”Enter first value:”); b= ? 7 9
scantf(”%d ”,&a);
printf(”First value: %d\n”,a );
printf(”Enter second value:”);
scantf(”%d ”,&b);
printf(”Second value: %d\n”,b);
printf(”Before swap: First value = %d, Second value = %d\n”,a,b);
a=a+b;
b=a-b;
a=a-b;
printf(”After swap: First value = %d, Second value = %d\n”,a,b);
return 0;
}
Sumanta Pyne (NITRKL) Autumn 2021, Dry run August 25, 2023 13 / 16
Display output and stop
#include<stdio.h>
int main()
{
int a,b;
printf(”Enter first value:”);
scantf(”%d ”,&a);
printf(”First value: %d\n”,a );
printf(”Enter second value:”);
scantf(”%d ”,&b);
printf(”Second value: %d\n”,b);
printf(”Before swap: First value = %d, Second value = %d\n”,a,b);
a=a+b;
b=a-b;
a=a-b;
printf(”After swap: First value = %d, Second value = %d\n”,a,b);
return 0;
}
Sumanta Pyne (NITRKL) Autumn 2021, Dry run August 25, 2023 14 / 16
Swap using multiplication-division
Write a C program to swap two integer values using multiply and divide operations.
Perform a dry run of your code considering the given input is 9 and 7.
Sumanta Pyne (NITRKL) Autumn 2021, Dry run August 25, 2023 15 / 16
Thank you
Sumanta Pyne (NITRKL) Autumn 2021, Dry run August 25, 2023 16 / 16