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

C++ Programming Basics and Examples

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

C++ Programming Basics and Examples

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

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 < [Link]()) {

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

Common questions

Powered by AI

The program calculates the sum by initializing a sum variable to 0 and iterating from 1 to n, incrementing the sum by the current iterator variable in each step. When n is 7, the program computes the sum as 28 by adding the numbers 1 through 7 successively .

A for loop structure is used in the program to count vowels. This loop is appropriate because it allows iteration over each character in the string, performing a set check for vowels in a systematic and efficient manner. The loop continues until the null-terminating character '' is encountered, which marks the end of the string .

The code determines if a number is even by using the modulus operator. Specifically, it checks if the given integer n has a remainder of 0 when divided by 2. If it does, the function returns true, indicating that the number is even. Otherwise, it returns false, indicating that the number is odd. In the provided code, n is assigned the value 247, and the result is 'Odd' since 247 % 2 equals 1 .

The program counts vowels by iterating over each character in the string and incrementing a counter each time a character matches one of the vowel letters (a, e, i, o, u) in either lowercase or uppercase form. For the provided string 'GeeksforGeeks to the moon', the program identifies 9 vowels .

Character case manipulation, as demonstrated by the toggle case program, is important for various text processing tasks, such as user input normalization, case-insensitive comparisons, and formatting purposes. The ability to systematically toggle cases illustrates control over data representation, which is critical in applications dealing with diverse input formats and requires programmers to ensure consistency and correctness in text processing .

The program iterates through each character of the input string 'GeeksforGeeks'. It checks if a character is lowercase using the islower function; if true, it converts it to uppercase using toupper. Conversely, if the character is uppercase, as checked by isupper, it converts it to lowercase using tolower. The output for the input string 'GeeksforGeeks' is 'gEEKSFORgEEKS' after toggling .

The code removes vowels by iterating through the string and copying non-vowel characters to a new position in the string. The vowels are identified by checking if a character is not one of 'a', 'e', 'i', 'o', or 'u' (in both lowercase and uppercase). The output for the string 'GeeksforGeeks' is 'GksfrGks' after vowels are removed .

The program calculates the factorial by initializing a variable fact to 1 and using a while loop to multiply fact by decrementing integers starting from n until it reaches 1. For the input number 5, the calculated factorial is 120 .

The inefficiency lies in the method's in-place removal and final clearing process that involves redundant copying and multiple checks in O(n) time complexity. It could be improved by using a second string (or iterable container) to store non-vowel characters during iteration, thereby avoiding additional write operations and reducing overall complexity. Using a standard library function or a more sophisticated algorithm like a list-based filter could also enhance performance .

The method involves iterating over each character in the string and checking if it is an alphabet using the isalpha function. Only alphabetic characters are added to a result string. For the input 'Gee$ksfor$geeks', the output string containing only alphabets is 'Geeksforgeeks' .

You might also like