Simple C programs
a). Swapping of two numbers using C
Aim :
To write a C program to swap two numbers.
Algorithm:
Step 1 - START.
Step 2 – Get two numbers, num1 and num2 from the user.
Step 3 -Create a temporary variable temp.
Step 4 -Store the value of num1 in temp.
Step 5 -Assign the value of num2 to num1.
Step 6 -Assign the value of temp to num2.
Step 7 –Print the values of num1 and num2.
Step 8 -The values of num1 and num2 have been swapped.
Step 9 -STOP.
Program:
#include <stdio.h>
int main() {
int num1, num2, temp;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
printf("Before swapping: num1 = %d, num2 = %d\n", num1, num2);
temp = num1;
num1 = num2;
num2 = temp;
printf("After swapping: num1 = %d, num2 = %d\n", num1, num2);
return 0;
}
Flowchart:
, Output:
Result:
Thus the program for swapping two numbers has been executed successfully.
a). Swapping of two numbers using C
Aim :
To write a C program to swap two numbers.
Algorithm:
Step 1 - START.
Step 2 – Get two numbers, num1 and num2 from the user.
Step 3 -Create a temporary variable temp.
Step 4 -Store the value of num1 in temp.
Step 5 -Assign the value of num2 to num1.
Step 6 -Assign the value of temp to num2.
Step 7 –Print the values of num1 and num2.
Step 8 -The values of num1 and num2 have been swapped.
Step 9 -STOP.
Program:
#include <stdio.h>
int main() {
int num1, num2, temp;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
printf("Before swapping: num1 = %d, num2 = %d\n", num1, num2);
temp = num1;
num1 = num2;
num2 = temp;
printf("After swapping: num1 = %d, num2 = %d\n", num1, num2);
return 0;
}
Flowchart:
, Output:
Result:
Thus the program for swapping two numbers has been executed successfully.