Thursday, 12 April 2012

C++ Program to Count and Print Even Numbers and Sum of Odd Numbers

C++ Program to Count and Print Even Numbers and Sum of Odd Numbers..
Sample Output:
Code:

//Program to count and print the number of even numbers and sum of odd numbers.
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
int limit;
void cal(int);         //prototype void
cout<<"Enter Limit"<<endl;
cin>>limit;
cal(limit);          //calling the function
getch();
return(0);
}
void cal(int x)              //function cal with argument having one variable
{
int ctr=0,sum=0;
for(int i=1;i<=x;i++)
{
if(i%2==0)
{
ctr=ctr+1;
}
else
{
sum=sum+i;
}
}
cout<<"Even numbers="<<ctr<<endl;
cout<<"Sum of odd numbers="<<sum;
}

Comment bellow for your Query and Feedback... :)

No comments:

Post a Comment