C++ Codes
C++ Codes
C++ Codes
int main() {
int prev_num, curr_num;
cout << "Enter integers (0 to stop): ";
cin >> prev_num;
cin >> curr_num;
while (curr_num != 0) {
if (curr_num == prev_num) {
cout << curr_num << " ";
while (curr_num == prev_num && curr_num != 0) {
cin >> curr_num;
}
}
else {
prev_num = curr_num;
cin >> curr_num;
}
}
return 0;
}
#include <iostream> //finds avg of odd and even inputs until 0 entered
using namespace std;
int main ()
if(input%2==0)
{
even=(even + input);
n++;
}
else if(input%2!=0)
{
odd=(odd + input);
n1++;
}
}
int main() {
int num1, num2;
cout << "Enter two numbers: ";
cin >> num1 >> num2;
int min_num = (num1 < num2) ? num1 : num2; // find the minimum of num1 and
num2
int i = 1, count = 0;
while (i <= min_num) {
if (num1 % i == 0 && num2 % i == 0) {
count++; // increment count if i divides both num1 and num2
}
i++; // move on to the next number
}
cout << "The number of common factors of " << num1 << " and " << num2 << "
is " << count << endl;
return 0;
}
#include <iostream> //factorial
using namespace std;
int main()
int x;
cout << "Enter the Number: ";
cin >> x;
int n=1,factorial=1;
while (n<=x)
{
factorial= n*factorial;
n++;
int x=0,y=1,n,z;
cout << "Enter n";
cin >> n;
{
z=x+y;
x=y;
y=z;
}
cout << z;
}
#include <iostream> // GCF of 3 numbers
using namespace std;
int main() {
int num1, num2, num3, gcf;
cout << "Enter three integers: ";
cin >> num1 >> num2 >> num3;
for (int i = 1; i <= num1 && i <= num2 && i <= num3; i++) {
if (num1 % i == 0 && num2 % i == 0 && num3 % i == 0) {
gcf = i;
}
}
cout << "The GCF of " << num1 << ", " << num2 << ", and " << num3 << " is "
<< gcf << endl;
return 0;
}
int x, y,z;
while (x%y!=0)
{
z = x % y;
x = y;
y = z;
}
cout << z;
}
#include <iostream> //largest common substring
#include <cstring>
length1 = strlen(word1);
length2 = strlen(word2);
if (maxSubstringLength > 0) {
cout << "The largest common substring is: ";
for (int i = start1; i < start1 + maxSubstringLength; i++) {
cout << word1[i];
}
cout << endl;
} else {
cout << "There is no common substring between the two words." << endl;
}
return 0;
}
#include <iostream>//largest and second largest integer
int main()
{
int largest=0,slargest=0,n;
while (n!=0)
{
cout << "Enter n";
cin >> n;
if (n>largest)
{
int temp = largest;
largest=n;
slargest=temp;
}
{
slargest=n;
}
}
}
#include <iostream> // lcm of two numbers
int main()
{
int num1, num2, lcm;
cout << "LCM of " << num1 << " and " << num2 << " is " << lcm;
return 0;
}
#include <iostream> // lcm of 3 numbers
using namespace std;
int main() {
int num1, num2, num3, lcm;
cout << "LCM of " << num1 << ", " << num2 << ", and " << num3 << " is " << lcm
<< endl;
return 0;
}
{
int rows=4,i,k=1;
for (i=1;i<=rows;i++)
{
for (int j=1;j<=i;j++)
{ cout << k;
k++;}
cout << endl;
}
}
#include <iostream> // checks if one is substring of another
using namespace std;
return false;
}
#include <iostream> /* ******
*****
****
and so on */
using namespace std;
int main() {
int row = 5;
int col = 5;
// Print spaces
while (space > 0) {
cout << " ";
space--;
}
// Print stars
while (star > 0) {
cout << "* ";
star--;
}
return 0;
}
{
int rows=5,i=5;
while (i>=1)
{
int j=1;
while (j<=i)
{
cout << j;
j++;
}
}
}
int main() {
int rows = 5; // number of rows in the pattern
int i = 1; // starting number for the pattern
int main() {
int x;
bool isPerfectCube = false;
int i = 1;
do {
if (i * i * i == x) {
isPerfectCube = true;
break;
}
i++;
} while (i * i * i <= x);
if (isPerfectCube) {
cout << x << " is a perfect cube" << endl;
} else {
cout << x << " is not a perfect cube" << endl;
}
return 0;
}
#include <iostream> //prime number calculator
using namespace std;
int main()
int x, y = 1, count = 0;
while (y <= x)
{
if (x%y==0)
{
count = count + 1;
y++;
}
else
{
y++;
}
}
if (count == 2)
{
cout << "Prime";
else {
cout << "not prime ";
}
}
//#include <iostream> //reversing characters in string
//#include <string>
//using namespace std;
//int main()
//
//{
// string x;
//
// cout << " Please enter the string: ";
// cin >> x;
//
// int y = x.length()-1;
//
//
// int i = 0;
//
// while (i <= y)
//
// {
// swap(x[i], x[y]);
//
// i=i+1;
// y = y - 1;
// }
// cout << x;
//
//}
#include <iostream> //reversing words in a sentence
#include <string>
using namespace std;
int main()
{
string x;
getline(cin,x);
for (i=0;i<x.length();i=j+1)
{
string word = " ";
{
word = x[j] + word;
}
output = output + " " + word;
}
cout << output;
return 0;
}
#include <iostream> // second largest string
using namespace std;
int main ()
string input;
int n=0;
while (n<50)
{
cout << "Enter the string: ";
cin >> input;
int lonlength=longest.length(),
sholength=second_longest.length(),strlength=input.length();
{ second_longest = longest;
longest = input;
}
else if (strlength > sholength && input != longest)
{
second_longest = input;
}
n++;
}
#include <iostream> //smallest and largest integer
int main()
{
int num, smallest, largest;
if (num > 0)
{
if (num < smallest)
{
smallest = num;
}
if (num > largest)
{
largest = num;
}
}
}
}
#include <iostream> // will give sum of positve and negative numbers
int main()
{
int x,positive=0,negative=0;
while (x!=0)
{
cout << "Enter x";
cin >> x;
if (x>0)
{
positive=positive+x;}
else
{
negative = negative + x;
}
}
#include <iostream> //read alternate words in string
using namespace std;
int main()
string x;
cout << "Enter the string ";
cin >> x;
int y=x.length();
int z=1;
while (z < y)
{
string a = x.substr(z,1);
cout << a;
z=z+2;
}
int rows=6,i=1;
while (i<=rows)
{
int j=1;
while (j<=rows +1 - i)
{
cout << i;
j++;
}
cout << endl;
i++;
}
}
int main() {
int x, y;
cout<<"Enter first number: ";
cin>>x;
cout<<"Enter second number: ";
cin>>y;
switch(result) {
case 1:
cout << "\n First number is greater than the second number";
break;
case 2:
cout << "\n Second number is greater than the first number";
break;
case 3:
cout << "\n Both of them are equal";
break;
}
return 0;
}
#include <iostream> //upper case string
using namespace std;
int main() {
string x;
cout << "Enter the word ";
cin >> x;
int n=0;
while (n<=length)
{
char y=x[n];
{
y=y+32;
n++;
}
else if (y >=97 && y<=122)
{y= y-32;
n++;
}
cout << y;
}