Lab Mannual
Lab Mannual
swap of a program
#include <iostream>
using namespace std;
int main()
{
int a,b,temp;
cout << "enter a number"<<endl;
cin>>a;
cout<<"enter a number:"<<endl;
cin>>b;
cout<<"before swapping"<<endl;
cout<<"a="<<a<<",b="<<b<<endl;
temp=a;
a=b;
b=temp;
cout<<"after swapping"<<endl;
cout<<"a="<<a<<",b="<<b<<endl;
return 0;
return 0;
}
output
enter a number
3
enter a number:
7
before swapping
a=3,b=7
after swapping
a=7,b=3
#include <iostream>
using namespace std;
int main()
{
int a,b,temp;
cout << "enter a "<<endl;
cin>>a;
cout<<"enter b:"<<endl;
cin>>b;
if(a!=b)
{
temp=a;
a=b;
b=temp;
cout<<"a="<<a<<",b="<<b<<endl;
}
else
{
cout<<"numbers are same no swapping needed"<<endl;
return 0;
}
} enter a
9
enter b:
9
numbers are same no swapping needed
#include <iostream>
using namespace std;
int main() {
char input;
cout << "Enter a input: "<<endl;
cin>>input;
else if ((input >= 'a' && input <= 'z') || (input >= 'A' && input <= 'Z')) {
cout << "Input is an alphabet." << endl;
}
else if (!isalnum(input)) {
cout << "Input is a special character." << endl;
}
else {
cout << "Input is a character." << endl;
}
return 0;
}
Enter an input:
*
Input is a special character.
#include <iostream>
using namespace std;
int main()
{
char input,choice;
do
{
else if ((input >= 'a' && input <= 'z') || (input >= 'A' && input <= 'Z')) {
cout << "Input is an alphabet." << endl;
}
else if (!isalnum(input)) {
cout << "Input is a special character." << endl;
}
else {
cout << "Input is a character." << endl;
}
cout<<"do you want to continue?(y/n)"<<endl;
cin>>choice;}
while(choice=='y'||choice=='Y');
return 0;
}
Enter a input:
7
Input is a digit.
do you want to continue?(y/n)
Y
Enter a input:
8
Input is a digit.
do you want to continue?(y/n)
N
#include <iostream>
int main() {
int num;
int choice;
do {
std::cout << "\nMenu:" << std::endl;
std::cout << "1. Calculate Sum of Squares" << std::endl;
std::cout << "2. Calculate Sum of Cubes" << std::endl;
std::cout << "3. Exit" << std::endl;
std::cout << "Enter your choice: ";
std::cin >> choice;
switch (choice) {
case 1:
calculateSumOfSquares(num);
break;
case 2:
calculateSumOfCubes(num);
break;
case 3:
std::cout << "Exiting program." << std::endl;
return 0;
default:
std::cout << "Invalid choice. Please choose a valid option." << std::endl;
break;
}
} while (true);
return 0;
}
Enter a number: 8
Menu:
1. Calculate Sum of Squares
2. Calculate Sum of Cubes
3. Exit
Enter your choice: 2
The sum of cubes till 8 is: 1296
Menu:
1. Calculate Sum of Squares
2. Calculate Sum of Cubes
3. Exit
Enter your choice: 1
The sum of squares till 8 is: 204
Menu:
1. Calculate Sum of Squares
2. Calculate Sum of Cubes
3. Exit
Enter your choice: 3
Exiting program.
reciprocal finding
#include <iostream>
int main() {
double num;
int choice;
do {
std::cout << "Menu:" << std::endl;
std::cout << "1. Find Reciprocal" << std::endl;
std::cout << "2. Exit" << std::endl;
std::cout << "Enter your choice: ";
std::cin >> choice;
if (choice == 1) {
std::cout << "Enter a number: ";
std::cin >> num;
if (num != 0) {
double reciprocal = 1 / num;
std::cout << "The reciprocal of " << num << " is: " << reciprocal <<
std::endl;
} else {
std::cout << "Error: Reciprocal of zero is undefined." << std::endl;
}
} else if (choice == 2) {
std::cout << "Exiting program." << std::endl;
} else {
std::cout << "Invalid choice. Please choose a valid option." << std::endl;
}
} while (choice != 2);
return 0;
}
Menu:
1. Find Reciprocal
2. Exit
Enter your choice: 1
Enter a number: 9
The reciprocal of 9 is: 0.111111
Menu:
1. Find Reciprocal
2. Exit
Enter your choice: 1
Enter a number: 2
The reciprocal of 2 is: 0.5
Menu:
1. Find Reciprocal
2. Exit
Enter your choice: 1
Enter a number: 4
The reciprocal of 4 is: 0.25
Menu:
1. Find Reciprocal
2. Exit
Enter your choice: