Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

Programming question: outline and sample

C. (C++ coding): Some problems to consider along with


pool of year questions and class lecture:

i)Comparing two integers and displaying the larger


one. (concepts: basic input output, conditional)

Solution

#include <iostream>
using namespace std;
int main() {
cout<<"It is the programme to find out the larger number
between two number.";
int a,b;
cout<<"\n\nInput a=";
cin>>a;
cout<<"\nInput b=";
cin>>b;
if ((a=b)) {
cout<<"\nThe two integers are equal.";
}
if (a>b) {
cout<<"\n"<<b<<"is larger";
}
else
cout<<"\n"<<a<<" is larger";
return 0;
}

ii) comparing three integers and displaying the


largest. (concepts: basic input output, conditional)

(2014)
Solution
#include <iostream>
using namespace std;

int main()
{
float n1, n2, n3;
cout << "Enter three numbers: \n";
cin >> n1 >> n2 >> n3;

if(n1 >= n2 && n1 >= n3)


{
cout << "Largest number: " << n1;
}

if(n2 >= n1 && n2 >= n3)


{
cout << "Largest number: " << n2;
}

if(n3 >= n1 && n3 >= n2) {


cout << "Largest number: " << n3;
}

return 0;
}

iii) evaluating and displaying the roots of the


quadratic equation, 𝒂𝒙𝟐 + 𝒃𝒙 + 𝒄 = 𝟎. (concepts: basic
input output, conditional)

Solution

#include <iostream>
#include <cmath>
using namespace std;
int main() {
float a, b, c, x1, x2, discriminant, realPart,
imaginaryPart;
cout << "Enter coefficients a, b and c: \n";
cin >> a >> b >> c;
discriminant = b*b - 4*a*c;
if (discriminant > 0) {
x1 = (-b + sqrt(discriminant)) / (2*a);
x2 = (-b - sqrt(discriminant)) / (2*a);
cout << "Roots are real and different." << endl;
cout << "x1 = " << x1 << endl;
cout << "x2 = " << x2 << endl;
}
else if (discriminant == 0) {
cout << "Roots are real and same." << endl;
x1 = (-b + sqrt(discriminant)) / (2*a);
cout << "x1 = x2 =" << x1 << endl;
}
else {
realPart = -b/(2*a);
imaginaryPart =sqrt(-discriminant)/(2*a);
cout << "Roots are complex and different." << endl;
cout << "x1 = " << realPart << "+" << imaginaryPart <<
"i" << endl;
cout << "x2 = " << realPart << "-" << imaginaryPart <<
"i" << endl;
}
return 0;
}

iv) evaluating and displaying sum of the series 1 + 2


+ 3 +.........+n, where n is any positive integer.
(concepts: basic input output, looping)

Solution

#include <iostream>
using namespace std;

int main()
{
cout<<"Sum of series 1+2+3+.....+n,where n is any positive
integer.\n";
int n, sum = 0;
cout << "Enter a positive integer: ";
cin >> n;
for (int i = 1; i <= n; ++i) {
sum += i;
}
cout << "Sum = " << sum;
return 0;
}
v) supplying the elements of an array of four
elements, evaluating the sum of the elements;
displaying the array and the sum. (concepts: basic
input output, looping, array)

Solution

#include <iostream>
using namespace std;
int main()
{
int arr[4];
int sum=0;
cout << "Please enter 4 integers:\n\n"; for(int i = 0;
i < 4; i++)
{
cout<<"\tarr["<<i<<"] = ";
cin >> arr[i];
cout<<endl;}
cout << "\n\nValues in array are now:";
for(int i = 0; i < 4; i++)
cout << " " << arr[i];
cout << endl;
for(int i=0; i<4; ++i)
sum+=arr[i];
cout<<"\n\nSum of the elements is = "<<sum<<endl;
return 0;
}
vi) supplying elements to a 2 x 3 matrix, displaying
the matrix and its transpose. (concepts: basic input
output, looping, array)

Solution

#include <iostream>
using namespace std;

int main()
{int arr[4];
int sum=0;
cout << "Please enter 4 integers:\n\n";
for(int i = 0; i < 4; i++)
{
cout<<"\tarr[“<<i<<“] = ";
cin >> arr[i];
cout<<endl;}
cout << "\n\nValues in array are now:";
for(int i=0; i< 4; i++)
cout << " " << arr[i];
cout << endl;
for(int i=0; i<4; ++i)
sum+=arr[i];
cout<<"\n\nSum of the elements is = "<<sum<<endl;
return 0;
}
vii) Write code in c plus plus to count 1 to 10 and
print:
1,2,3,4,5,6,7,8,9,10. Thank You. (2016)

Solution
#include <iostream>
using namespace std;
int main()
{
cout<<"This is the programme to count 1 to 10.";
int num;
num=1;

while (num<=10)
{
cout<<num<<"\t";
num++;
}
cout<<"Thank You";

return 0;
}

Viii) In C++:
(2016)
i. evaluates whether the following identifiers are
valid or not.
double,
4_x,
variable1,
My.Variable
ii. compare the expressions:
x and 'x'
iii. compare the statements:
int a;
int a = 5;
const int a = 5;
iv.compare the statements:
cout<<"Hello";
cout <<Hello;

Solution

i.
double: Here double is a fundamental data type numeric
variable. So double is not valid.

4_x: The name of identifier cannot begin with a digit. So 4_x


is not valid.

variable1: Only alphabetic characters, digits and


underscore (_) are permitted in C++ language for declaring
identifier. Here the identifier is variable1. So it is valid.

My.Variable: Special characters like dot(.) are not allowed


for naming a variable / identifier. So My.Variable is not
valid.

ii.
x: Here x is integer type variable.
‘x’: Here x is character type variable.

iii.
int a: Here a is a changeable variable. The value of a isn't
declaring yet. We can declare a’s value later.

int a=5: Here a=5 is a changeable variable. We can change a’s


value later.

const int a = 5: Here a=5 is a constant variable. The


constant value can’t be changed.

iv.
cout << "Hello"; // prints Hello
cout << Hello; //prints the content of variable Hello
ix) Write code in any high level language for the
determination of area of a circle. (2013)

Solution

#include<iostream>
using namespace std;

int main()
{
float r,area;
cout<< "\nEnter radius of circle : ";

cin>>r;
area = 3.14*r*r;

cout<<"Area of circle : "<<area;

return 0;
}
x) Write a programming code in C or C++ for conversion of
temperature in Fahrenheit scale from Celsius scale
and vice versa. (2012)
Solution
#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
char more, equ;
cout<<"\n\n\t Convertion of temperature between celsius and
farenheit.\n\n\t\t";
float C,F; //indicating possition.
Level1:
cout<<" For converting celsius to farenheit
press'c''C'n\n\t for converting farenheit to celsius press
'f''F'.\n\t\n";
cin>>equ;

if(equ=='c'|| equ=='C' ||equ=='f' || equ=='F')


{
if(equ=='c'|| equ=='C')
{
cout<<"\n\n\t Put C=";
cin>>C;
F=(9*C)/5+32;
cout<<"\n\nThe value in farenheit is F="<<F<<"degree\n\t";
}
if (equ=='f' || equ=='F')
{
cout<<"\n\n\t Put F=";
cin>>F;
C=5*(F-32)/9;
cout<<"\n\n\t The value in celsius is C="<<C<<"degree.";
}
}
else
{
cout<<"\n\n you pressed a wrong keyword. please press the
correct one.";

} //calculating more.
cout<<"\n\n\tIf you want to calculate more press 'y'
otherwise press any key to exit.\n\t";
cin>>more;
if(more=='y'|| more=='Y')
goto Level1;
cout<<"\n\n\t Thank you for using this programme.";
return 0;
}

You might also like