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

Lab 4

The document contains 12 programming questions related to loops and conditional statements in C++. It includes questions on printing names and numbers, finding odd/even numbers, calculating sums and averages, finding factors and primes, Fibonacci series, factorials, and Armstrong numbers.

Uploaded by

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

Lab 4

The document contains 12 programming questions related to loops and conditional statements in C++. It includes questions on printing names and numbers, finding odd/even numbers, calculating sums and averages, finding factors and primes, Fibonacci series, factorials, and Armstrong numbers.

Uploaded by

shazil.xia094
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Question 1:

Write a program to print your name 20 times on screen.


#include <iostream>

usikng namespace std;

int main() {

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

cout<<"Fatima";

return 0;

Question no 2:
Write a program to print numbers from 1 to 10
#include <iostream>

using namespace std;

int main() {

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

cout<<i<<endl;

return 0;

Question no 3:
Write a program to print odd numbers from 1 to 20
#include <iostream>

using namespace std;

int main() {
for(int i=1; i<=20; i++){

if(i%2==1){

cout<<i<<endl;

return 0;

Question no 4:
Write a program to print even numbers from 50 to 70
#include <iostream>

using namespace std;

int main() {

for(int i=50; i<=70; i++){

if(i%2==0){

cout<<i<<endl;

return 0;

Question no 5:
Ask user to enter 10 numbers. Print the sum and average of the entered numbers.
#include <iostream>

using namespace std;

int main() {

float n, sum=0, average;


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

cout<<"Enter your "<<i<<" number"<<endl;

cin>>n;

sum=sum+n;

average=sum/10;

cout<<"The sum of the numbers is "<<sum<<endl;

cout<<"The average of the ten numbers is"<<average;

return 0;

Question no 6:
Enter a number and display its divisors. (e.g., divisors of 15 are: 1,3,5,15)

#include <iostream>

using namespace std;

int main() {

int n;

cout<<"Enter you number"<<endl;

cin>>n;

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

if(n%i==0){

cout<<i<<endl;

return 0;

}
Question no 7:
Enter a number and show its factors upto a specific number. (factors of 3 are : 3,6,9,12 …. )

#include <iostream>

using namespace std;

int main() {

int n;

cout<<"Enter you number"<<endl;

cin>>n;

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

n=n+3;

cout<<n<<endl;

return 0;

Question no 8:
Write a program to calculate and print the sum of all multiples of 7 from 1 to 100.

#include <iostream>

using namespace std;

int main() {

int sum=0;

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

if(i%7==0){

sum=sum+i;
}

cout<<"Sum of all the multiples of 7 from 1 to 100 is: "<<sum;

return 0;

Question no 9:
Two numbers are entered through the keyboard. Write a program to find the value of one
number raised to the power of another.

#include <iostream>

using namespace std;

int main() {

int n1, n2, product=1;

cout<<"Enter your first number"<<endl;

cin>>n1;

cout<<"Enter your second number"<<endl;

cin>>n2;

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

product=product*n2;

cout<<"The first number raised to the power of another is: "<<product;

return 0;

Question no 10:
Write a program to enter a value and calculate its factorial. (e.g., 5! = 5*4*3*2*1)
#include <iostream>

using namespace std;

int main() {

int n, product=1;

cout<<"Enter your first number"<<endl;

cin>>n;

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

product=product*i;

cout<<product<<endl;

return 0;

Question no 11:
Enter a number and tell whether it is prime or not.

#include <iostream>

using namespace std;

int main() {

int n, count=0;

cout<<"Enter the number"<<endl;

cin>>n;

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

if(n%i==0){

count++;
}

if(count==0){

cout<<"Prime Number";

else{

cout<<"Not a Prime Number";

return 0;

Question no 12:
Write a program to print Fibonacci series (1 1 2 3 5 7 12 19 31 ……)

#include <iostream>

using namespace std;

int main() {

int a=1, b=1, sum=0;

cout<<a<<endl<<b<<endl;

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

sum=a+b;

a=b;

b=sum;

cout<<sum<<endl;

return 0;

}
Question 13:
Enter a 3 digit number and find whether its Armstrong number or not? If sum of cubes of digits
of three-digit number is equal to the number itself, then the number is called an Armstrong
number. For example, 153 = ( 1 * 1 * 1 ) + ( 5 * 5
*5)+(3*3*3)

#include <iostream>

using namespace std;

int main() {

int n, r, sum=0, cube;

cout<<"Enter the number"<<endl;

cin>>n;

for(int i=20; i>0; i--){

r=n%10;

cube=r*r*r;

sum=sum+cube;

n=n/10;

cout<<sum;

return 0;

Question no 16:
Write a program to print out all Armstrong numbers between 100 and 500.

1.
#include <iostream>
using namespace std;

int main() {

int n, r, sum, cube;

for(int i=100; i<=500; i++){

sum=0;

n=i;

while(n>0){

r=n%10;

cube=r*r*r;

sum=sum+cube;

n=n/10;

if(sum==i){

cout<<i<<endl;

return 0;

2.
#include <iostream>

using namespace std;

int main() {

int n, r, sum, cube;

for(int i=100; i<=500; i++){

sum=0;

n=i;

for(int j=1000; j>0; j--){

r=n%10;
cube=r*r*r;

sum=sum+cube;

n=n/10;

if(sum==i){

cout<<i<<endl;

return 0;

You might also like