You are on page 1of 4

programing fundamentals

ASSIGNMENT NO:02

SUBMITTED TO: Dr.Sohaib

SUBMITTED BY: Muhammad Haseeb

Class: BSCS012315078

- -
Question#01
Let’s say we want to find some of the first five 5 numbers 1,2,3,4,5
sum of these numbers is 1+2+3+4+5. Here I know how many times I
need to run the loops . yes, you guessed it right 5 times as we have 5
numbers. If we knowhow many times to execute then the best choice
is counter loops.
SOLUTION:
#include<iostream>
using namespace std;
int main(){
int a[5],i,sum=0;
for(i=0;i<5;i++){
cout<<"enter values"<<endl;
cin>>a[i];
sum=sum+a[i];
}
cout<<"the sum of the number is"<<sum;
return 0;
}
QUESTION #02
Program to find GCD between two numbers.
SOLUTION:
#include<iostream>
using namespace std;
int main(){
int num1, num2,x;
cout<<" Enter the 1st number";
cin>>num1;
cout<<"enter the 2nd number";
cin>>num2;
while(num2!=0){
x=num2;
num2=num1%num2;
num1=x;
}
cout<<"the gcd of the given numbers is ="<<num1;
return 0;
}
Question #03
Program to find sum of positive numbers .if the user enters a
negative number ‘the loop ends .the negative number entered
is not added to the sum.
SOLUTION:
#include<iostream>
using namespace std;
int main()
{int sum=0;
while(1){
cout<< "enter a num:";
int a;
cin>>a;
if(a<0){
cout<<"oops you entered a negative number"<<endl;break; }
sum=sum+a;
}
3

You might also like