C Revision
C Revision
C Revision
int f = 1;
f = f * i;
return f;
// (with recursion)
int factorial(int n)
if (n == 1)
return 1;
int main()
int a, b, c, sum;
cout << "enter a ,b and c\n";
// another soution
// another solution
/*
arr1++;
arr2++;
arr12++;*/
int main()
read(a);
read(b);
read(c);
add(a, b, ab);
add(b, c, bc);
add(a, c, ac);
print(ab);
print(bc);
struct student
int id;
float score;
char grade;
};
int main()
student st[30];
for(int i=0;i<=29;i++)
cin>>st[i].id>>st[i].score>>st[i].grade;
for(int i=0;i<=29;i++)
if(st[i].score>=50)
cout<<st[i].id<<endl;
}
}
#include <iostream>
int main(void)
int a, b, c;
if (a<b)
myswap(&a, &b);
if (a<c)
myswap(&a, &c);
if (b<c)
myswap(&b, &c);
return 0;
int temp;
temp = *num1;
*num1 = *num2;
*num2 = temp;
#include <iostream>
struct person
int id;
int salary;
char gender;
};
int main()
int age[10];
cin >> per[i].id >> per[i].year >> per[i].month >> per[i].day >> per
[i].salary >> per[i].gender;
temp = per[k];
per[k] = per[i];
per[i] = temp;
cout << " Person " << i + 1 << endl<< per[i].id << endl << per[i].year
<< endl << per[i].month << endl << per[i].day << endl << per
[i].salary
return 0;
int main()
int a;
cin >> a;
if (prime(a) == 0)
else
return 0;
int prime(int x)
int flag = 0;
if (x%i == 0)
flag = 1;
return flag;
int main()
cout << "loop : " << lop << "\t" << "recursion : " << rec;
cin >> h;
return 0;
if (m == 1)
return n;
else
int p = 1;
p = p * x; // p = 2 * 2 * 2
}
return p;
#include <iostream>
//using loop
void fib();
int main() {
fib();
return 0;
// 0,1,1,2,3,5,8,...
void fib() {
int i, a = 0, b = 1, c;
c = a + b;
a = b;
b = c;
//using array
void fib();
int main() {
fib();
return 0;
void fib() {
int a[20], k;
a[0] = 0;
a[1] = 1;
//using recursion
int fib(int);
int main() {
int i;
return 0;
int fib(int n)
if (n == 0)
return 0;
else if (n == 1)
return 1;
else
9. Define a structure has id (int ), dept (int), salary (float) . then write
five functions Read function to read id, dept, salary of 10 employees
using array of structure. Function to find the id of employee that
have the highest salary. Function to find the average salary of all
employees. Function to print counts of the employees who takes
salary greater than average & who takes salary less than average
#include <iostream>
struct employees
int id;
int dept;
float salary;
};
void read()
cout << "Enter id, dept, salary of employee number " << i + 1 << " : ";
}
}
void highest()
highs = emp[i].salary;
highid = i;
float avg()
float sum = 0;
float avg = 0;
sum += emp[i].salary;
return avg;
void counter()
{
counterhigher++;
else
counterlower++;
cout << "The number of employees takes higher than average are : " <<
counterhigher
<< " and lower than average are : " << counterlower;
int main()
read();
highest();
avg();
counter();
return 0;
}
10.
Did your program compile? If so, what does it print? If not, what error message do you get?
11.
Did your program compile? If it does, what does the program output? If not, what error message
does it produce? Will run and get A
12. Write a program to add two matrices which are entered by user
and print the result and matrix. (Note: each matrix has dimensions
3x5)
Solution:
#include <iostream>
using namespace std;
int main()
{
int arr1[3][5];
int arr2[3][5];
cout << "plz enter the first matrix: ";
for (int i = 0; i < 3; i++)
{
cout << "enter the 5 elements for row no " << i << " : \n";
for (int j = 0; j < 5; j++)
{
cout << "element in the column no " << j << " : \n";
cin >> arr1[i][j];
}
}
int main()
{int a, b;
cout<<"enter value of a and b";
cin>>a>>b;
int res=Sum(a,b);
cout<<"res="<<res;
return 0;
}
14. Consider the following C++ code. What is preventing it from compiling?
struct Employee {
int id;
float wage;
}
15. Given the following C++ structs, how would you set the value of the id in the Union
struct to 35?
struct Union {
int id;};
struct Employee {
Union aflcio;
long emp_id;
float rate;
};
#include <iostream>
using namespace std;
int main() {
char *ptr,*ptr2,*ptr3;
char Str[] = "Have a nice day";
ptr = Str;
ptr += 7;
cout << ptr<<endl;
ptr2 = ptr+1;
cout << ptr2;
return 0;
}
Answer:
nice day
ice day
18. Write a program to read the string from the user and print its length, then
compare it with text “SoftwareTestingHelp”, then add .com to the text
“SoftwareTestingHelp”, finally print length of text as shown in the following
scenario:
#include <iostream>
#include <string>
int main() {
string str;
getline(cin, str);
cout << "Length of the string str is: " << str.length() << endl;
if (str == target) {
else {
// Append ".com" to target string and print the new string and its length
cout << "New str1 after adding .com: " << str1 << endl;
cout << "str1 new length: " << str1.length() << endl;
return 0;
19. Write a C++ program that will prompt the user to input n
integer values. The program will display the smallest and greatest of
those values.
20.Write a program to call a function that print the reverse order of n numbers
using pointer.
#include <iostream>
#include <conio.h>
21. Tracing the following code and choose the right answer.
1. int a;
int* p;
a = 2;
p = &a;
a = a + 1;
cout << *p;
a) 2 b) 3 c) Won't run
2. int a;
int* p;
a = 2;
p = a;
a = a + 2;
cout << *p;
a) 2 b) 4 c) Won't run
3. int a;
int b;
int* p;
p = &a;
*p = 4;
p = &b;
*p = 3;
cout << a << “ “ << b;
a) 4 3 b) 3 3 c) Won't run
4. int a;
int b;
int* p;
int* q;
a = 3;
p = &a;
q = p;
*q = *q + 5;
cout << *p;
a) 8 b) 3 c) Won't run
5. int a;
int* p;
a = 4;
p = &a;
cout << (*p) / a;
a) 1 b) 4 c) Won't run
6. ref(int* p)
{(*p) = (*p) * 2;}
int main()
{
int a = 5;
ref(&a);
cout << a;
}
a) 5 b) 10 c) Won't work
7. int a;
int b;
int* p;
int* q;
a = 3;
p = &a;
q = p;
b = 4;
*q = b;
cout << *p << a;
a) 4 3 b) 3 4 c) 4 4
8. int a;
int* p;
a = 3;
p = &a;
cout << p;
a) 3 3 b) A memory address c) Won’t run
1. string* x, y;
a) int *ip;
b) string s, *sp = 0;
c) int i; double* dp = &i;
d) int *pi = 0;
ans: c)
a) b is assigned to a
b) p now points to b
c) a is assigned to b
d) q now points to a
ans: b
Exam Solution:
Solution:
C++ Good
Wood
Solution : 7
#include <iostream>
ptr++;
ptr += 2;
return 0;
Solution :
Line 1: 10
Line 2: 20
Line 3: 40
#include <iostream>
struct Student {
string name;
int age;
};
int main() {
return 0;
Solution:
Line 1: Alice
Line 2: 20
#include <iostream>
int main() {
int a = 5;
if (*ptr > 3) {
} else {
return 0;
Solution:
#include <iostream>
struct Book {
string title;
int pages;
};
int main() {
return 0;
Solution:
g. An array in C++ must have all elements of the same data type. True
h. The while loop and do-while loop always execute the loop body at least once. False
int main() {
int N;
cin >> N;
add(A, B, C, N);
cout << "C[" << i << "] = " << C[i] << endl;
delete[] A;
delete[] B;
delete[] C;
return 0;
}
#include <iostream>
if (num <= 1) {
return false;
if (num % i == 0) {
return false;
return true;
int main() {
int n;
cin >> n;
// Check if the number is prime
if (isPrime(n)) {
} else {
return 0;
#include <iostream>
int temp = a;
a = b;
b = temp;
if (a > b) {
swap(a, b);
if (a > c) {
swap(a, c);
if (b > c) {
swap(b, c);
int main() {
int x, y, z;
sortThree(x, y, z);
cout << "Sorted order: " << x << " " << y << " " << z << endl;
return 0;