Navigation Bar

Showing posts with label numbers. Show all posts
Showing posts with label numbers. Show all posts

Print the sequence of all Odd numbers



#include<conio.h>
#include<iostream.h>
void main()
{
clrscr();
int a, b;
cout<<"Enter Starting Limit";
cin>>a;
cout<<"Enter ending Limit";
cin>>b;   
cout<<"All Odd Numbers Between "<<a<<" & "<<b<<" is\n";
while (a<=b)
{
if (a!=0 && a%2!=0)
cout<<a<<" ";
a++;
}
getch();
}

Output


Enter Starting Limit: 2
Enter ending Limit: 10
All Even Numbers Between 2 & 10 is
3 5 7 9

Print the sequence of all Even numbers



#include<conio.h>
#include<iostream.h>
void main()
{                
   clrscr();
   int a, b;
   cout<<"Enter Starting Limit: ";
   cin>>a;
   cout<<"Enter ending Limit: ";
   cin>>b;
   cout<<"All Even Numbers Between "<<a<<" & "<<b<<" is\n";
   while (a<=b)
{
if (a!=0 && a%2==0)
cout<<a<<" ";
a++;
}
   getch();
}

Output


Enter Starting Limit: 3
Enter ending Limit: 17
All Even Numbers Between 3 & 17 is
4 6 8 10 12 14 16

Program to find perfect number

What is a perfect number?
"Perfect number is a positive number which sum of all positive divisors excluding that number.
For example 6 is Perfect Number since divisor of 6 are 1, 2 and 3. Sum of its divisor is
1 + 2+ 3 =6
and  28 is also a Perfect Number
 since 1+ 2 + 4 + 7 + 14= 28
Other perfect numbers: 496, 8128
CODE FOR PERFECT NUMBER IN C++



  1. #include<iostream.h
  2. #include<conio.h>
  3. void main()                 //Start of main
  4. {
  5.   clrscr();
  6.    int i=1, u=1, sum=0;
  7.    while(i<=500)
  8.  {                                  // start of first loop.
  9.    while(u<=500)
  10.    {                               //start of second loop.
  11.      if(u<i)
  12.      {
  13.       if(i%u==0 )
  14.       sum=sum+u;
  15.      }                          //End of if statement
  16.      u++;
  17.    }                           //End of second loop
  18.    if(sum==i)
  19.    {
  20.     cout<<i<<" is a perfect number."<<"\n";
  21.    }
  22.    i++;
  23.    u=1;  sum=0;
  24.  }                             //End of First loop
  25.    getch();
  26.  }
 Sample output

is a perfect number.
28 is a perfect number.
496 is a perfect number.

Note:
If you want to calculate perfect number within your desire limit simply take a variable and replaced 500 with it.