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

Simple Programs

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 views3 pages

Simple Programs

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/ 3

Program to count the number of even and odd elements in an array.

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

Program to find the average of numbers in an array.


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

Program to print the given digit in words.


#include <iostream>
using namespace std;
int main() {
int digit;
cout << "Enter a digit: ";
cin >> digit;
switch(digit){
case 0: cout<<"Zero"; break;
case 1: cout<<"One"; break;
case 2: cout<<"Two"; break;
case 3: cout<<"Three"; break;
case 4: cout<<"Four"; break;
case 5: cout<<"Five"; break;
case 6: cout<<"Six"; break;
case 7: cout<<"Seven"; break;
case 8: cout<<"Eight"; break;
case 9: cout<<"Nine"; break;
default: cout<<"Not a digit";
}
return 0;
}

Program to check if a large number is divisible by 6 or not.


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

Program to check if a number is a palindrome.


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

Program to count vowels in a string.


#include <iostream>
using namespace std;
int main() {
string str = "hello world";
int count=0;
for(char c: str){
if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u') count++;
}
cout << "Vowels: " << count;
return 0;
}

Program to check if the given number is a perfect square.


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

Program to find the maximum element in a matrix.


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

Program to find the sum of the elements of an array.


#include <iostream>
using namespace std;
int main() {
int arr[] = {1,2,3,4,5};
int sum = 0;
for(int i=0;i<5;i++) sum += arr[i];
cout << "Sum = " << sum;
return 0;
}

Program to swap two numbers without using a third variable.


#include <iostream>
using namespace std;
int main() {
int a=5, b=10;
a = a+b;
b = a-b;
a = a-b;
cout << "a=" << a << ", b=" << b;
return 0;
}

You might also like