[go: up one dir, main page]

0% found this document useful (0 votes)
38 views6 pages

Basics Programs

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)
38 views6 pages

Basics Programs

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/ 6

Find the maximum number in an array of integers.

Ans. (Define a maximum variable which will store the maximum in each
iteration)
#include
using namespace std;
int main() {
int arr[5] = { 1, 2, 5, 4, 3 };
int max = 0;
for(int i = 0;i<5;i++)
{
if(max < arr[i])
max = arr[i];
}
cout<<max<<endl;
return 0;
}

Try to swap two numbers with a third variable.


Ans. (store one number is a temporary variable)
#include
using namespace std;
int main() {
int number1 = 145;
int number2 = 200;
cout<<number1<<" "<<number2<<endl;
int temp = number1;
number1 = number2;
number2 = temp;
cout<<number1<<" "<<number2<<endl;
return 0;
}

1. Can you now swap two numbers without a third variable?


Ans. (use the difference and sum operation)

2. #include
3. using namespace std;
4. int main() {
5. int number1 = 145;
6. int number2 = 200;
7. cout<<number1<<" "<<number2<<endl;
8. number1 = number1 + number2;
9. number2 = number1 - number2;
10. number1 = number1 - number2;
11. cout<<number1<<" "<<number2<<endl;
12. return 0;
}
Output:
145 200
200 145

Can you check whether a number is prime or not?


Ans. (simple check whether all number below it can divide it or not)
#include
using namespace std;
int main() {
int a = 23;
int b = 2;
//start from b as 1 can divide any number
bool prime = true;
while(b!=a){
if(a%b == 0)
{
prime = false;
break;
}
b++;
}
if(prime)
cout<<"prime";
else cout<<"not prime";
return 0;
}
Output:
prime

1. Write a program to find the reverse of a number.


Ans. (try to get each digit and multiply with its corresponding power of 10)
2. #include
3. using namespace std;
4. int main() {
5. int a = 1223;
6. int res = 0;
7. while(a!=0){
8. int dig = a%10;
9. res = res*10 + dig;
10. a =a/10;
11. }
12. cout<<res;
13. return 0;
}
Output:
3221
1. Find the factorial of a number.
Ans. (Factorial of n = 12345…(n-1) n, multiply all number below given
numbers)
2. #include
3. using namespace std;
4. int factorial(int n){
5. int res = 1;
6. for(int i = 2;i<=n;i++)
7. res = res*i;
8. return res;
9. }
10. int main() {
11. int a = 5;
12. cout<<factorial(a)<<endl;
13. return 0;
}
Output:
120

#include <iostream> // Including the input-output stream header file


#include <climits> // Including the header file for integer limits
using namespace std; // Using the standard namespace
int main() // Start of the main function
{
cout << "\n\n Check the upper and lower limits of integer :\n"; // Outputting a message for checking
integer limits
cout << "--------------------------------------------------\n"; // Outputting a separator line

// Outputting the maximum and minimum limits of various data types


cout << " The maximum limit of int data type : " << INT_MAX << endl;
cout << " The minimum limit of int data type : " << INT_MIN << endl;
cout << " The maximum limit of unsigned int data type : " << UINT_MAX << endl;
cout << " The maximum limit of long long data type : " << LLONG_MAX << endl;
cout << " The minimum limit of long long data type : " << LLONG_MIN << endl;
cout << " The maximum limit of unsigned long long data type : " << ULLONG_MAX << endl;
cout << " The Bits contain in char data type : " << CHAR_BIT << endl;
cout << " The maximum limit of char data type : " << CHAR_MAX << endl;
cout << " The minimum limit of char data type : " << CHAR_MIN << endl;
cout << " The maximum limit of signed char data type : " << SCHAR_MAX << endl;
cout << " The minimum limit of signed char data type : " << SCHAR_MIN << endl;
cout << " The maximum limit of unsigned char data type : " << UCHAR_MAX << endl;
cout << " The minimum limit of short data type : " << SHRT_MIN << endl;
cout << " The maximum limit of short data type : " << SHRT_MAX << endl;
cout << " The maximum limit of unsigned short data type : " << USHRT_MAX << endl;

cout << endl; // Outputting a blank line for better readability

return 0; // Returning 0 to indicate successful program execution


} // End of the main function
#include <iostream>
using namespace std;

// Function to calculate a result based on the equality of two


integers x and y
int test(int x, int y)
{
return x == y ? (x + y) * 3 : x + y; // If x equals y, return the
sum of x and y multiplied by 3. Otherwise, return the sum of x and y.
}

// Main function
int main()
{
cout << test(1, 2) << endl; // Output the result of test function
with arguments 1 and 2
cout << test(3, 2) << endl; // Output the result of test function
with arguments 3 and 2
cout << test(2, 2) << endl; // Output the result of test function
with arguments 2 and 2
return 0; // Return 0 to indicate successful execution of the
program
3
5
12
#include <iostream> // Including input/output stream library
#include <string> // Including string library for string manipulation
using namespace std; // Using the standard namespace

// Function to reverse a string


string reverse_string(string str) {
string temp_str = str; // Creating a temporary string to store the original string
int index_pos = 0; // Initializing index position to start from the beginning

// Loop to reverse the string


for (int x = temp_str.length()-1; x >= 0; x--) // Iterating through the string in reverse order
{
str[index_pos] = temp_str[x]; // Reversing the characters and storing in the original
string
index_pos++; // Moving to the next index position
}
return str; // Returning the reversed string
}

int main()
{
cout << "Original string: w3resource"; // Displaying the original string
cout << "\nReverse string: " << reverse_string("w3resource"); // Displaying the reversed string

cout << "\n\nOriginal string: Python"; // Displaying the original string


cout << "\nReverse string: " << reverse_string("Python"); // Displaying the reversed string

return 0; // Returning 0 to indicate successful execution


}
#include <iostream>
#include <ctime> // Including the C Standard Library header for time
functions
using namespace std;

int main()
{
// current date and time in current system using time_t data type
time_t current_dt = time(0); // Get the current system time in
seconds since epoch (Jan 1, 1970)

// convert date time to string format using ctime function


char* result = ctime(&current_dt); // Convert the time_t value to a
string representing local time
// Display the current date and time in string format
cout << "The current date and time is:\n" << result << endl;
}

You might also like