-->

program to display first 5 Armstrong numbers between 0 and 1000.

//program to display first 5 Armstrong numbers between 0 and 1000
//we are taking numbers with three digits
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
int i=1,rem,k,sum=0,count=0;
while(i<=1000)
{
     k=i;
     sum=0;
while(k!=0)
  {
    rem=k%10;
    sum=sum+pow(rem,3);
    k=k/10;
  }

 if(sum==i)
 {
          count++;
           if(count<=5)
               {   printf("%d\n",i);
                }  }
 sum=0;
 i++;

}
getch();
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------
logics in mind:-
-------------------
->we have to display Armstrong numbers in given range 0 and 1000.Armstrong can be for 3/4 digits numbers.
   Here we taking 3 digits only.
->First  we take first number (k) then we apply following method        
                  ->For this, 
                 ->we have to divide by 10 to get last digit as remainder 
                 ->we find its cube and then go for sum
                -> to get second digit, we get first 12 and for this , we use 123/10.It is done to get integer                           part  only.
                  ->then we compare the number 'i' with that sum. If they are same then we display that.
->We repeat this until the value reaches 0 and for all numbers and individually. We use loop for this as shown above in the code.     
->as the first number is checked, we , once again, assign value '0' to sum to check next value.And this goes on. 
->while displaying Armstrong number, we also count it.If it is less than/equal to 5 then we display that.

program to display all Armstrong numbers between 0 and 100 solution here we are taking Armstrong number with 3 digit. but more digit also can be taken

//armstrong numbers lying in given range 0 and 1000
//we are taking numbers with three digits
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
int i=1,rem,k,sum=0;
while(i<=1000)
{
     k=i;
     sum=0;
while(k!=0)
 {
   rem=k%10;
   sum=sum+pow(rem,3);
   k=k/10;
 }

if(sum==i)
{
      printf("%d\n",i);

}
sum=0;
 i++;

}
getch();
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------
logics in mind:-
-------------------
->we have to display Armstrong numbers in given range 0 and 1000.Armstrong can be for 3/4 digits numbers.
   Here we taking 3 digits only.
->First  we take first number (k) then we apply following method        
                  ->For this, 
                 ->we have to divide by 10 to get last digit as remainder 
                 ->we find its cube and then go for sum
                -> to get second digit, we get first 12 and for this , we use 123/10.It is done to get integer                           part  only.
                  ->then we compare the number 'i' with that sum. If they are same then we display that.
->We repeat this until the value reaches 0 and for all numbers and individually. We use loop for this as shown above in the code.     
->as the first number is checked, we , once again, assign value '0' to sum to check next value.And this goes on.  

program to display first 20 prime numbers between 0 and 500

-----------------------------------------------------------------------------------------------------
// first twenty prime numbers lying in given range 0 and 500
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int i,j,count,first=0,total=0;
for(i=1;i<=500;i++)
{
    if(i<=3)
{
 printf("%d\n",i);

}
      else
      {
     count=0;
       for(j=2;j<=i-1;j++)
 {
   if(i%j==0)
     {
      count=1;

     }
 }
       }

 if(count==0 )

  {
    total++;

if(total<=17 )

{
 printf("%d\n",i);
}
  }
}
getch();
}
--------------------------------------------------------
logics in mind:-
-------------------
->we have to display first twenty prime numbers in given range 0 and 100
->numbers are 1,2,3,,5,7,....
  we can see that first three numbers are 1,2,3 prime so for them we have used loop with 'if'
  If we count then we have three numbers  as a total between 1 and 3.
-> as the number(i) exceeds 3 it transfers the control to next loop where that number is divided from 2 to  i-1. We are not taking 1 because we want to know , is there any number which can divide with remainder '0' or not.
->We want to know here , how many numbers are there which can divide completely with remainder '0'.
->To know that, we have used one variable 'count'. If it is divisible by by some other numbers then 'count' becomes '1'. otherwise it remains '0'.And to display first 17 numbers(3 already gone) we have used another variable as as a counter named 'total'.
->If the variable 'count' has same value '0' and total is less than 17   then we display that value using variable 'i'.

program to display all prime numbers between 0 and 100.

//prime numbers in given range 0 and 100
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int i,j,count;
for(i=1;i<=100;i++)
{
      if(i<=3)
{
 printf("%d\n",i);
}
      else
      {
     count=0;
       for(j=2;j<=i-1;j++)
 {
   if(i%j==0)
     {
      count=1;
     }
 }
      }
 if(count==0)
  {
   printf("%d\n",i);
  }
}
getch();
}
----------------------------------------------------------------------------------------------------------------------------------
logics in mind:-
---------------
->we have to display prime numbers in given range 0 and 100
->number s are 1,2,3,,5,7,....
  we can see that first three numbers are 1,2,3 prime so for them we have used loop with 'if'
-> as the number(i) exceeds 3 it transfers the control to next loop where that number is divided from 2 to  i-1. We are not taking 1 because we want to know , is there any number which can divide with remainder '0' or not.
->We want to know here , how many numbers are there which can divide completely with remainder '0'.
->To know that, we have used one variable 'count'. If it is divisible by by some other numbers then 'count' becomes '1'. otherwise it remains '0'.
->If the variable 'count' has same value '0' then we display that value using variable 'i'.


program to count number of digits present in a number.

//program to count number of digits present in a number.
#include<stdio.h>
#include<conio.h>
void main()
{
   int n,rem,count=0;
   printf("enter a number for 'n'\n");
  scanf("%d",&n);
  while(n!=0)
    {
        rem=n%10;
       count++;
        n=n/10;    
   }
printf("total digits =%d",count);
getch();
}
--------------------------------------------------------------------------------------------------
logics in mind:-
>first we enter a number(for 'n').
->If we have number 123 then it has 3 digits and they are 3, 2 and 1 or 1,2 and 3. It means, first we have to get 3 then 2 and then 1 and we have to count them.        
                  ->For this, 
                 ->we have to divide by 10 to get last digit as remainder 
                 ->we also use 'count' to start counting  digits.
                -> to get second digit, we get first 12 and for this , we use 123/10.It is done to get integer                           part  only.
->We repeat this until the value reaches 0. We use loop for this as shown above in the code.     
->at last we display total number of digits stored in variable 'count'.   

program to get sum of given series with................nth terms.

//program to get sum of x+ x2/2+x3/3+x4/4+.............................nth terms.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
  float i,n,x,m=1,sum=0;
   printf("enter  positive number for 'n'\n");
  scanf("%f",&n);
printf("enter value for 'x'\n");
scanf("%f",&x);
  for(i=1;i<=n;i++)
    {               
          sum=sum+(pow(x,m)/i);
           m=m+1;
     }
  printf("the sum=%f",sum);
getch();
}
-------------------------------------------------------------------------------------------------------------------------------------------------
logics in mind:
->enter a number for range for which you want to get sum, say 'n'.
->let a variable 'sum' with initial value '0'
->here we also need value for 'x' so let's input that
->we get sum using formula sum=sum+(pow(x,m)/i); because in terms x+ x2/2+x3/3+x4/4+..............  ,
    ->we have two components namely numerator and denominator. Let's look at numerator;they are
           x1,x2,x3,x4.....it can be formulated as pow(x,m) .  .here the power (m) goes on increasing.
   ->similarly for denominator, it is simply 1,2,3,4.... so it can be used from loop (variable i).
->we find sum inside loop
->at last we display the final sum.
   here 'pow' means finding power. it exists inside math.h

program to get sum of 1+3/4+7/16+15/64..............................nth terms.

//program to get sum of 1+3/4+7/16+15/64..............................nth terms.
#include<stdio.h>
#include<conio.h>
void main()
{
   float i,n,k=2,sum=0,m=0;
   printf("enter  positive number for 'n'\n");
  scanf("%f",&n);
  for(i=1;i<=n;i++)
    {               
          sum=sum+(pow(k,i)-1)/(pow(k,m));
          m=m+2;    
     }
  printf("the sum=%f",sum);
getch();
}
-------------------------------------------------------------------------------------------------------------------------------------------------
logics in mind:
->enter a number for range for which you want to get sum, say 'n'.
->let a variable 'sum' with initial value '0'
->we get sum using formula sum=sum+(pow(k,i)-1)/(pow(k,m)) because in terms 1+3/4+7/16+15/64...    ,
    ->we have two components namely numerator and denominator. Let's look at numerator;they are
           1,3,7,16.....it can be formulated as pow(k,i)-1 that is    21-1  .here the power goes on increasing
   ->similarly for denominator, it has power of 2 i.e.
                 20,22,24. we can see here that power is increasing by 2.
                for this we have,pow(k,m) then m=m+2
->we find sum inside loop
->at last we display the final sum.