0% found this document useful (0 votes)
28 views13 pages

C++ Programming Exercises Solutions

Uploaded by

myr7hcxt5g
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views13 pages

C++ Programming Exercises Solutions

Uploaded by

myr7hcxt5g
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

ASSIGNMENT NO.

04

Submitted by:
Ahmad Sameet Bin Manzar

Roll No:
SU92-BIOTM-S24-015

PROGRAMMING FUNDAMENTALS

THE SUPERIOR UNIVERSITY, LAHORE


Department Of Information & Technology
Section: BSIOT-1A

Submitted to:
Sir Nadeem Sarfraz

MAY 7, 2024
1. Write a C++ program that uses a for loop to print the squares of the first 10 positive integers.
#include <iostream>
using namespace std;
int main()
{
int a;
cout<<"quare of the first 10 positvie inttegers: "<<endl;
for(a=1; a <= 10 ; ++a)
{
cout<<" saquare of " <<a<< " is "<<a*a<<endl;
}
return 0;
}
2. Create a C++ program that calculates the average of a set of numbers entered by the user. The
program should first ask the user how many numbers will be entered, then use a for loop to input
those numbers and calculate their average.
#include <iostream>
using namespace std;
int main()
{
int numbercount;
double sum =0.0;
cout<<"enter the number of value ";
cin>>numbercount;
for(int i=0; i<numbercount;++i)
{
double num;
cout<<"enter number " <<i+1<< ": ";
cin>>num;
sum+=num;
}
double average =sum/ numbercount;
cout<<"the average of the entered number is : "<<average<<endl;
return 0;

}
3. Write a program to display the multiplication table up to 10 for a given number.
#include<iostream>
using namespace std;
int main ()
{
int number;
cout<<"enter number : "<<endl;
cin>>number;

for(int i=1;i<=10;++i)
{cout<<number<< " * "<< i <<" = " << (number*i) <<endl;
}
return 0;
}
4. Write a program that Calculate the factorial of a given number N.
#include<iostream>
using namespace std;
int main()
{
int num;
int factorial=1;

cout<<"enter number for factorial : "<<endl;


cin>>num;

for(int i= 1 ; i <=num; ++i )

{ factorial *=i ;

}
cout<<"factorial of = " << num<< " is " <<factorial <<endl;

return 0;

5. Write a C++ program to determine whether a given number is a prime number or not.

#include<iostream>

using namespace std;

int main()

int num;

char press;

do{

bool isprime = true ;

cout<<"enter number : ";

cin>>num;
if(num<=1){

cout<<num<<" nmber is not prime ";

isprime =false;

return 0;

}else{

for(int a=2;a*a<=num;++a){

if(num%a==0)

isprime=false;

break;

if(isprime){

cout<<num<<" is prime"<<endl;

}else{

cout<<num<<" is not prime"<<endl;

cout<<"press Y TO DO IT AGAIN"<<endl;

cin>>press;

} while(press=='y'||press=='Y');

return 0;

6. Write a program that counts the number of even numbers between 1 and
100.
#include<iostream>
using namespace std;
int main()
{
int cnt=0;
for(int i=1;i<=100; i+=2)
{
cnt ++;
cout<<" = "<<i<<endl;

}
cout<<"the even number between 1 to 100 is : " <<cnt;
return 0;
}

7. Write a program that prompts the user to input an integer and then
outputs both the individual digits of the number and the sum of the
digits. For example, it should output the individual digits of 3456 as 3 4 5
6, output the individual digits of 8030 as 8 0 3 0, output the individual
digits of 2345526 as 2 3 4 5 5 2 6, output the individual digits of 4000 as 4
0 0 0, and output the individual digits of -2345 as 2 3 4 5.
#include<iostream>
using namespace std;
int main()
{
int num;
cout<<"enter number : ";
cin>>num;
if(num<0){
num=-num;
}
int digit;
int sum=0;
cout<<"indiv digit";
while(num>0){
digit=num%10;
cout<<digit<< " ";
sum+=digit;
num/=10;
}
cout<< "sum of digit "<<sum;
return 0;

8. Write a program that prompts the user to enter two integers. The program outputs how many numbers
are multiples of 3 and how many numbers are multiples of 5 between the two integers.

#include<iostream>

using namespace std;

int main()

{ int num1,num2;

int count5=0;

int count3=0;

cout<<"enter number ";

cin>>num1;
cout<<" enter number ";

cin>>num2;

for(int a=num1; a<=num2; ++a){

if(a%3==0)

count3++;

if(a%5==0)

count5++;

cout<<"multiples of "<<num1<<" and "<<num1<<" is "<<count3<<endl;

cout<<"multiples of "<<num2<<" and "<<num2<<" is "<<count5<<endl;

return 0;

[Link] a program that prompts the user to input a positive integer. It should
then output a message indicating whether the number is a prime number.
(Note: An even number is prime if it is 2. An odd integer is prime if it is not
divisible by any odd integer less than or equal to the square root of the
number.)

#include<iostream>

#include<cmath>

using namespace std;

int main()

int number;

cout<<"enter positive number : ";

cin>>number;

if(number<=1){

cout<<number<<" is not prime ";

return 0;

}else if(number==2){

cout<<number<<" number is prime";


return 0;

else {

for(int num=3;num<=sqrt(number);num +=2){

if (number%num==0){

cout<<number<<" is no0t a prime";

return 0;

cout<<number <<" bumber is a prime number ";

return 0;

10. Write a program that uses while loops to perform the following steps: a. Prompt the user to input two
integers: firstNum and secondNum (firstNum must be less than secondNum). b. Output all odd numbers
between firstNum and secondNum. c. Output the sum of all even numbers between firstNum and
secondNum.

#include<iostream>

using namespace std;

int main()

int firstnum,secondnum;

cout<<"enter first number : ";

cin>>firstnum;

cout<<"enter second number greater then first : ";

cin>>secondnum;
if(firstnum>=secondnum){

cout<<"error enter second number greater then first "<<endl;

return 1;

int current;

int sumeven=0;

cout<<"odd number between : "<<firstnum<<" o r " <<secondnum<<" are "<<endl ;

current =firstnum+(!(firstnum%2));

while (current<=secondnum){

cout<<current<< " "<<endl;

current+=2;

cout<<"sum of even number between"<<firstnum<< " o r "<<secondnum<< " is "<<endl;

current = firstnum + (!(firstnum % 2));

while(current<=secondnum){

sumeven+=current;

current+=2;

cout<< sumeven<<endl;

return 0;

11. Redo Programming Exercise 8 using for loops.

#include<iostream>

using namespace std;

int main()

int num1,num2;

int count3=0;

int count5=0;

char press;
cout<<"enter number";

cin>>num1;

cout<<"enter number";

cin>>num2;

for(int a=num1;a<=num2;++a){

if(a%3==0)

count3++;

if(a%5==0)

count5++;

cout<<"multiples of 3 between "<<num1<<" and "<<num2<<" is "<<count3<<endl;

cout<<"multiples of 5 between "<<num1<<" and "<<num2<<" is "<<count5<<endl;

return 0;

12. Redo Programming Exercise 8 using do...while loops.

#include<iostream>

using namespace std;

int main()

int num1,num2;

int count3=0;

int count5=0;

char press;

do{

cout<<"enter number";

cin>>num1;

cout<<"enter number";
cin>>num2;

for(int a=num1;a<=num2;++a){

if(a%3==0)

count3++;

if(a%5==0)

count5++;

cout<<"multiples of 3 between "<<num1<<" and "<<num2<<" is "<<count3<<endl;

cout<<"multiples of 5 between "<<num1<<" and "<<num2<<" is "<<count5<<endl;

cout<<"press Y to continue";

cin>>press;

}while(press='y'||press=='Y');

return 0;

13. Write a program that print Fibonacci sequence .The Fibonacci sequence is a series where the next
term is the sum of the previous two terms.

#include<iostream>

using namespace std;

int main()

int num;

cout<<"enter number : ";

cin>>num;

int first =0;

int second =1;

int nextnum;

cout<<"fibonocci series";
for(int a=1; a<= num;++a){

cout<<first<<" ";

nextnum=first+second;

first=second;

second = nextnum;

return 0;

14. Write a program to print the multiplication table of the number entered by the user. The table should
get displayed in the following form. 29 * 1 = 29 29 * 2 = 58

#include<iostream>

using namespace std;

int main ()

int num;

cout<<"enter number : ";

cin>>num;

for(int a=1;a<=10; ++a){

cout<<num<<" * "<<a<<" = "<<num*a<<endl;

return 0;

15. Write a program to produce the following output:

#include <iostream>

using namespace std;

int main() {

int n;
cout << "Enter the number of rows: ";

cin >> n;

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

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

cout << " ";

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

cout << j << " ";

for (int j = i - 1; j >= 1; --j) {

cout << j << " ";

cout << endl;

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

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

cout << " ";

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

cout << j << " ";

for (int j = i - 1; j >= 1; --j) {


cout << j << " ";

cout << endl;

return 0;

16. Write a program to produce the following output:

Common questions

Powered by AI

The program uses a for loop to iterate from the smaller of the two integers to the larger. Within the loop, it checks each integer for divisibility by 3 and 5 using the modulus operator. It maintains separate counters for each case, incrementing them as it finds multiples. This simple check ensures that all numbers in the given interval are assessed .

The program uses a while loop to repeatedly extract the last digit of the number using the modulus operator. It prints this digit and accumulates it into a sum variable. The number is then divided by ten to remove the last digit, and the process continues until the number is reduced to zero. This logical structure effectively handles both the digit extraction and summation .

The program incorrectly identifies odd numbers instead of counting even numbers. The loop increments by two, starting at one, effectively counting odd numbers. To correctly count even numbers, the loop should start at 2 and also increment by two, or iterate over all numbers between 1 and 100, checking for evenness using the modulus operator .

The program first checks if the number is less than or equal to 1, which means it is not prime. For numbers greater than 1, it assumes the number is prime. It then iterates from 2 to the square root of the number, checking divisibility. If the number is divisible by any integer in this range, it is not prime. Otherwise, it remains prime .

The C++ program uses a for loop to iterate through the integers 1 to 10. For each integer 'a' in this range, it calculates the square by multiplying 'a' by itself and then prints the result. This loop continues until all ten integers are processed .

The program initializes the first two numbers in the Fibonacci sequence, 0 and 1. Then, it enters a loop where it calculates the next number as the sum of the previous two numbers, updates the variables representing the last two numbers in the series, and outputs the sequence iteratively for a fixed count specified by the user .

The program employs two nested for loops to print a symmetrical pattern of numbers. The outer loop moves through each row, while the inner loops manage spaces and increasing/decreasing numbers. As a result, it constructs a centered triangle of numbers, followed by an inverted version, thus creating the symmetrical effect .

The program first prompts the user to enter the number of values they will input. It reads each of those numbers inside a for loop, summing them up. After collecting all numbers, it divides the total sum by the number of entries to calculate the average. The consideration here is ensuring the correct amount of numbers is provided according to the initial input, as failure to enter precisely the specified amount will lead to incorrect calculations .

The program prompts the user to input two integers, with the first integer supposed to be less than the second. It checks this condition and outputs an error message if the condition is not met, effectively terminating the process. This ensures that all further calculations occur only with valid input, preventing logical errors in the loop-based calculations .

The program handles negative integers by converting them to positive using 'num = -num' if the input is less than zero. This approach ensures that the digit extraction and summation logic can proceed without additional conditional checks due to the uniform handling of positive numbers. However, this modification may not be appropriate if the sum should consider negative signs .

You might also like