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

Assignment Solutions - Variables and Data Types

This document contains 6 coding questions and their solutions in C++. The questions cover basic C++ concepts like variables, data types, input/output, arithmetic operations, functions. The questions demonstrate how to take user input, perform calculations, find data type sizes, swap variables. The full code solutions are provided for each question.

Uploaded by

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

Assignment Solutions - Variables and Data Types

This document contains 6 coding questions and their solutions in C++. The questions cover basic C++ concepts like variables, data types, input/output, arithmetic operations, functions. The questions demonstrate how to take user input, perform calculations, find data type sizes, swap variables. The full code solutions are provided for each question.

Uploaded by

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

C++

Variables and Data Types

Assignment Solutions
Assignment Solutions

Q1 - Take 2 integer values in two variables x and y and print their product.

#include <iostream>

using namespace std;

int main()

int x = 23;

int y = 50;

cout << x*y << endl;

return 0;

Q2 - Print the ASCII value of character ‘U’.

#include <iostream>

using namespace std;

int main()

cout << "ASCII value of u is " << (int)'U' << endl;

return 0;

Cracking the Coding Interview in C++ - Foundation


Assignment Solutions

Q3 - Write a C++ program to take length and breadth of a rectangle and print its area.

#include <iostream>

using namespace std;

int main()

int length=10;

int breadth=20;

cout <<"Lenght "<< length << endl;

cout<< "Breadth" << breadth << endl;

int area = length*breadth;

cout << "Area is " << area << endl;

return 0;

Cracking the Coding Interview in C++ - Foundation


Assignment Solutions

Q4 - Write a C++ program to calculate the cube of a number.

#include <iostream>

using namespace std;

int main()

int x = 2;

int cube;

cout << "number is "<< x<<endl ;

cout <<"cube of the number is "<< x*x*x << endl;

return 0;

Cracking the Coding Interview in C++ - Foundation


Assignment Solutions

Q5 - Write a C++ program to find size of basic data types.

#include <iostream>

using namespace std;

int main()

cout << "\nSize of fundamental data types :\n";

cout << " The sizeof(char) : " << sizeof(char) << " bytes \n" ;

cout << " The sizeof(short) : " << sizeof(short) << " bytes \n" ;

cout << " The sizeof(int) : " << sizeof(int) << " bytes \n" ;

cout << " The sizeof(long): " << sizeof(long) << " bytes \n" ;

cout << " The sizeof(long long) :" << sizeof(long long) << " bytes \n";

cout << " The sizeof(float) :" << sizeof(float) << " bytes \n" ;

cout << " The sizeof(double) :" << sizeof(double) << " bytes \n";

cout << " The sizeof(long double) :" << sizeof(long double) << " bytes \n";

cout << " The sizeof(bool) : " << sizeof(bool) << " bytes \n\n";

return 0;

Cracking the Coding Interview in C++ - Foundation


Assignment Solutions

Q6 - Write a C++ program to swap two numbers with the help of a third variable.

#include <iostream>

using namespace std;

int main()

cout << "\n\n Swap two numbers :\n";

cout << "-----------------------\n";

int n1 = 2, n2 = 5, temp;

cout << " Before swapping the 1st number is : "<< n1 <<"\n" ;

cout << " Before swapping the 2nd number is : "<< n2 <<"\n" ;

temp=n2;

n2=n1;

n1=temp;

cout << " After swapping the 1st number is : "<< n1 <<"\n" ;

cout << " After swapping the 2nd number is : "<< n2 <<"\n" ;

Cracking the Coding Interview in C++ - Foundation

You might also like