First Day C++ program
1. Hello World program
Code:
#include<iostream>
using namespace std;
main()
{
cout<<"Hello World";
return 0;
}
2. Program for simple interest
Code:
#include<iostream>
using namespace std;
main()
{
int p,r,t;
float si;
cout<<"type principle rate and time"<<endl;
cin>>p>>r>>t;
si=(p*r*t)/100.0; // 100.0 because of type casting
cout<<si;
return 0;
}
3. Program to find sum of numbers from 1 to N
Code:
#include<iostream>
using namespace std;
main()
{
int n;
cin>>n;
int i=1;
int sum=0;
while(i<=n){
sum=sum+i;
i=i+1;
}
cout<<"Sum of 1 to "<<n<<" is : "<<sum<<endl;
return 0;
}
4. Program to find sum of Digits
Code:
#include<iostream>
using namespace std;
main()
{
int n;
cin>>n;
int sum=0;
while(n>0){
int last_digit=n%10;
sum=sum+last_digit;
n=n/10; // update statement
}
cout<<sum<<endl;
return 0;
}
0 Comments