KEMBAR78
Simple Programs | PDF | Namespace | Software Engineering
0% found this document useful (0 votes)
3 views2 pages

Simple Programs

The document contains a series of C++ code snippets demonstrating various programming tasks, including counting even and odd elements in an array, calculating the average of numbers, printing digits in words, checking divisibility, determining palindrome status, counting vowels in a string, checking for perfect squares, finding the maximum element in a matrix, and summing digits. Each code snippet is accompanied by its expected output. The examples illustrate basic programming concepts and operations in C++.

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)
3 views2 pages

Simple Programs

The document contains a series of C++ code snippets demonstrating various programming tasks, including counting even and odd elements in an array, calculating the average of numbers, printing digits in words, checking divisibility, determining palindrome status, counting vowels in a string, checking for perfect squares, finding the maximum element in a matrix, and summing digits. Each code snippet is accompanied by its expected output. The examples illustrate basic programming concepts and operations in C++.

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
You are on page 1/ 2

1) Count Even and Odd Elements in an Array

#include <iostream>
using namespace std;
int main() {
int arr[] = {1, 2, 3, 4, 5, 6};
int n = 6, even = 0, odd = 0;
for(int i=0; i<n; i++) {
if(arr[i] % 2 == 0) even++;
else odd++;
}
cout << "Even: " << even << "\nOdd: " << odd;
return 0;
}
Output: Even: 3 Odd: 3

2) Average of Numbers in Array


#include <iostream>
using namespace std;
int main() {
int arr[] = {10, 20, 30, 40, 50};
int n = 5, sum = 0;
for(int i=0; i<n; i++) sum += arr[i];
cout << "Average = " << (sum / n);
return 0;
}
Output: Average = 30

3) Print Digit in Words


#include <iostream>
using namespace std;
int main() {
int n = 123;
string words[] = {"zero","one","two","three","four","five","six","seven","eight","nine"};
cout << words[1] << " " << words[2] << " " << words[3];
return 0;
}
Output: one two three

4) Check if Number Divisible by 6


#include <iostream>
using namespace std;
int main() {
long long n = 123456;
if(n % 6 == 0) cout << "Divisible by 6";
else cout << "Not divisible";
return 0;
}
Output: Divisible by 6

5) Palindrome Number
#include <iostream>
using namespace std;
int main() {
int n = 121, rev = 0, temp = n;
while(temp > 0) {
rev = rev*10 + temp%10;
temp /= 10;
}
if(rev == n) cout << "Palindrome";
else cout << "Not Palindrome";
return 0;
}
Output: Palindrome

6) Count Vowels in String


#include <iostream>
using namespace std;
int main() {
string s = "Hello World";
int count = 0;
for(char c : s) {
if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u'||
c=='A'||c=='E'||c=='I'||c=='O'||c=='U')
count++;
}
cout << "Vowels = " << count;
return 0;
}
Output: Vowels = 3

7) Perfect Square Check


#include <iostream>
#include <cmath>
using namespace std;
int main() {
int n = 49;
int root = sqrt(n);
if(root*root == n) cout << "Perfect Square";
else cout << "Not Perfect Square";
return 0;
}
Output: Perfect Square

8) Maximum Element in Matrix


#include <iostream>
using namespace std;
int main() {
int mat[2][2] = {{1, 5}, {7, 3}};
int maxVal = mat[0][0];
for(int i=0;i<2;i++)
for(int j=0;j<2;j++)
if(mat[i][j] > maxVal) maxVal = mat[i][j];
cout << "Max = " << maxVal;
return 0;
}
Output: Max = 7

9) Sum of Digits
#include <iostream>
using namespace std;
int main() {
int n = 1234, sum = 0;
while(n > 0) {
sum += n % 10;
n /= 10;
}
cout << "Sum = " << sum;
return 0;
}
Output: Sum = 10

You might also like