0% found this document useful (0 votes)
9 views11 pages

Lab Mannual

The document contains several C++ programs demonstrating basic programming concepts such as swapping numbers, identifying input types, calculating sums of squares and cubes, and finding reciprocals. Each program includes user input prompts and outputs results based on the operations performed. The programs also handle various conditions and provide menus for user interaction.

Uploaded by

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

Lab Mannual

The document contains several C++ programs demonstrating basic programming concepts such as swapping numbers, identifying input types, calculating sums of squares and cubes, and finding reciprocals. Each program includes user input prompts and outputs results based on the operations performed. The programs also handle various conditions and provide menus for user interaction.

Uploaded by

geniekhan798
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Programming

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

swapping program by if else

#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

=== Code Execution Successful ===


input is character ,digit,alphabet,special character.

#include <iostream>
using namespace std;

int main() {
char input;
cout << "Enter a input: "<<endl;
cin>>input;

if (input >= '0' && input <= '9') {


cout << "Input is a digit." << endl;
}

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.

=== Code Execution Successful ===


BY LOOOP

#include <iostream>
using namespace std;

int main()
{
char input,choice;
do
{

cout << "Enter a input: "<<endl;


cin>>input;

if (input >= '0' && input <= '9') {


cout << "Input is a digit." << endl;
}

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

=== Code Execution Successful ===

#include <iostream>

void calculateSumOfSquares(int num) {


int sum = 0;
for (int i = 1; i <= num; i++) {
sum += i * i;
}
std::cout << "The sum of squares till " << num << " is: " << sum << std::endl;
}

void calculateSumOfCubes(int num) {


int sum = 0;
for (int i = 1; i <= num; i++) {
sum += i * i * i;
}
std::cout << "The sum of cubes till " << num << " is: " << sum << std::endl;
}

int main() {
int num;
int choice;

std::cout << "Enter a number: ";


std::cin >> num;

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.

=== Code Execution Successful ===

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:

You might also like