C++ Codes

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 22

#include <iostream> //code print alternate numbers which are repeating

using namespace std;

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 ()

int input,even,odd, n=1,n1=1;


even=0, odd=0;
while (input !=0)

{ cout << "Enter the number: ";


cin >> input;

if(input%2==0)

{
even=(even + input);
n++;
}
else if(input%2!=0)

{
odd=(odd + input);
n1++;
}
}

float avg_even = (float) even/n;


float avg_odd = odd/n;

cout << avg_even;

#include <iostream> //count number of lcm


using namespace std;

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++;

cout << factorial;

#include <iostream> //fibannaci


using namespace std;
int main ()

int x=0,y=1,n,z;
cout << "Enter n";
cin >> n;

for(int i=2;i<=n; i++ )

{
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;
}

#include <iostream> //HCF


using namespace std;
int main()

int x, y,z;

cout << "Enter the smaller number: ";


cin >> x;

cout << "Enter the larger number: ";


cin >> y;

while (x%y!=0)
{
z = x % y;

x = y;
y = z;
}
cout << z;
}
#include <iostream> //largest common substring
#include <cstring>

using namespace std;


int main() {
char word1[100], word2[100];
int length1, length2;
int maxSubstringLength = 0;
int start1 = 0, start2 = 0;

cout << "Enter first word: ";


cin >> word1;
cout << "Enter second word: ";
cin >> word2;

length1 = strlen(word1);
length2 = strlen(word2);

for (int i = 0; i < length1; i++) {


for (int j = 0; j < length2; j++) {
int k = 0;
while (i + k < length1 && j + k < length2 && word1[i+k] ==
word2[j+k]) {
k++;
}
if (k > maxSubstringLength) {
maxSubstringLength = k;
start1 = i;
start2 = j;
}
}
}

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

using namespace std;

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;
}

if (n<largest && n>slargest)

{
slargest=n;
}
}

cout << largest << endl << slargest;

}
#include <iostream> // lcm of two numbers

using namespace std;

int main()
{
int num1, num2, lcm;

cout << "Enter first number: ";


cin >> num1;

cout << "Enter second number: ";


cin >> num2;

// Find maximum of two numbers


int max = (num1 > num2) ? num1 : num2;

// Start loop from maximum of two numbers


for (int i = max; ; i += max)
{
// Check if i is a multiple of both numbers
if (i % num1 == 0 && i % num2 == 0)
{
lcm = i;
break;
}
}

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 << "Enter three numbers: ";


cin >> num1 >> num2 >> num3;

// finding the maximum number among the three


int max = num1 > num2 ? (num1 > num3 ? num1 : num3) : (num2 > num3 ? num2 :
num3);

// iterating through each number starting from the maximum number


for (lcm = max; ; lcm += max) {
if (lcm % num1 == 0 && lcm % num2 == 0 && lcm % num3 == 0) {
break;
}
}

cout << "LCM of " << num1 << ", " << num2 << ", and " << num3 << " is " << lcm
<< endl;

return 0;
}

//#include <iostream> //number of words in string


//#include <string>
//using namespace std;
//int main()
//
//{
// string x;
//
//
// cout << "Enter the sentence: ";
//
// getline(cin, x);
//
// int y = x.length() - 1, n = 0,word=1;
//
// while (n <= y)
//
// {
//
// if (x[n]==' ')
//
//
// {
// word = word + 1;
// n++;
//
// }
//
// else
// {
// n++;
// }
//
//
//
//
// }
//
// cout << word;
//
//
//
//}

include <iostream> //read number 1 to 10


using namespace std;
int main ()

{
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;

bool isSubstring(string str1, string str2) {


int len1 = str1.length();
int len2 = str2.length();

int limit = len1 - len2 + 1;


if (limit < 0) {
limit = len2 - len1 + 1;
}

for (int i = 0; i < limit; i++) {


bool match = true;
for (int j = 0; j < len2; j++) {
if (str1[i+j] != str2[j]) {
match = false;
break;
}
}
if (match) {
return true;
}
}

return false;
}
#include <iostream> /* ******
*****
****
and so on */
using namespace std;

int main() {
int row = 5;
int col = 5;

while (row >= 1) {


int space = 5 - row;
int star = row;

// Print spaces
while (space > 0) {
cout << " ";
space--;
}

// Print stars
while (star > 0) {
cout << "* ";
star--;
}

// Move to the next row


cout << endl;
row--;
}

return 0;
}

#include <iostream> /* pattern 12345


1234
and so on*/
using namespace std;
int main ()

{
int rows=5,i=5;
while (i>=1)

{
int j=1;

while (j<=i)

{
cout << j;

j++;
}

cout << endl;


i--;

}
}

#include <iostream> /* pattern 111111


22222
and so on*/

using namespace std;

int main() {
int rows = 5; // number of rows in the pattern
int i = 1; // starting number for the pattern

// loop for rows


while (i <= rows + 1) {
// loop for columns
int j = 1;
while (j <= 6) {
cout << i << " ";
j++;}

// move to next row


cout << endl;
i++;
}
return 0;
}
#include <iostream> //perfect cube, also do while loop example

using namespace std;

int main() {
int x;
bool isPerfectCube = false;
int i = 1;

cout << "Enter an integer: ";


cin >> x;

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;

cout << "Enter the Number: ";


cin >> x;

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);

string output=" ";


int i,j;

for (i=0;i<x.length();i=j+1)

{
string word = " ";

for (j=i;x[j]!= ' ' && j< x.length();j++)

{
word = x[j] + word;

}
output = output + " " + word;
}
cout << output;

return 0;
}
#include <iostream> // second largest string
using namespace std;
int main ()

string input;

string longest = "", second_longest= "";

int n=0;

while (n<50)

{
cout << "Enter the string: ";
cin >> input;

int lonlength=longest.length(),
sholength=second_longest.length(),strlength=input.length();

if (strlength > lonlength)

{ second_longest = longest;
longest = input;

}
else if (strlength > sholength && input != longest)

{
second_longest = input;
}

n++;

cout << second_longest <<endl;

}
#include <iostream> //smallest and largest integer

using namespace std;

int main()
{
int num, smallest, largest;

cout << "Enter a positive integer (0 or negative integer to exit): ";


cin >> num;

// Initialize smallest and largest to the first number entered


smallest = largest = num;

// Keep taking positive integers as input until the user enters 0 or a


negative integer
while (num > 0)
{
cout << "Enter a positive integer (0 or negative integer to exit): ";
cin >> num;

if (num > 0)
{
if (num < smallest)
{
smallest = num;
}
if (num > largest)
{
largest = num;
}
}
}

// Print the smallest and largest numbers


cout << "Smallest positive integer entered: " << smallest << endl;
cout << "Largest positive integer entered: " << largest << endl;

}
#include <iostream> // will give sum of positve and negative numbers

using namespace std;

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;
}

cout << positive << endl << negative;

}
#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;
}

#include <iostream> /*pattern 111111


222222
33333
444
55
6
*/
using namespace std;
int main ()

int rows=6,i=1;

while (i<=rows)

{
int j=1;
while (j<=rows +1 - i)

{
cout << i;
j++;
}
cout << endl;
i++;
}
}

#include <iostream> //switch example


using namespace std;

int main() {
int x, y;
cout<<"Enter first number: ";
cin>>x;
cout<<"Enter second number: ";
cin>>y;

int result = (x > y) ? 1 : (x < y) ? 2 : 3;

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 length = x.length() -1 ;

int n=0;

while (n<=length)

{
char y=x[n];

if (y>=65 && y<=90)

{
y=y+32;

n++;

}
else if (y >=97 && y<=122)

{y= y-32;
n++;
}
cout << y;
}

You might also like