0% found this document useful (0 votes)
2 views

C++ Code

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

C++ Code

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Number is Even Or Odd

C++
// C++ program to check
// for even or odd
#include <iostream>
using namespace std;

// Returns true if n is
// even, else odd
bool isEven(int n) { return (n % 2 == 0); }

// Driver code
int main()
{
int n = 247;
if (isEven(n) == true) {
cout << "Even" << endl;
}
else {
cout << "Odd";
}

return 0;
}

Output
Odd

8. Write a Program to Toggle Each Character in a String


C++
// C++ Program to toggle string
#include <cstring>
#include <iostream>
using namespace std;

int main()
{
string str = "GeeksforGeeks";

for (int i = 0; str[i] != '\0'; i++) {


if (islower(str[i])) {
str[i] = toupper(str[i]);
}
else if (isupper(str[i])) {
str[i] = tolower(str[i]);
}
}

cout << "Toggled string: " << str << endl;

return 0;
}

Output
Toggled string: gEEKSFORgEEKS

9. Write a Program to Count the Number of Vowels

// C++ Program to count the number of vowels

#include <cstring>

#include <iostream>

Using namespace std;

Int main()

String str = “GeeksforGeeks to the moon”;

Int vowels = 0;

For (int I = 0; str[i] != ‘\0’; i++) {

If (str[i] == ‘a’ || str[i] == ‘e’ || str[i] == ‘I’

|| str[i] == ‘o’ || str[i] == ‘u’

|| str[i] == ‘A’ || str[i] == ‘E’

|| str[i] == ‘I’ || str[i] == ‘O’


|| str[i] == ‘U’) {

Vowels++;

Cout << “Number of vowels in the string: “ << vowels

<< endl;

Return 0;

Output

Number of vowels in the string: 9

10. Write a Program to Remove the Vowels from a String


C++
// C++ Program to remove the vowels from a string
#include <cstring>
#include <iostream>
using namespace std;

int main()
{
int j = 0;

string str = "GeeksforGeeks";

for (int i = 0; str[i] != '\0'; i++) {


if (str[i] != 'a' && str[i] != 'e' && str[i] != 'i'
&& str[i] != 'o' && str[i] != 'u'
&& str[i] != 'A' && str[i] != 'E'
&& str[i] != 'I' && str[i] != 'O'
&& str[i] != 'U') {
str[j++] = str[i];
}
}
while (j < str.size()) {

str[j] = '\0';

j++;
}
cout << "String without vowels: " << str << endl;

return 0;
}

Output
String without vowels: GksfrGks

13. Write a Program to Find the Sum of the First N Natural


Numbers
C++
// C++ program to find
// Sum of first
// n natural numbers.
#include <iostream>
using namespace std;

// Function to find sum


int findSum(int n)
{
int sum = 0;
for (int i = 1; i <= n; i++)
sum = sum + i;
return sum;
}

int main()
{
int n = 7;
cout << findSum(n);
return 0;
}

Output
28
Write a Program to Remove All Characters From a String Except
Alphabets
C++
// C++ Programto remove all characters from a string except
// alphabets
#include <cctype>
#include <iostream>
#include <string>

using namespace std;

string remove_non_alphabets(string str)


{
string result = "";
for (char c : str) {
if (isalpha(c)) {
result += c;
}
}
return result;
}

int main()
{
string str = "Gee$ksfor$geeks";

cout << "Alphabets only: " << remove_non_alphabets(str)


<< endl;

return 0;
}

Output
Alphabets only: Geeksforgeeks
Write a Program to Find the Factorial of a Number Using Loops
C++
// C++ program to find factorial using loops
#include <bits/stdc++.h>
using namespace std;

// function to find factorial


int factorial(int n)
{
int fact = 1;
while (n > 1) {
fact *= n;
n--;
}

return fact;
}

// driver code
int main()
{
int num = 5;

cout << factorial(num);

return 0;
}

Output
120

You might also like