Welcome To Uday Satya Blog

11-20 PROGRAMS



/***************************************************
               11.  Program to reverse a number
*****************************************************/

#include
#include
void main()
{
  int n,r=0,s;
  clrscr();
  cout<<"Enter the number to be reversed"<
  cin>>n; 
  while(n>0)
 {
  s=n%10;
  n=n/10;
  r=r*10+s;
}
cout<<"The reversed number is "<
getch();
}

OUTPUT:
Enter the number to be reversed 134
The reversed number is 431














/*********************************************************
        12. Program to find roots of a quadratic equation
***********************************************************/

#include
#include
#include
void main()
{
  float a,b,c,sq,x,r,x1,x2,real,imag;
  clrscr();
  cout<<"Enter the x^2 coefficient (a)"<
  cin>>a;
  cout<<"Enter the x coefficient (b)"<
  cin>>b;
  cout<<"Enter the constant value (c)"<
  cin>>c;
  sq=b*b-4*a*c;
  if (sq==0)
  {
    x=-b/(2*a);
    cout<<"The roots are: x1="<            }
  else if (sq>0)
  {
    r=sqrt(sq);
    x1=(-b+r)/(2*a);
    x2=(-b-r)/(2*a);
    cout<<"The roots are: x1="<            }
else if (sq<0)
{
    sq=-sq;
    r=sqrt(sq);
    real = -b/(2*a);
    imag = sq/(2*a);
    cout<<"Roots are complex, The roots are:"<
    cout<<"x1="<
    cout<<"x2="<
    cout<
            }
getch();
}



OUTPUT:
Enter the x^2 coefficient (a)    1
Enter the x coefficient (b)    -1
Enter the constant value (c)   -6
x1=3   x2=-2




























/**************************************************************
         13. Program to check a given number is prime or not
***************************************************************/

#include
#include
void main()
{
  int n,i,b=0,a;
  clrscr();
  cout<<"Enter the number to check if it is prime \t ";
  cin>>n;
  cout<
  for (i=2;i
  {
    a=n%i;
    if(a==0)
    {
    b++;
    break;
            }
            }
  if(b==0)
  cout<<"The number is prime"<
  else
  cout<<"The number is not prime"<
  getch();
}

OUTPUT:
Enter the number to check if it is prime 7
The number is prime






/*********************************************************
         14. Program to print prime numbers from 1 to n
**********************************************************/

#include
#include
void main()
{
  int n,i,b=0,a,j;
  clrscr();
  cout<<"Enter nth number \t ";
  cin>>n;
  cout< 
  cout<<"The prime numbers are "<
  for (j=2;j<=n;j++)
 {
  for (i=2;i
  {
    a=j%i;
    if(a==0)
    {
    b++;
    break;
            }
            } 
  if(b==0)
  cout<
  b=0;
}

getch();
}

OUTPUT:
Enter nth number  15

The prime numbers are
2 3 5 7 11 13


/***********************************************************
         15. To find if a number is an armstrong or not
*************************************************************/

#include
#include
void main()
{clrscr();
int n,r,a,orig;
cout<<"Enter the number to find if it is armstrong ";
cin>>n;
orig=n;
a=0;
while(n>0)
{r=n%10;
 a=a+(r*r*r);
 n=n/10;
 }
 if(a==orig)
 cout<<"The number is an armstrong number ";
 else
 cout<<"The number is not an armstrong number ";

getch();
}

OUTPUT
Enter the number to find if it is armstrong 153
The number is an armstrong number









/********************************************************
        16. Prg to print armstrong numbers from 1-n
*********************************************************/

#include
#include
void main()
{clrscr();
int n,r,a,orig;
cout<<" Enter the limit ";
cin>>n;
for(int i=1;i<=n;i++)
{orig=i;
 a=0;
 while(i>0)
 {r=i%10;
  a=a+(r*r*r);
  i=i/10;
 }
 if(a==orig)
 cout<
 i=orig;
}
getch();
}

OUTPUT
Enter the limit 500
1
153
370
371
407






/*************************************************************
        17. Program to check a given number is perfect or not
**************************************************************/

#include
#include
void main()
{
  int n,s=0,d;
  clrscr();
  cout<<"Enter the number to check if perfect number"<
  cin>>n;
  for ( int i=1; i
  {
    d=n%i;
    if ( d==0)
    s = s + i;
    }
  if ( s==n)
  cout<<"The number is a perfect number"<
  else
  cout<<"The number is not a perfect number"<
   getch();
  }

OUTPUT:
Enter the number to check if perfect number  6
The number is a perfect number











/********************************************************
        18. Program to print perfect numbers from 1 to n
**********************************************************/

#include
#include
void main()
{
  int n,s=0,d;
  clrscr();
  cout<<"Enter the nth number"<
  cin>>n;
 cout<<"The perfect numbers are"<
 for (int j=1;j<=n;j++)
{
  for ( int i=1; i
  {
    d=j%i;
    if ( d==0)
    s = s + i;
    }
  if ( s==j)
  cout<
  s=0;
}

   getch();
  }

OUTPUT:
Enter the nth number  500
The perfect numbers are  6  28 496







/****************************************************
           19. Program to print Fibonacci series
*****************************************************/

#include
#include
void main()
{
  int f1,f2,n;
  clrscr();
  cout<<"How many numbers you want print"<
  cin>>n;
  cout<<"Enter the first two number of the series"<
  cin>>f1>>f2;
  cout<<"The fibonacci series:"<
  cout<
  for (int i=2; i
  {
    int f=f1+f2;
    cout<<"\t"<
    f1=f2;
    f2=f;
            }
   getch();
}
 
OUTPUT:
How many numbers you want print 6
Enter the first two number of the series 0 1

The fibonacci series:
0  1  1  2  3  5







/*****************************************************
          20.  Program to print multiplication table
******************************************************/

#include
#include
void main()
{
  int n;
  clrscr();
  cout<<"Enter the table number to be printed"<
  cin>>n;
  cout<<"The multiplication table is "<
  for ( int i=1; i<=12 ; i++)
  {
    int c=i*n;
    cout<
            }
   getch();
}



OUTPUT:

Enter the table number to be printed 3
The multiplication table
1*3=3
2*3=6
3*3=9
4*3=12
5*3=15
6*3=18
7*3=21
8*3=24
9*3=27
10*3=30
11*3=33
12*3=36

0 comments:

Post a Comment