Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
23 views

Program Using Looping Statement and Array

Uploaded by

ur mom
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Program Using Looping Statement and Array

Uploaded by

ur mom
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

1.

Write a program that prints 100, 98, 96, 94, 92, …, 0

(i) While

#include<iostream>

using namespace std;

int main()

int num=100;

while(num>=0)

cout<<num<<endl;

num=num-2;

return 0;

(ii) Do While

#include<iostream>

using namespace std;

int main()

int a=100;

do

cout<<a<<endl;

a=a-2;

}while(a>=0);

return 0;

}
(iii) For Loop
#include<iostream>
using namespace std;
int main()
{
for(int num=100; num>=0;num=num-2)
{
cout<<num<<endl;
}
return 0;
}

2. Write a program to print sum of negative numbers and sum of positive number from a list of
numbers entered by the user.

#include<iostream>
using namespace std;
int main()
{
int n[10],pos=0,neg=0;
cout<<"Enter the numbers";
for(int i=0; i<5;i++)
{
cin>>n[i];
}
for(int i=0;i<5;i++)
{
if(n[i]>0)
pos=pos+n[i];
else if(n[i]<0)
neg=neg+n[i];
else
cout<<n[i]<<"is zero";
}
cout<<"Positve sum="<<pos<<endl;
cout<<"Negative sum="<<neg<<endl;
return 0;
}

3.Write a program to print count of negative numbers and count of positive number from a list of
numbers entered by the user.
#include<iostream>
using namespace std;
int main()
{
int n[10],pos=0,neg=0;
cout<<"Enter the numbers";
for(int i=0; i<5;i++)
{
cin>>n[i];
}
for(int i=0;i<5;i++)
{
if(n[i]>0)
pos=pos+1;
else if(n[i]<0)
neg=neg+1;
else
cout<<n[i]<<"is zero";
}
cout<<"Positive count="<<pos<<endl;
cout<<"Negative count="<<neg<<endl;
return 0;
}

You might also like