Learn C++ beginer programs

July 8, 2009

write a program to find sum of n numbers

Filed under: cin,cout,for loop,loop,numbers,sum — snehilkhanor @ 1:27 pm
A number n is taken from user.
Sum of all natural numbers upto n is displayed.
Select To use this code as it is.. select and copy paste this code into code.cpp file :)
  1. #include<iostream.h>
  2. #include<conio.h>
  3. void main()
  4. {
  5. clrscr();
  6. int n,sum=0;
  7. cout<<”Please enter a number: “;
  8. cin>>n;
  9. for (int i=1;i<=n;i++)
  10. sum=sum+i;
  11. cout<<sum;
  12. getch();
  13. }

July 5, 2009

Write a program to calculate sum of two numbers

Filed under: cin,cout,sum — snehilkhanor @ 6:05 pm
Two numbers are taken from the user and sum is calculated.

Select To use this code as it is.. select and copy paste this code into code.cpp file :)
  1. #include <iostream.h>
  2. #include <conio.h>
  3. void main()
  4. {
  5. clrscr();
  6. float a,b,s;
  7. cout<<”Please enter a no.: “;
  8. cin>>a;
  9. cout<<”Please enter another no.: “;
  10. cin>>b;
  11. s=a+b;
  12. cout<<”The sum is: “<<s;
  13. getch();
  14. }

Theme: Rubric. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.