0% found this document useful (0 votes)
4 views2 pages

C++ Basics: Sums, Strings, and Loops

Uploaded by

Abhilash Alshi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

C++ Basics: Sums, Strings, and Loops

Uploaded by

Abhilash Alshi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

31) Sum of Even Numbers

#include <iostream>
using namespace std;
int main() {
int n = 10, sum = 0;
for(int i = 2; i <= n; i+=2)
sum += i;
cout << "Sum = " << sum;
return 0;
}
Output: Sum = 30

32) Sum of Odd Numbers


#include <iostream>
using namespace std;
int main() {
int n = 10, sum = 0;
for(int i = 1; i <= n; i+=2)
sum += i;
cout << "Sum = " << sum;
return 0;
}
Output: Sum = 25

33) Print Alphabets A-Z


#include <iostream>
using namespace std;
int main() {
for(char ch = 'A'; ch <= 'Z'; ch++)
cout << ch << " ";
return 0;
}
Output: A B C ... Z

34) Print Numbers 1-10


#include <iostream>
using namespace std;
int main() {
for(int i = 1; i <= 10; i++)
cout << i << " ";
return 0;
}
Output: 1 2 3 4 5 6 7 8 9 10

35) Calculate Average


#include <iostream>
using namespace std;
int main() {
int a=10, b=20, c=30;
float avg = (a+b+c)/3.0;
cout << "Average = " << avg;
return 0;
}
Output: Average = 20

36) Print Stars


#include <iostream>
using namespace std;
int main() {
for(int i=1;i<=5;i++){
for(int j=1;j<=i;j++)
cout<<"*";
cout<<"\n";
}
return 0;
}
Output: * ** *** **** *****

37) Reverse String


#include <iostream>
#include <string>
using namespace std;
int main() {
string s = "hello";
for(int i=[Link]()-1;i>=0;i--)
cout<<s[i];
return 0;
}
Output: olleh

38) Length of String


#include <iostream>
#include <string>
using namespace std;
int main() {
string s = "hello";
cout << "Length = " << [Link]();
return 0;
}
Output: Length = 5

39) Concatenate Strings


#include <iostream>
#include <string>
using namespace std;
int main() {
string s1="Hello ", s2="World";
cout << s1+s2;
return 0;
}
Output: Hello World

40) Copy String


#include <iostream>
#include <string>
using namespace std;
int main() {
string s1="Hello";
string s2=s1;
cout << "Copied: " << s2;
return 0;
}
Output: Copied: Hello

You might also like