0% found this document useful (0 votes)
45 views5 pages

Lab04 (For)

The document contains 6 programming exercises involving loops in C++. 1) Write a program to find the sum of all even numbers between 1 to n using a for loop. 2) Write a program to find all factors of a natural number using a for loop. 3) Write a program to calculate power of a number using a for loop.

Uploaded by

Samer Sweileh
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)
45 views5 pages

Lab04 (For)

The document contains 6 programming exercises involving loops in C++. 1) Write a program to find the sum of all even numbers between 1 to n using a for loop. 2) Write a program to find all factors of a natural number using a for loop. 3) Write a program to calculate power of a number using a for loop.

Uploaded by

Samer Sweileh
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/ 5

Loop Programming Exercises

(Lab 04)

1. Write a C++ program to find sum of all even numbers between 1 to n.


#include <iostream>
using namespace std;
int main()
{
int i, num, sum=0;
cout<<"Enter any number: "<<endl;
cin>>num;

for(i=2; i<=num; i+=2)


{
sum+=i;
}
cout<<"Sum of all even number between 1 to " << num << ": "<<sum;
return 0;
}
2. Write a C++ program to find all factors of a natural number.
#include <iostream>
using namespace std;
int main() {
int n, i;
cout << "Enter a positive integer: ";
cin >> n;

cout << "Factors of " << n << " are: ";


for(i = 1; i <= n; i++) {
if(n % i == 0)
cout << i << " ";
}
return 0;
}
3. Write a C++ program to find power of a number using for loop.
#include <iostream>
using namespace std;
int main()
{
int base, exponent, result=1;
cout <<"Input the base: ";
cin >>base;
cout <<"Input the exponent: ";
cin>>exponent;

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


{
result*=base;
}
cout <<base<<" ^ "<<exponent<<" = "<<result<<endl ;
}

4. Write a C ++ program to check whether a number


is perfect number or not.
#include<iostream>
using namespace std;
int main ()
{
int num, div, sum=0;
cout << "Enter the number to be checked : ";
cin >> num;
for (int i=1; i < num; i++)
{ perfect number, a positive
div = num % i; integer that is equal to the
if (div == 0) sum of its proper divisors.
sum = sum + i;
} ‫ عدد صحيح موجب يساوي‬، ‫رقم كامل‬
if (sum == num)
‫مجموع قسومه الصحيحة‬.
cout << "\n" << num <<" is a perfect number.";
else
cout << "\n" << num <<" is not a perfect number.";
return 0;
}
5. Write a C++ program to print multiplication table of any number.
#include <iostream>
using namespace std;
int main()
{
int n;
cout<<"Enter the number you want to produce a multiplication
table:";
cin>>n;

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


cout << n << " * " << i << " = " << n * i << endl;
}
return 0;
}
6. Write a C++ programs to print the following shapes.
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *

#include <iostream>
using namespace std;

int main(){
for (int i=0;i<5;i++)
{
for (int j=0;j<5;j++)
{
cout<<"*";
}
cout<<endl;
}
return 0;
}

* * * * *
* * * * * #include <iostream>
* * * * * using namespace std;
* * * * *
* * * * * int main(){
for (int i=1;i<=5;i++)
{
for(int s=5;s>=1+i;s--)
{
cout<<" ";
}
for(int j=1;j<=5;j++)
{
cout<<"*";
}
cout<<endl;
}
return 0;
}

* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
#include <iostream>
using namespace std;

int main(){
for (int i=1;i<=5;i++)
{
for(int s=1;s<=1+i;s++)
{
cout<<" ";
}
for(int j=1;j<=5;j++)
{
cout<<"*";
}
cout<<endl;
}
return 0;
}

*
* *
* * *
* * * *
* * * * *
#include <iostream>
using namespace std;

int main(){
for (int i=1;i<=5;i++)
{
for(int s=5;s>5-i;s--)
{
cout<<"*";
}
for(int j=1;j<=4;j++)
{
cout<<" ";
}
cout<<endl;
}
return 0;
}

* * * * *
* * * *
* * *
* *
*
#include <iostream>
using namespace std;

void main(){
for (int i=1;i<=5;i++)
{
for(int z=1;z<=i-1;z++)
{
cout<<" ";
}

for(int s=1;s<=6-i;s++)
{
cout<<"*";
}

cout<<endl;
}

*
* * *
* * * * *
* * * * * * *
* * * * * * * * *

#include <iostream>
using namespace std;

int main(){
for (int i=0;i<5;i++){
for (int s=0;s<=4-i;s++)
{
cout<<" ";
}
for(int j=0;j<(2*i+1);j++)
{
cout<<"*";
}

cout<<endl;
}
return 0;
}

You might also like