C++ exercises and solutions programming


C++ exercises

C++ exercises will help you test your knowledge and skill of programming in C++ and practice the C++ programming language concepts. You will start from basic C++ exercises to more complex exercises. The solution is provided for each exercise. You should try to solve each problem by yourself first before you check the solution. To navigate from one page of exercises to another, you will use the right navigation bar. If you have any questions regarding to each problem, you can post them at our forum.

Exercise 1: Write a C++ program to print the following lines:

You are 10 years old.
You are too young to play the game.

Solution:


#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
    int age;
    age=10;
    cout<<" You are "<<age<<" years old.\n";
    cout<<" You are too young to play the game.\n";
    system("PAUSE");
    return EXIT_SUCCESS;
}

Exercise 2: Write five C++ statements to print the asterisk pattern as shown below.


*****
*****
*****
*****
*****

Solution:
#include <cstdlib>
#include <iostream>
using namespace std;
 
int main(int argc, char *argv[])
{
    cout<<"*****\n";
    cout<<"*****\n";
    cout<<"*****\n";
    cout<<"*****\n";
    cout<<"*****\n";
    system("PAUSE");
    return EXIT_SUCCESS;
}

Exercise 3: Write a C++ program to declare two integer , one float variables and assign 10, 15, and 12.6 to them respectively. It then prints these values on the screen.

Solution:


#include <cstdlib>
#include <iostream>
 
using namespace std;
 
int main(int argc, char *argv[])
{
    int x;
    int y;
    float z;
    x=10;
    y=15;
    z=12.6;
    cout<<"x="<<x <<"\t"<<"y= "<<y<<"\t"<<"z="<<z;
    cout<<"\n";
       
    system("PAUSE");
    return EXIT_SUCCESS;
}

Exercise 4: Write a C++ program to prompt the user to input her/his name and print this name on the screen, as shown below. The text from keyboard can be read by using cin>> and to display the text on the screen you can use cout<<.

Hello Sok! .

Solution:


#include <cstdlib>
#include <iostream>
 
using namespace std;
 
int main(int argc, char *argv[])
{
    char name[20];
    cout<<"Enter your name:";
    cin>>name;
    cout<<"Hello "<<name<<"! \n";
       
    system("PAUSE");
    return EXIT_SUCCESS;
}

Exercise 5: Write a C++ program to prompt the user to input 3 integer values and print these values in forward and reversed order, as shown below.


 
Please enter your 3 numbers: 12 45 78
Your numbers forward:
12
45
78
 
Your numbers reversed:
78
45
12

Solution:
 
#include <cstdlib>
#include <iostream>
 
using namespace std;
 
int main(int argc, char *argv[])
{
    int val1;
    int val2;
    int val3;
    cout<<"Please enter your 3 numbers:";
    cin>>val1>>val2>>val3;
   
    cout<<"\nYour numbers forward:\n";
    cout<<val1<< "\n"<<val2<<"\n" <<val3<<"\n";
    cout<< "Your numbers reversed:\n";
    cout<<val3<<"\n"<<val2 <<"\n"<<val1<<"\n";
    system("PAUSE");
    return EXIT_SUCCESS;
}


Comments

mohammad comment

 mohammad

write a program to copy one structure variable to another variable and then to printit on screen with out using any build in function
how we solve


2017-05-16
berat comment

 berat

Can anyone help?
S= 1(3/2) + 2*(4/2) + 3(5/2) + 4(5/2) + 5(5/3) + 6(5/4) + ...
for (i=1; i<n; i++)
Thank you!


2017-04-27
td machir  comment

 td machir

tnxs


2017-04-12
Mahesh BudhaThoki comment

 Mahesh BudhaThoki

//Best way to reverse number in c++ is using array

#include <iostream>
#include <cstdlib>

using namespace std;

int main(){
int size , i;
int num[20];
cout<<"Enter the lenght of number !"<<endl;
cin>>size;
cout<<"Enter the "<<size <<" Element for array "<<endl;
for(i = 0; i<size;i++){
cin>>num[i];
}
cout<<endl<<endl;
cout<<"Inserted Elements in Array "<<endl<<endl;
for(i = 0; i<size;i++){
cout<<"In "<<i <<" position " <<num[i]<<endl;
}
cout<<endl<<endl;
cout<<"Elements in Reverse Order"<<endl;
for(i = size-1; i>=0;i--){
cout<<num[i]<<endl;
}

}




2017-03-19
Mahesh BudhaThoki comment

 Mahesh BudhaThoki

//Best way to reverse number in c++ is using array

#include <iostream>
#include <cstdlib>

using namespace std;

int main(){
int size , i;
int num[20];
cout<<"Enter the lenght of number !"<<endl;
cin>>size;
cout<<"Enter the "<<size <<" Element for array "<<endl;
for(i = 0; i<size;i++){
cin>>num[i];
}
cout<<endl<<endl;
cout<<"Inserted Elements in Array "<<endl<<endl;
for(i = 0; i<size;i++){
cout<<"In "<<i <<" position " <<num[i]<<endl;
}
cout<<endl<<endl;
cout<<"Elements in Reverse Order"<<endl;
for(i = size-1; i>=0;i--){
cout<<num[i]<<endl;
}

}




2017-03-19
MOHSIN HAYAT comment

 MOHSIN HAYAT

/*
Generic code for this type of output
1_______1
12_____21
123___321
1234_4321
123___321
12_____21
1_______1
*/

#include <iostream>
using namespace std;
void main() {
int n;
cout << "Enter a number = ";
cin >> n;
for (int rows = 1; rows < n*2; rows++)
{
if (rows <= n) {
for (int i = 1; i <= rows ; i++)
{
cout << i;
}
for (int dsh = (n*2) - rows*2; dsh>=0 ;dsh--)
{
cout << "_";
}
for (int i = rows; i > 0; i--)
{
cout << i;
}
}
else {
for (int i = 1; i <= n*2-rows; i++)
{
cout << i;
}
for (int dsh = 0; dsh <= rows * 2 - n*2; dsh++)
{
cout << "_";
}
for (int i = n * 2 - rows; i > 0; i--)
{
cout << i;
}
}
cout << endl;
}
}



2017-03-17
Sangram comment

 Sangram

there are m men and m women in adance group.Each man may either dance with one ormore than one women in a group or may not dance with any women in the group.Find one to one danceing partner matches for these group of men and women such that: 1 male dance partner is paired with women dancer partner of his choice. 2 :No male or female dancer left alone write a progamme using C++ language

give me a code


2017-03-09
prasad comment

 prasad

Dance Carnival
There are m men and m women in a dance group. Each man may either to dance with one or more than one women in a group or may not to dance with any women in the
group. Find one to one dancing partner matches for these group of men and women such that:
1. The male dance partner is paired with woman dancer of his choice.
2. No male or female dancer is left alone.
The output must return total number of matches possible . If the dance partner match is not possible then the output returned will be -1.
For example, consider the following figure:
In the figure, M1, M2, M3, M4 and M5 are man and W1, W2, W3, W4 and W5 are women. Lines between men and women indicate which particular man would to dance with
that particular woman e.g. M1 would to dance with W2 and W4.
The only valid dance partner match in this situation is M1 to W2, M2 to W1, M3 to W3, M4 to W5, M5 to W4. There is no other valid dance partner match. Hence, the output is
1.
Input Specification:
Input 1: An integer(m)
Input 2: Input should be {< name of 1st man >#< list of women whom he likes to dance with separated by # >, < name of 2nd man >#< list of women whom he likes to dance
with separated by # >, ............... ,< name of mth man >#< list of women whom he likes to dance with separated by # >}.
For the example considered in the question, input should be {M1#W2#W4,M2#W1#W2,M3#W1#W3#W4,M4#W4#W5,M5#W4}.
Output Specification:
Output should be total number of matches possible.
Or
return -1 if no match possible
Example 1:
Input 1: 5
Input 2: {M1#W2#W4,M2#W1#W2,M3#W1#W3#W4,M4#W4#W5,M5#W4}
Output: 1
Example 2:
Input 1: 5
Input 2: {M1#W2#W4,M2#W1#W2,M3#W1#W3#W4,M4#W4#W5,M5}
Output: -1
Explanation:
M5 would notto dance with anyone. So the output is -1.
Example 3:
Input 1: 5
QUESTIONS (HTTPS://WWW.TECHGIG.COM/CHALLENGE/QUESTION/HCL-THE-TECHWIZ-BLAST/NZK1NUAJJEAJJDE5MTUYODDAIYRAIYQZODC2MDE1QCMKQCMKMTQ4NZU5ODC2NUAJJEAJJDYYMDK1)
HCL - THE TECHWIZ BLAST
(hTttps://www.techgig.com) ime Remaining - 00:54:26 Questions Attempted - 25/26
2/20/2017 HCL ­ The Techwiz Blast|Techgig
https://www.techgig.com/challenge/question/hcl­the­techwiz­blast/Nzk1NUAjJEAjJDE5MTUyODdAIyRAIyQzODc2MDE1QCMkQCMkMTQ4NzU5ODc2NUA… 2/2
NORMAL Line: 8 Col: 1
Need help? SUBMIT CODE
Input 2: {M1#W4,M2#W1#W2,M3#W1#W3#W4,M4#W4#W5,M5#W4}
Output: -1
Explanation:
M1 and M5 wouldto dance with the same woman which is not valid. So the output is -1.
Example 4:
Input 1: 10
Input 2: {M1#W6#W10,M2#W1#W5,M3#W1#W3#W5#W9,M4#W3#W4,M5#W2#W6,M6#W1#W2#W6,M7#W1#W7#W8,M8#W8#W10,M9#W3#W9,M10#W10}
Output: 2

answer quickly


2017-03-06
mohit comment

 mohit

Dance Carnival
There are m men and m women in a dance group. Each man may either to dance with one or more than one women in a group or may not to dance with any women in the
group. Find one to one dancing partner matches for these group of men and women such that:
1. The male dance partner is paired with woman dancer of his choice.
2. No male or female dancer is left alone.
The output must return total number of matches possible . If the dance partner match is not possible then the output returned will be -1.
For example, consider the following figure:
In the figure, M1, M2, M3, M4 and M5 are man and W1, W2, W3, W4 and W5 are women. Lines between men and women indicate which particular man would to dance with
that particular woman e.g. M1 would to dance with W2 and W4.
The only valid dance partner match in this situation is M1 to W2, M2 to W1, M3 to W3, M4 to W5, M5 to W4. There is no other valid dance partner match. Hence, the output is
1.
Input Specification:
Input 1: An integer(m)
Input 2: Input should be {< name of 1st man >#< list of women whom he likes to dance with separated by # >, < name of 2nd man >#< list of women whom he likes to dance
with separated by # >, ............... ,< name of mth man >#< list of women whom he likes to dance with separated by # >}.
For the example considered in the question, input should be {M1#W2#W4,M2#W1#W2,M3#W1#W3#W4,M4#W4#W5,M5#W4}.
Output Specification:
Output should be total number of matches possible.
Or
return -1 if no match possible
Example 1:
Input 1: 5
Input 2: {M1#W2#W4,M2#W1#W2,M3#W1#W3#W4,M4#W4#W5,M5#W4}
Output: 1
Example 2:
Input 1: 5
Input 2: {M1#W2#W4,M2#W1#W2,M3#W1#W3#W4,M4#W4#W5,M5}
Output: -1
Explanation:
M5 would notto dance with anyone. So the output is -1.
Example 3:
Input 1: 5
QUESTIONS (HTTPS://WWW.TECHGIG.COM/CHALLENGE/QUESTION/HCL-THE-TECHWIZ-BLAST/NZK1NUAJJEAJJDE5MTUYODDAIYRAIYQZODC2MDE1QCMKQCMKMTQ4NZU5ODC2NUAJJEAJJDYYMDK1)
HCL - THE TECHWIZ BLAST
(hTttps://www.techgig.com) ime Remaining - 00:54:26 Questions Attempted - 25/26
2/20/2017 HCL ­ The Techwiz Blast|Techgig
https://www.techgig.com/challenge/question/hcl­the­techwiz­blast/Nzk1NUAjJEAjJDE5MTUyODdAIyRAIyQzODc2MDE1QCMkQCMkMTQ4NzU5ODc2NUA… 2/2
NORMAL Line: 8 Col: 1
Need help? SUBMIT CODE
Input 2: {M1#W4,M2#W1#W2,M3#W1#W3#W4,M4#W4#W5,M5#W4}
Output: -1
Explanation:
M1 and M5 wouldto dance with the same woman which is not valid. So the output is -1.
Example 4:
Input 1: 10
Input 2: {M1#W6#W10,M2#W1#W5,M3#W1#W3#W5#W9,M4#W3#W4,M5#W2#W6,M6#W1#W2#W6,M7#W1#W7#W8,M8#W8#W10,M9#W3#W9,M10#W10}
Output: 2


2017-02-20
jasmine comment

 jasmine

write a program that calculate the area between the circle and the outside edge of a square ?


2017-02-19
SudRad comment

 SudRad

/* Write a program that captures any numbers to print the positive,negative,odd and even numbers
and write the sum of the captured numbers.*/
#include <iostream>

using namespace std;

int main()
{
int even,odd,pos,neg;
double total;
cout<<"Input an even number"<<endl;
cin>>even;
if (even%2==0)
{
cout<<"Even Number is "<<even<<endl;
}
else
{
cout<<even<<" is not even"<<endl;
even=0;
}
cout<<"Input an odd number"<<endl;
cin>>odd;
if (odd%2!=0)
{
cout<<"Odd Number is "<<odd<<endl;
}
else
{
cout<<odd<<" is not odd"<<endl;
odd=0;
}
cout<<"Input positive number"<<endl;
cin>>pos;
if(pos>0)
{
cout<<"Positive Number is "<<pos<<endl;
}
else
{
cout<<pos<<" is not a positive number"<<endl;
pos=0;
}
cout<<"Input a negative number"<<endl;
cin>>neg;
if(neg<0)
{
cout<<"Negative number is "<<neg<<endl;
}
else
{
cout<<neg<<" is not a negative number"<<endl;
neg=0;
}
total=even+odd+pos+neg;
cout<<"Your addition is "<<total;
return 0;
}


2017-02-15
chai comment

 chai

can anyone help me ?? write a c++ program based on this
HAPPY Holiday Tour Agency will give a special rate for a holiday in Langkawi Island, Pangkor Island,
and Tioman Island during the school holiday. Full cost for 3 days for an adult are as follows:
Destination Flight(2 ways ) Hotel(3 days)
Langkawi RM 120.00 RM 260.00
Pangkor RM 90.00 RM 330.00
Tioman RM 150.00 RM 390.00
For a family to go for a vacation, discount is given based on the following:
Bed Room ( Hotel )
1 adult - no discount given
2 adults - 20% discount each adult
3 or more adults - 30% discount each adult
children - 50% discount each children
Flight
No discount for adult
50% discount for each child
Design the above problem using the problem solving tools mentioned below - that will report the total
cost needed by a family to go for this holiday package. Assume that your inputs are destination, total
adults, and total children.


2017-02-13
chai comment

 chai

can anyone help me ?? write a c++ program based on this
HAPPY Holiday Tour Agency will give a special rate for a holiday in Langkawi Island, Pangkor Island,
and Tioman Island during the school holiday. Full cost for 3 days for an adult are as follows:
Destination Flight(2 ways ) Hotel(3 days)
Langkawi RM 120.00 RM 260.00
Pangkor RM 90.00 RM 330.00
Tioman RM 150.00 RM 390.00
For a family to go for a vacation, discount is given based on the following:
Bed Room ( Hotel )
1 adult - no discount given
2 adults - 20% discount each adult
3 or more adults - 30% discount each adult
children - 50% discount each children
Flight
No discount for adult
50% discount for each child
Design the above problem using the problem solving tools mentioned below - that will report the total
cost needed by a family to go for this holiday package. Assume that your inputs are destination, total
adults, and total children.


2017-02-13
chai comment

 chai

can anyone help me ?? write a c++ program based on this
HAPPY Holiday Tour Agency will give a special rate for a holiday in Langkawi Island, Pangkor Island,
and Tioman Island during the school holiday. Full cost for 3 days for an adult are as follows:
Destination Flight(2 ways ) Hotel(3 days)
Langkawi RM 120.00 RM 260.00
Pangkor RM 90.00 RM 330.00
Tioman RM 150.00 RM 390.00
For a family to go for a vacation, discount is given based on the following:
Bed Room ( Hotel )
1 adult - no discount given
2 adults - 20% discount each adult
3 or more adults - 30% discount each adult
children - 50% discount each children
Flight
No discount for adult
50% discount for each child
Design the above problem using the problem solving tools mentioned below - that will report the total
cost needed by a family to go for this holiday package. Assume that your inputs are destination, total
adults, and total children.


2017-02-13
Mehran comment

 Mehran

Can you help me, i want to get code of given program...

Write a C++ program to generate 20 random numbers in the range of -1 to 55 and store them in an array named arrayNums . Then have your program find the largest random number stored in the array.

please Reply as soon as possible


2017-02-12
Mehran comment

 Mehran

Can you help me, i want to get code of given program...

Write a C++ program to generate 20 random numbers in the range of -1 to 55 and store them in an array named arrayNums . Then have your program find the largest random number stored in the array.


2017-02-12
Mehran comment

 Mehran

Can you help me, i want to get code of given program...

Write a C++ program to generate 20 random numbers in the range of -1 to 55 and store them in an array named arrayNums . Then have your program find the largest random number stored in the array.


2017-02-12
Younas comment

 Younas

Can any person help me to solve below question:

write a C++ program to solve the below mathematical expression:
k!(n-1)!/n!(n-k)!

please reply me code c++

thanks.


2017-02-06
Younas comment

 Younas

Can any person help me to solve below question:

write a C++ program to solve the below mathematical expression:
k!(n-1)!/n!(n-k)!

please reply me code c++

thanks.


2017-02-06
imran israr comment

 imran israr

/* write a program that calculate any numbers to print the positive,negative,zero,odd and even numbers
and write the sum of all the numbers that you input .*/
#include <iostream>
using namespace std;
int main()
{
int even,odd,positive,negative;
int sum=0;
cout<<"please enter the even number:"<<"\t";
cin>>even;
if((even%2)==0)
{
cout<<"Even number:"<<"\t"<<even<<"\n";
}
else
{
cout<<"not even number:"<<"\t"<<even<<"\n";
}
cout<<"please enter the odd number:"<<"\t";
cin>>odd;
if((odd%2)!=0)
{
cout<<"odd number:"<<"\t"<<odd<<"\n";
}
else
{
cout<<"not odd number:"<<"\t"<<odd<<"\n";
}
cout<<"please enter the positive number:"<<"\t";
cin>>positive;
if(positive>=0)
{
cout<<"positive number:"<<"\t"<<positive<<"\n";
}
else
{
cout<<"not positive number:"<<"\t"<<positive<<"\n";
}
cout<<"please enter the negative number:"<<"\t";
cin>>negative;
if(negative<0)
{
cout<<"negative number:"<<"\t"<<negative<<"\n";
}
else
{
cout<<"not negative number:"<<"\t"<<negative<<"\n";
}
sum=even+odd+positive+negative;
cout<<"sum is:"<<"\t"<<sum<<"\n";
system("pause");
}


2017-01-19
imra comment

 imra

/*write a program that read the distance
between two cities in kilometers and change it into Meters,
Feet, Inches, Centimeters and Millimeters.*/
#include <iostream>
using namespace std;
int main()
{
double result;
char city_1[10],city_2[10],convert;
double e=2.71828;
double distance_1;
cout<<"please enter the name of first city:"<<"\t";
cin>>city_1;
cout<<"please enter the name of second city:"<<"\t";
cin>>city_2;
cout<<"please enter the distance of city_1(in kilometer):"<<"\t";
cin>>distance_1;
cout<<"Distance are converted into meter pressed for(M) ,inches for(i),centimeter for(c),millimeter(m) :"<<"\t";
cin>>convert;
if(convert=='M')
{
result=distance_1*1000;
cout<<"The result in Meter:"<<"\t"<<result<<"\n";
}
else if(convert=='i')
{
result=distance_1*39370.1;
cout<<"The result in inches:"<<"\t"<<result<<"\n";
}
else if(convert=='c')
{
result=distance_1*100000;
cout<<"The result in centimeter:"<<"\t"<<result<<"\n";
}
else if(convert=='m')
{
result=distance_1*((1*e)+6);
cout<<"The result in milimeter:"<<"\t"<<result<<"\n";
}
system("pause");
}


2017-01-18
imran israr comment

 imran israr

//C&M Airlines (Charlie and Mable) operates a small commuter airline with a single plane that seats 12 passengers.
// The plane makes one slight per day. Write a seating reservation program as described.
//Each structure holds the seat ID, a marker that indicates if the seat has been assigned,
//a passenger first name and a passenger last name.
//The program repeatedly displays the following menu until Quit is selected.
//Show list of empty seats .
//Assign a seat.
//Delee a seat assignment.
#include <iostream>
using namespace std;
int main()
{
int seat_pass,tic;
char name[10],f_name[10],male[10],female[10],gen;
cout<<"\t"<<"\t"<<"\t"<<"\t"<<"\t"<<"\t"<<"C&M Airlines:"<<"\n";
cout<<"\t"<<"\t"<<"\t"<<"\t"<<"\t"<<"\t"<<"Seat Reservation:"<<"\n";
cout<<"please enter your first name:"<<"\t"<<"\n";
cin>>name;
cout<<"Name :"<<name<<"\t"<<"\n";
cout<<"please enter your last name:"<<"\t"<<"\n";
cin>>f_name;
cout<<"Last Name :"<<f_name<<"\t"<<"\n";
cout<<"please enter your Gender, if Gender is male type(m) whereas type(w):"<<"\t"<<"\n";
cin>>gen;
if(gen=='m')
{
cout<<"your Gender is Male:"<<"\n";
}
if(gen=='w')
{
cout<<"your Gemder is Female:"<<"\n";
}
cout<<"wellcom to C&M Airline:"<<"\t"<<name<<"\t"<<f_name<<"\n";
cout<<"How much seats are booked:"<<"\t"<<"\n";
cin>>tic;
cout<<name<<"\t"<<"your seats are confirmed are successfully:"<<"\n";
cout<<"Seats ID:"<<"\t"<<"0007"<<"\n";
cout<<"\t"<<"\t"<<"0008"<<"\n";
cout<<"\t"<<"\t"<<"0009"<<"\n";
cout<<"\t"<<"\t"<<"0010"<<"\n";
cout<<"Departure Lahore to Islamabad:"<<"\t"<<"20-1-2017 at 12:00 PM:"<<"\n";
cout<<"I Hope your journey will be good and you will come next again:"<<"\n";
system("pause");
}


2017-01-15
Sheikh FAIZAN comment

 Sheikh FAIZAN

cout is not coming please anyone see this problem



#include <iostream>
#include <conio.h>

using namespace std;

int sum(int,int);
double sum(int,double);
double sum(double,int);
double sum(double,double);

int main ()

{
double f,s,sum,values;

cout << "enter first number" << endl;

cin >> f;

cout << "enter second number" << endl;

cin >> s;

cout << values;

getch();
return 0;
}

int sum(int a,int b)

{

int result= a+b;

return result;
}

double sum(double a,int b)

{

double result= a+b;

return result;
}

double sum(int a,double b)

{

double result= a+b;

return result;
}

double sum(double a,double b)

{

double result= a+b;

return result;
}


2016-12-15
humaira comment

 humaira

#include <iostream>

using namespace std;

class a
{
private:
int iprA;
protected:
int iprta;

public:
int ia;
a()
{
iprA=iprta=ia=0;
}

};


class b: private a //b inherits a privately
{
private:

protected:
int iprtb;
public:
int ib;
b()
{
iprtb=ib=0;
}

};

class c
{
private:
int iprC;
protected:
int iprtc;

public:
int ic;
c()
{
iprC=iprtc=ic=0;
}

};


class d: protected c //d inherits c protectedly
{
private:

protected:
int iprtd;
public:
int id;
d()
{
iprtd=id=0;
}

};

class e
{
private:
int iprE;
protected:
int iprte;

public:
int ie;
e()
{
iprE=iprte=ie=0;
}

};


class f: public e //f inherits e publicly
{
private:

protected:
int iprtf;
public:
int fi;
f()
{
iprtf=fi=0;
}

};



int main()
{ a a1;
a1.ia=90;
//a1.iprta=70;

b b1;
b1.ib=100;
b1.set_ia_from_b(9);
b1.set_iprta_from_b(10);
b1.set_iprA_from_b(112);
/*
first set and then get the values of the following variables:
i) iprA from object of class b
ii) iprC from object of class d
iii) iprE from object of class e
implement appropriate functions to do the above
*/
return 0;
}


2016-12-04
Rizwan comment

 Rizwan

/*Here Farooq
For output
1
234
56789*/

#include "stdafx.h"
#include<iostream>
using namespace std;

int main()
{
int k = 1;
for (int i = 1;i <= 5;i+=2)
{
for (int j = 1;j <= i;j++)
{
cout << k;

k++;
}
cout << endl;
}
return 0;
}


2016-12-02
hayat g comment

 hayat g

it is very helpful to make my assignment.it is very helpful for every student.


2016-11-20
farook comment

 farook

who can solve the following :

1
2 3 4
5 6 7 8 9


Best Regards


2016-11-07
Zaman ALi comment

 Zaman ALi

To be a programmer. Practice is very compulsory.


2016-10-17
Aashick comment

 Aashick

I have certain questions I need answers for that.how would I find


2016-10-12
Romy comment

 Romy

Hello,

This was very helpful. I have learned a lot with these exercises. From these basic activities, I managed to do a research and further understand much more complicated activities.

This really helped me to learn from basics to advanced c++


2016-10-07
D comment

 D

Can anyone solve this???????
1_______1
12_____21
123___321
1234_4321
123___321
12_____21
1_______1


2016-10-04
D comment

 D

Can anyone solve this???????
1_______1
12_____21
123___321
1234_4321
123___321
12_____21
1_______1


2016-10-04
Meera comment

 Meera

Find the c++ code for the question "there are m men and m women in a dance group.Each men may either to dance with one or more than one womenin the group or may not dance with any women in the group.Find one to one dancing partner matches for these men and womensuch that the male dancer is paired withwomen dancer of his choice.No male or female dancer is left alone."
Find the required c++ code?


2016-10-01
mohit comment

 mohit

Please help me, I am stuck up.

Using C++
C&M Airlines (Charlie and Mable) operates a small commuter airline with a single plane that seats 12 passengers. The plane makes one slight per day. Write a seating reservation program as described.
The passenger information is stored in an array of 12 structures. This array should be maintained in the main() function.
Each structure holds the seat ID, a marker that indicates if the seat has been assigned, a passenger first name and a passenger last name. The program repeatedly displays the following menu until Quit is selected.
Choose a function: a.) Show number of emptyseats b.) Show list of empty seats c.) Assign a seat
d.) Delee a seat assignment
e.) Quit
Choices.c.) and d.) will require additional input from the user; Passenger name for both, and seat
number forc.) Checks should be made so that a passenger can't be assigned to a seat that is already taken.
A separate functionshould be provided to accomplish each of the choices a.), b.), c.), and d.)


2016-08-29
HITLER comment

 HITLER

SO EASY!NO DIFFICULT?


2016-08-07
zaki comment

 zaki

Hi, my name is zaki, i need someone who is able to help me


2016-05-05
Vishul Patel comment

 Vishul Patel

I now work at Kwik e Mart, but I will do master programmer someday with your kind help. I will remember you when I do great things with programming.


2016-04-09
Prahdeep comment

 Prahdeep

I thank you greatly for very kind examples to help me. I will use this experience to advance my programming knowledge and someday make good video games.


2016-04-09
Calib comment

 Calib

This is good for me. I much thank you for C++ work. I very excite to use my skills in village.


2016-04-09
sonsun comment

 sonsun

those examples very, but for us .


2016-03-30
Nigger comment

 Nigger

Hey pussies


2016-03-11
Taimoor Salahuddin comment

 Taimoor Salahuddin

It's not easy but practice makes a man perfact...


2016-03-08
Taimoor Salahuddin comment

 Taimoor Salahuddin

It's not easy but practice makes a man perfact...


2016-03-08
priti jadhav comment

 priti jadhav

The Arca Carania Mountain national park is opening up for tourist trafï¬


2016-01-27
priti jadhav comment

 priti jadhav

The Arca Carania Mountain national park is opening up for tourist trafï¬


2016-01-27
laalaa comment

 laalaa

why programming so difficult for me. this is not fair.


2015-12-15
Josman comment

 Josman

Pls any suggestion on this:
You are required to develop a multi-tasking/multi-threaded C/C++ program that simulates a control system for a robotic assembly line.

The robotic cell has 4 lines: line A, line B, line C, and line D. Objects come from line A (the producer) in random colours of red, green and blue. The robot task (the consumer) is to separate objects by their colour placing red objects into line B, green objects into line C and blue objects into line D. Lines B, C and D may run at a slower speed than line A.

Your simulation will need to create separate threads or processes for each of the four lines. You will also require a separate I/O process that accepts inputs from the PC keyboard and provide a display for the control system by accessing and displaying the contents of lines B, C, and D. The keys of the PC keyboard will be used to simulate commands and are made up of a two character key sequence. The first key always identifies the line, while the second key, a letter indicating the command itself. These commands are outlined below:
â


2015-12-08
Hani comment

 Hani

please help me in my assignment.
Q#Write a c++ program that initially takes an integer value from the user as the loopâ


2015-11-29
Hani comment

 Hani

please help me in my assignment.
Q#Write a c++ program that initially takes an integer value from the user as the loopâ


2015-11-29
Emanman Edward comment

 Emanman Edward

someone help me solve this!email: emanmanedward@gmail.com
1. Taach Hotel offers accommodation services to customers for fees. There are two types of accommodation services namely, SUP and DOW which cost Sh.2000 and Sh.1200 per day respectively. A customer who goes for accommodation for at least 3 days but not exceeding 5 days gets a discount of 20% on accommodation cost. For accommodation of at least 5 days, a customer gets discount of 25% on accommodation cost. There is no discount for accommodation of less than 3 days unless athe customer goes for SUP accommodation service in which case the discount would be 10%. Write a C++ program to input for each customer, name, type of accommodation paid for, and number of days required of accommodation, and to determine and output for each customer, accommodation charge, discount (if any) and net charge. Output should also include total (accumulated) net charges. Net charge is charge less discount. The program should work for any number of customers, and its execution should stop when either â


2015-11-27
sana comment

 sana

help me in a C++ program please.
write a program that read the distance between two cities in kilometers and change it into Meters, Feet, Inches, Centimeters and Millimeters.


2015-11-04
arunabh comment

 arunabh

Plz help to do a table program.........


2015-10-30
archie bui  comment

 archie bui

PLS! help me to do this exercise...
Exercise:
An iron manufacturing company maintains data about all of its employees including employee name, id, age, address and salary. The company wants to sort the addresses of the employees so as to easily trace how many employees belong to same region. Write a C program module using strcmp which sorts all of the available addresses of employees in ascending order.


2015-09-23
yyah comment

 yyah

Please help me for my project in c++ this is the given output:
For ex:
Item: 100
Rawscore:70

2.2
PASSED

Compute Show table
Clear Exit

If i enter show table the given output
Must be :
Item. rating
10. 1.0
to to
1 5.0

Thankyou


2015-08-13
kavi comment

 kavi

what is the solution of given problem????????????????
Problem : Reverse Gear

A futuristic company is building an autonomous car. The scientists at the company are training the car to perform Reverse parking. To park, the car needs to be able to move in backward as well as forward direction. The car is programmed to move backwards B meters and forwards again, say F meters, in a straight line. The car does this repeatedly until it is able to park or collides with other objects. The car covers 1 meter in T units of time. There is a wall after distance D from car's initial position in the backward direction.

The car is currently not without defects and hence often hits the wall. The scientists are devising a strategy to prevent this from happening. Your task is to help the scientists by providing them with exact information on amount of time available before the car hits the wall.

Input Format:

First line contains total number of test cases, denoted by N
Next N lines, contain a tuple containing 4 values delimited by space
F B T D, where

F denotes forward displacement in meters
B denotes backward displacement in meters
T denotes time taken to cover 1 meter
D denotes distance from Car's starting position and the wall in backward direction


Output Format:

For each test case print time taken by the Car to hit the wall

Constraints:

First move will always be in backward direction

1 <= N <= 100

backward displacement > forward displacement i.e. (B > F)

forward displacement (F) > 0

backward displacement (B) > 0

time (T) > 0

distance (D) > 0

All input values must be positive integers only

Sample Input and Output

SNo. Input Output
1
2
6 9 3 18
3 7 5 20


162
220



Note:

Please do not use package and namespace in your code. For object oriented languages your code should be written in one class.

Note:

Participants submitting solutions in C language should not use functions from <conio.h> / <process.h> as these files do not exist in gcc

Note:

For C and C++, return type of main() function should be int.


2015-08-07
BAD ASS comment

 BAD ASS

hmmmmm seems ok ok !!!!


2015-06-25
;yazan H comment

 ;yazan H

this is a hard exercise ,, write a program that calculate any numbers to print the positive,negative,zero,odd and even numbers and write the sum of all the numbers that you input / if you input (0.01) ,the program will stop.


2015-06-04
Engineer Reiz Troy comment

 Engineer Reiz Troy

For more C++ exercise, please visit : http://ccphplusplus.blogspot.com/


2015-05-20
Engineer Reiz Troy comment

 Engineer Reiz Troy

For more C++ exercise, please visit : http://ccphplusplus.blogspot.com/


2015-05-20
abdirahman mohamud abdulahi comment

 abdirahman mohamud abdulahi

first i'm still learning IT but this subject i like it and i hope to success..... thank you for all


2015-05-05
dine comment

 dine

http://acm.epoka.edu.al:8080/en/problem-pid-c50f?ps=1&smt=a&smpwid=0

can anyone help me with this exercise ??


2015-04-27
Diallo comment

 Diallo

hello guys im a beginer IT but i found the c++ is very hard to understand since i started i couldn't catch anything


2015-04-11
a comment

 a

#include<iostream>
#include<conio.h>
#include<iomanip>

using namespace std;

void printPascalTr(int size);
int getFirstDay(int year,int month);
int getDayCode(int year);
void printCar(int year,int month);
int main()

{

/* Carlenda writen by:

*/

int i, year;
char ch='y';
while(ch=='y'){
cout<<"Enter year of the carlenda:";//year input
cin>>year;
//print carlenda for the input year
for(i=1;i<=12;i++)
printCar(year,i);
cout<<"Continue? press y:";
cin>>ch;

}

getch();
return 0;

}

// calculate the first day of week for current month
int getFirstDay(int year,int month){
int fday=getDayCode(year);
int num_days=0; // number of days of the year before the current month
int dCode=getDayCode(year);
switch(month){
case 12: num_days+=30;
case 11: num_days+=31;
case 10: num_days+=30;
case 9: num_days+=31;
case 8: num_days+=31;
case 7: num_days+=30;
case 6: num_days+=31;
case 5: num_days+=30;
case 4: num_days+=31;
case 3: num_days+=28;
case 2: num_days+=31;

}

if(month==1) return(fday);else if((year%4==0) || (year%400==0) && month>2) return (num_days+1+dCode)%7 ;
// leap year

else return (num_days+dCode)%7;}

// calculate the first day of the year
int getDayCode(int year){
int fday;
fday=(((year-1)*365+(year-1)/4-(year-1)/100+(year-1)/400)+1)%7;

return fday;

}


void printCar(int year, int month){
int fday_month=getFirstDay(year,month);
int code[7]={0,1,2,3,4,5,6};
char *Day[7]={"Sun","Mon","Tues","Wed","Thu","Fri","Sat"};
char *Month[12]={"January","February","March","April","May","June","Junly","August","September","October","November","December"};

int num_days,i;

if(month==1||month==3||month==5||month==7|| month==8|| month==10|| month==12)
num_days=31;
else if(month==2)
{

; if((year%4==0) || (year%400==0)) num_days=29;else num_days=28;

}

else num_days=30;
cout<<"\n.............."<
cout<<"First day of the month:"<
cout<<"Sun\t"<<"Mon\t"<<"Tues\t"<<"Wed\t"<<"Thu\t"<<"Fri\t"<<"Sat"<
for(i=1;i<=fday_month;i++) cout<<"\t";
int d=1;
int con=0;
while(d<=num_days)
{
cout<<d<<"\t";
if(fday_month>0) {
if(d==7-fday_month) {cout<<"\n";con=d;}
else if(d%(con+7)==0) {cout<<"\n";con=d;}

}

else if(d%7==0) cout<<"\n";

d++;

}

cout<<"\n";
}




2015-03-24
Prince Dube comment

 Prince Dube

awesome exercises!!!! really helped me understanding what i was learning :)


2015-03-15
1234 comment

 1234

great :))


2015-03-10
1234 comment

 1234

thanks


2015-03-10
kadam suraj comment

 kadam suraj

cool!!!!


2015-01-27
Nigam Bhattarai comment

 Nigam Bhattarai

They are very Simple... i want prepare some difficult ones for my exam..


2015-01-19
Jazib Shahzad comment

 Jazib Shahzad

from Pakistan.Nice exercies keep it up..............


2015-01-12
BTH comment

 BTH

this is very basic , is there a next level ?


2014-11-27
shanto comment

 shanto

i want to goin


2014-11-19
Dev comment

 Dev

int a = 2, b = 5, c;
c = a + ... b;
c out << c;


2014-11-15
Lazarus Makwenda comment

 Lazarus Makwenda

This is so helpful...Appreciations


2014-11-12
mike ten comment

 mike ten

its very fine to me. Thanks a lot


2014-11-06
Slicen Randa  comment

 Slicen Randa

Please suggest some sites for testing my program's .......


2014-10-31
Slices Randall comment

 Slices Randall

Please get the program from the user and try to explain it. Thank you for your help.


2014-10-31
nouman comment

 nouman

My many points clear
thanks a lot


2014-10-28
nouman comment

 nouman

My many points clear
thanks a lot


2014-10-28
Benttini2 comment

 Benttini2

Thanks a million for make so clear to comprehend c++ programming language, specially to the newbies.


2014-10-07
abcd comment

 abcd

write a program to find the biggest prime number??? in 32 bit OS..?????/???.


2014-09-30
Md humayun kabir comment

 Md humayun kabir

It's good


2014-09-21
Akuete Sossavi comment

 Akuete Sossavi

This is great!!! It help me a lot to understand what I was doing and how many different way codes could be written. Great job!


2014-08-11
fadi comment

 fadi

nice


2014-08-04
MrNiceGuy comment

 MrNiceGuy

Nice, I want more exercises.

<script>alert("hello")</scirpt>


2014-08-01
Younis comment

 Younis

it's very helpful to me.Thank you for writing that for me.


2014-07-01
Gemechis bulti@amu comment

 Gemechis bulti@amu

It's very good exercise,so i like it so much......


2014-06-23
beekan boona comment

 beekan boona

Really your program is good it help me to develop my knowlege of c.


2014-05-20
imtiaz immy comment

 imtiaz immy

It is very easy for me to understand the basic of c


2014-05-15
akash kumar comment

 akash kumar

Nothing is impossible in c++


2014-04-30
viet comment

 viet

Thanks so much, really helpful for beginners.


2014-04-21
Abid Hussain comment

 Abid Hussain

Very nice Exercises. I like it.


2014-03-27
Usama Rehman comment

 Usama Rehman

It's easy to understand such kinds of logics.. nice.. :)


2014-02-07
Naol Getachew comment

 Naol Getachew

I appreciate you. Its best for me to understand about C++ more.


2013-09-04
sada comment

 sada

Thanks for C++ exercises!
It is useful for me.


2013-08-05
ly comment

 ly

Do you mean \t?
\t in programming language is special character that is equivalent to tab key press.


2012-12-25
Sharad Pradhan comment

 Sharad Pradhan

What does t mean in Exercise 1 of operator?


2012-12-25
CAPTCHA image



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.