0% found this document useful (0 votes)
16 views4 pages

BSCS0123015078

This document contains an assignment submission for a programming fundamentals class. It includes solutions to 3 questions - writing a program to calculate the sum of the first 5 numbers input by the user using a for loop, writing a program to find the greatest common divisor (GCD) of two numbers using a while loop, and writing a program to calculate the sum of positive numbers input by the user until a negative number is entered using a while loop.

Uploaded by

Faseeh Ur Rehman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views4 pages

BSCS0123015078

This document contains an assignment submission for a programming fundamentals class. It includes solutions to 3 questions - writing a program to calculate the sum of the first 5 numbers input by the user using a for loop, writing a program to find the greatest common divisor (GCD) of two numbers using a while loop, and writing a program to calculate the sum of positive numbers input by the user until a negative number is entered using a while loop.

Uploaded by

Faseeh Ur Rehman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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