C QEUSTION Exercise 1 Write a program that print out the fir
C++ QEUSTION!!!
Exercise 1
Write a program that print out the first N lines of following pattern:
#
##
###
####
#####
……
The first line includes 1 pound sign;
The second line includes 2 pound signs;
The third line includes 3 pound signs;
……
Exercise 2
Ask a user to input a positive integer number and calculate the sum of the digits.
For example: number = 245, the sum of the digits = 2 + 4 + 5 = 11
Solution
1)
#include <iostream>
using namespace std;
int main()
{
int n;
cout<<\"Enter a positive number:\";
cin>>n;
for(int i=1;i<=n;i++)
{
for(int j=0;j<i;j++)
{
cout<<\'#\';
}
cout<<endl;
}
return 0;
}
2)
#include <iostream>
using namespace std;
int main()
{
int n;
cout<<\"Enter a number:\";
cin>>n;
int sum=0;
while(n>0)
{
sum = sum + n%10;
n = n/10;
}
cout<<sum<<endl;
return 0;
}

