Professional Documents
Culture Documents
OF SOUTHERN PHILIPPINES
Alubijid | Cagayan de Oro | Claveria | Jasaan | Oroquieta | Panaon
1. Write a program in C++ to find the sum of first 10 numbers using for loop.
SOURCE CODE:
#include<iostream>
using namespace std;
int main ()
{
int j, sum = 0;
cout<<"The First 10 Numbers are:\n";
j=1;
while(j<=10)
{
sum = sum + j;
cout<<" "<<j;
j++;
}
cout<<"\nThe Sum is :"<< sum;
}
OUTPUT:
2. Write a program in C++ to find the sum of numbers in the given range by the user.
SOURCE CODE:
#include<iostream>
using namespace std;
int main(){
int min,max,sum=0;
cout<<"\nEnter the minimun range: ";
cin>>min;
cout<<"\nEnter The Maximum Range: ";
cin>>max;
for(int i=min;i<=max;i++){
sum=sum+i;
}
cout<<"\nThe Sum of Natural Numbers from "<<min<<"to"<<max<<"is"<<sum;
cout<<endl;
return 0;
}
OUTPUT:
3. Write a program in C++ to display the series of number 1-50 in reverse using the for
loop.
SOURCE CODE:
#include<iostream>
#include<conio.h>
int main()
{
int i;
for(i=50;i>=1;i--)
{
printf("\t%d",i);
}
getch();
}
OUTPUT:
4. Write a program in C++ to display “HELLO WORLD” repeatedly depending on the
number given by the user using the do-while loop. Sample scenario, if the user input 5,
“HELLO WORLD” will be displayed 5 times. So it should be like this:
HELLO WORLD
HELLO WORLD
HELLO WORLD
HELLO WORLD
HELLO WORLD
SOURCE CODE:
#include<iostream>
using namespace std;
int main() {
int i;
int number;
cout<<"Number of Times;"<<endl;
cin>>number;
for(i=0;i<number;i++)
cout<<"HELLO WORLD"<<endl;
}
OUTPUT:
5. Write a program in C++ where it will get the total of all the numbers between 1 – n. The
value of n will be given by the user.
SOURCE CODE:
#include<iostream>
int main()
{
int fir,las;
cout<<"Enter The First Number:";
cin>>fir;
cout<<"Enter The Last Number:";
cin>>las;
cout<<"The Sum of the Numbers from"<<fir<<"to"<<las<<"is"<<(las+fir)*
(las-fir + 1)/ 2;
return 0;
OUTPUT:
6. Write a number to display the numbers in reverse from the range given by the user. Like
if user gives 1 and 10, your programs should display 10, 9, 8,…..1
SOURCE CODE:
#include<iostream>
using namespace std;
int main()
{
int number,reversedNumber = 0;
cout<< "number:";
cin>>number;
while(number!=0){
reversedNumber *= 10;
reversedNumber += number % 10;
number/=10;
}
cout<<"Reversed:"<< reversedNumber;
system("pause>0");
}
OUTPUT: