Showing posts with label Sum. Show all posts
Showing posts with label Sum. Show all posts

C++ Program to display the sum of this series: 1+1/2+1/3....1/n.

C++ Program to display the sum of this series: 1+1/2+1/3....1/n.



#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
float a,b=0,i;
cout<<"Enter a number: ";
cin>>a;
for(i=1;i<=a;i++)
b=b+(1/i);
cout<<"The sum of the series is: "<<b;
getch();
}

C++ Program to display the sum of a number's digits.

C++ Program to display the sum of a number's digits.



#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a,b,c;
cout<<"Enter a number to display sum of it's digits: ";
cin>>a;
b=a%10;
while(a!=0)
{
a=a/10;
c=a%10;
b=b+c;
}
cout<<"The sum of the digits is: "<<b;
getch();
}