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

c++ problems

Uploaded by

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

c++ problems

Uploaded by

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

C++ PROBLEMS

ON ARRAYS
1.to find the 3 biggest numbers in an array

#include<iostream>

using namespace std;

int main(){

int n;

cout<< "enter no of elements od array";

cin>> n;

int arr[n];

cout<< " enter elements";

for(int i=0 ; i<n;i++){

cin>> arr[i];

for(int i=0;i<n-1;i++){

for ( int j=i+1;j<n;j++){

if(arr[j]< arr[i]){

int temp=arr[i];

arr[i]=arr[j];

arr[j]=temp;

}
cout<<" your sorted array is : ";

for(int i=0;i<n;i++){

cout<< arr[i]<<" ";

cout<<" three biggest numbers from the array are";

for(int i=n-1;i>n-4;i--){

cout<<arr[i]<<" ";

2.to find n largest numbers

#include<iostream>

using namespace std;

int main(){

int n;

cout<< "enter no of elements of array";

cin>> n;

int arr[n];

cout<< " enter elements";

for(int i=0 ; i<n;i++){

cin>> arr[i];

}
for(int i=0;i<n-1;i++){

for ( int j=i+1;j<n;j++){

if(arr[j]< arr[i]){

int temp=arr[i];

arr[i]=arr[j];

arr[j]=temp;

int k;

cout<< "enter how many largest numbers do you want find";

cin>> k;

cout<< " your "<<k<<" largest numbers from the array are";

for (int i=n-1;i>n-k-1;i--){

cout<<arr[i]<<" ";

3.to seperate even and odd numbers from an aaarray

#include<iostream>

using namespace std;

int main(){

int n;

cout<< "enter no of elements of array";

cin>> n;

int arr[n];
cout<< " enter elements";

for(int i=0 ; i<n;i++){

cin>> arr[i];

int evencount=0,oddcount=0;

for(int i=0;i<n;i++){

if (arr[i]%2==0){

evencount++;

else{

oddcount++;

int even[evencount],odd[oddcount];

int evenindex=0,oddindex=0;

for ( int i=0;i<n;i++){

if(arr[i]%2==0){

even[evenindex]=arr[i];

evenindex++;

else{

odd[oddindex]=arr[i];

oddindex++;

}
cout<<" numbers you have entered : ";

for (int i =0;i<n;i++){

cout<< arr[i]<<" ";

}cout<<endl;

cout<< " even numbers : ";

for(int i =0; i<evencount; i++){

cout<<even[i]<<" ";

}cout<<endl;

cout << " odd numbers : ";

for(int i =0;i<oddcount;i++){

cout<< odd [i]<< " ";

ON FUNCTIONS
COUNTING NO OF VOWELS IN AN STRING
#include <iostream>

#include <string>

using namespace std;

bool isovwel( char c){

c=tolower(c);

return c== 'a'|| c=='e'|| c=='i'|| c=='o'|| c=='u';

int noofvowels(string s){

int count =0;


for(char c : s){

if (isovwel(c)){

count++;

return count;

int main(){

string s;

cout<<"enter a stringv : ";

getline(cin,s);

cout<<" no of ovwels in the are ";

cout << noofvowels(s);

factorial of a number
#include <iostream>

using namespace std;

int fact(int n){

if( n>0){

return n*fact(n-1);

else{

return 1;

}
int main(){

int n ;

cout << " enter a number ";

cin >> n;

cout << " factorial of "<< n << "is : "<< fact(n);

reversing a string
#include <iostream>

#include <string>

using namespace std;

void reverse(string s){

for(int i=0;i<s.length();i++){ for(int i=s.length()-1;i>=0;i--){

cout<<s[s.length()-i-1]; cout<<arr[i];

int main(){

string s;

cout<< " enter a strilng : ";

getline(cin, s);

reverse(s);

}
optional((( #include <iostream>

#include <string>

using namespace std;

void reverse(char* arr,string s){

cout<< " reversed string is : ";

for(int i=s.length()-1;i>=0;i--){

cout<<arr[i];

void stringtoarr(string s){

char* arr=new char[s.length()+1];

for ( int i=0;i<s.length();i++){

arr[i]=s[i];

cout << " entered string is : "<<arr<<endl;

reverse(arr,s);

int main(){

string s;

cout<< " enter a strilng : ";

getline(cin, s);

stringtoarr(s);
} ))))

palindrome
#include <iostream>

#include <string>

using namespace std;

void ispalindrome(string s){

int count;

for(int i =0;i<s.length();i++){

if(s[i]==s[s.length()-i-1]){

count=0;

else{

count=1;

if (count==0){

cout<<" its a palindrome";

else{

cout << " its not";

int main(){

string s;

cout<< " enter a strilng : ";


getline(cin, s);

ispalindrome(s);

is prime or not
// Online C++ compiler to run C++ program online

#include <iostream>

using namespace std;

bool isPrime(int number){

if (number==1){

return false;

if(number==2 || number==3){

return true;

if(number>3){

for( int i =2;i<number;i++){

if(number%i==0){

return false;

return true;

int main() {

int number;
cout << "Enter a number: ";

cin >> number;

if (isPrime(number)) {

cout << number << " is a prime number." << endl;

} else {

cout << number << " is not a prime number." << endl;

return 0;

classes
Create a base class Vehicle with private member variables for
make, model, and year.

Provide a constructor, getter and setter methods, and a


display method

#include <iostream>

#include<string>

using namespace std;

class vehicle{

private :

int year;

string model;

string make;
public:

vehicle(int y , string m , string mo){

year = y;

model = mo;

make =m;

void setyear(int y){

year =y;

void setmake(string m){

make =m;

void setmodel( string mo){

model = mo;

int getyear(){

return year;

string getmake(){

return make;

string getmodel(){

return model;

};

int main(){

vehicle one(2022, "Toyota", "Corolla");


cout << "Vehicle Info:" << endl;

cout << "Year: " << one.getyear() << endl;

cout << "Make: " << one.getmake() << endl;

cout << "Model: " << one.getmodel() << endl;

You might also like