Professional Documents
Culture Documents
Demata J Activity 6
Demata J Activity 6
OF SOUTHERN PHILIPPINES
Alubijid | Cagayan de Oro | Claveria | Jasaan | Oroquieta | Panaon
Section: IT1R13
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:
UNIVERSITY OF SCIENCE AND TECHNOLOGY
OF SOUTHERN PHILIPPINES
Alubijid | Cagayan de Oro | Claveria | Jasaan | Oroquieta | Panaon
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 minimum range: ";
cin>>min;
cout<<"Enter 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:
UNIVERSITY OF SCIENCE AND TECHNOLOGY
OF SOUTHERN PHILIPPINES
Alubijid | Cagayan de Oro | Claveria | Jasaan | Oroquieta | Panaon
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:
UNIVERSITY OF SCIENCE AND TECHNOLOGY
OF SOUTHERN PHILIPPINES
Alubijid | Cagayan de Oro | Claveria | Jasaan | Oroquieta | Panaon
int number;
cout<<"Number of times: "<<endl;
cin>>number;
for (i=0; i<number; i++)
cout<<"Hello world"<<endl;
Output:
UNIVERSITY OF SCIENCE AND TECHNOLOGY
OF SOUTHERN PHILIPPINES
Alubijid | Cagayan de Oro | Claveria | Jasaan | Oroquieta | Panaon
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 first number: ";
cin >> fir;
cout << "Enter last number: ";
cin >> las;
cout << "The sum of the numbers from " << fir << " to " << las << " is " << (las + fir) *
(las - fir + 1) / 2;
return 0;
}
Output:
UNIVERSITY OF SCIENCE AND TECHNOLOGY
OF SOUTHERN PHILIPPINES
Alubijid | Cagayan de Oro | Claveria | Jasaan | Oroquieta | Panaon
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: