Simple math in C++: Difference between revisions

wikademia>Nirdh
added a sample program
Line 80: Line 80:
=== End of lesson 3 ===
=== End of lesson 3 ===


You should now be able to get C++ to do some simple math for you. In reality, it never gets much more complex than this. Almost everything you will write will be based on just a few simple operators. Thanks for reading this far. Hopefully you have learned something.
You should now be able to get C++ to do some simple math for you. In reality, it never gets much more complex than this. Almost everything you will write will be based on just a few simple operators. Thanks for reading this far. Hopefully you have learned something. Still, for your enhancement, following is the program that finds prime numbers until specified number:
 
 
 
----
 
/*This program finds prime numbers between 1 and number provided by user*/
<code><pre>
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();
unsigned int a,i,n,x;
cout<<"This program is developed by Nirdh... press enter to continue.";
cout<<"Enter a number till which prime numbers are wanted: ";
cin>>a;
for(x=1;x<a+1;x++)
{
  n=0;
  for(i=1;i<=x;i++)
  {
  if(x%i==0)
  {
    n=n+1;
  }
  else
  {
    n=n;
  }
  }
  if(n<3)
  {
  cout<<x<<"\t";
  }
}
getch();
}
</pre></code>
 
--[[User:Nirdh|Nirdh]] 11:36, 5 April 2009 (UTC)
 
----
 
 


==Where To Go Next==
==Where To Go Next==