Program Using Looping Statement and Array
Program Using Looping Statement and Array
(i) While
#include<iostream>
int main()
int num=100;
while(num>=0)
cout<<num<<endl;
num=num-2;
return 0;
(ii) Do While
#include<iostream>
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;
}