Write a c program to check given number is strong number or not.





Code 1:
1. Write a c program to check whether a number is strong or not

#include<stdio.h>
int main(){
  int num,i,f,r,sum=0,temp;

  printf("Enter a number: ");
  scanf("%d",&num);
 
  temp=num;
  while(num){
      i=1,f=1;
      r=num%10;

      while(i<=r){
         f=f*i;
        i++;
      }
      sum=sum+f;
      num=num/10;
  }
  if(sum==temp)
      printf("%d is a strong number",temp);
  else
      printf("%d is not a strong number",temp);

  return 0;
}

Sample output:
Enter a number: 145
145 is a strong number

Code 2:
1. C program for strong number
2. Strong number program in c

#include<stdio.h>
int main(){
  int num,i,f,r,sum,temp;
  int min,max;

  printf("Enter minimum range: ");
  scanf("%d",&min);

  printf("Enter maximum range: ");
  scanf("%d",&max);

  printf("Strong numbers in given range are: ");
  for(num=min; num <= max; num++){
      temp = num;
      sum=0;

      while(temp){
           i=1;
           f=1;
           r=temp%10;

           while(i<=r){
             f=f*i;
             i++;
           }
         sum=sum+f;
         temp=temp/10;
      }
        
      if(sum==num)
           printf("%d ",num);
  }
  return 0;
}

Sample output:
Enter minimum range: 100
Enter maximum range: 100000
Strong numbers in given range are: 145 40585





Definition of strong number:

A number is called strong number if sum of the factorial of its digit is equal to number itself. For example: 145 since
1! + 4! + 5! = 1 + 24 + 120 = 145





10. Write a c program to add two numbers without using addition operator.
11. Write a c program to subtract two numbers without using subtraction operator.
15. Write a c program to solve quadratic equation.
18. Write a c program which passes structure to function.
28. Write a c program which takes password from user.
29. Write a scanf function in c which accept sentence from user.
30. Write a scanf function in c which accept paragraph from user.

11 comments:

  1. could u please explain with giving an example

    ReplyDelete
  2. strong number like 145=1!+4!+5!=1+24+120=145(individuals digits factorials is equal to number.

    use two loop(outer loop) that would extract reminder(individual digit) second loop for calculating factorials

    while(num>0)
    {
    r=num%10; 145%10=5;
    while(i<=5) 1<=5
    f=f*i f=1*1
    i++ i=2

    as loop complete f=120
    sum=sum+f (sum=120 in first time)
    num=num/10;
    }

    At end iteration sum is 145 equal to original num so it is strong number

    ReplyDelete
  3. could u give d definition of strong number............. plz........

    ReplyDelete
    Replies
    1. strong no. is that what adding the factorial of a number u will get the same no. for example(take a no. 145 1!+4!+5!=145)so 145 is the strong number.

      Delete
  4. /*the below program holds good for any no. of digits*/
    #include
    int main()
    {
    int m,t,i=1,s=0,n,r,j=1,k,fact;
    printf("enter the no: ");
    scanf("%d", &n);
    m=t=n;
    while(j!=0)
    {
    r=n%10;
    if(n<10)
    {
    break;
    }
    n=n/10;
    j++;
    }
    printf("no of digits=%d\n",j);
    while(i<=j)
    {
    r=m%10;
    k=1,fact=1;
    while(k<=r)
    {
    fact=fact*k;
    k++;
    }
    printf("%d\n",fact);
    m=m/10;
    s=s+fact;
    i++;
    }
    printf("sum=%d\n",s);
    if(s==t)
    {
    printf("valid");
    }
    else
    {
    printf("invalid");
    }
    getch();
    return 0;
    }

    ReplyDelete
  5. can anyone explain about how to find out strong number in a given range

    ReplyDelete
  6. i have my own logic that i can show you

    ReplyDelete
  7. instead i have one more effective one logic i suggest the code like

    for finding perfect number we are saying that given number is strong if the sum of the factorials of its digit = number

    so i have suggested a code if you find suitable

    //while(num!=0)
    { r=num%10;
    t=factorial(r);
    sum+=t;
    num=num/10;
    }
    after this inside this loop function call
    int factorial(int r)
    { if(r==0||r==1)
    return 1;
    else
    return r*factorial(r-1);
    }
    //
    bcoz lets take an eg.
    145
    while(145){
    r=num%10 // 145%10=5
    t=factorial(5);//on fucn call recursively factorial function recurisvely solves it to 5!=120
    then
    sum=sum+t//sum=0+120=120 so now sum becomes 120
    then num=num/10=145/10=14
    now again loop starts
    while(14)
    and finally you see answer comes out to be 145 and this equals the given number
    so if anyone feel about the complexity of my code please suggest me over this

    ReplyDelete
  8. :
    1. Begin
    2. Repeat steps 3 through 9 for 1000 times
    3. Accept values of UserID, UserName, LastMonthMeterReading, CurrentMonthMeterReading and store in respective variables
    4. Display user types as below to user and accept his/her choice and store in UserType:
    3.1 1 = BPL Home
    3.2 2 = APL Home
    3.3 3 = Commercial Small
    3.4 4 = Commercial Large
    5. Assign FixedCharge based on the foll. conditions:
    5.1 If UserType = 1, FixedCharge = 100
    5.2 If UserType = 2, FixedCharge = 120
    5.3 If UserType = 3, FixedCharge = 200
    5.4 If UserType = 4, FixedCharge = 100
    5.5 If UserType is anything else, print error message and exit
    6. If LastMonthMeterReading < CurrentMonthMeterReading, goto step 6 --- else print an error message and exit
    7. Assign difference of CurrentMonthMeterReading and LastMonthMeterReading to UnitsConsumed
    8. Calculate the net amount for the month and store it as shown:

    8.1 If UserType = 1:
    8.1.1 If UnitsConsumed <= 100, NetAmount = UnitsConsumed * .75 + FixedCharge
    8.1.2 If 100 < UnitsConsumed <=150, NetAmount = 75 + ((UnitsConsumed-100) * 1.25) + FixedCharge
    8.1.3 If UnitsConsumed > 150, NetAmount = 75 + 62.5 + ((UnitsConsumed-150) * 2) + FixedCharge

    8.2 If UserType = 2:
    8.2.1 If UnitsConsumed <= 100, NetAmount = UnitsConsumed * .95 + FixedCharge
    8.2.2 If 100 < UnitsConsumed <=150, NetAmount = 95 + ((UnitsConsumed-100) * 1.5) + FixedCharge
    8.2.3 If UnitsConsumed > 150, NetAmount = 95 + 75 + ((UnitsConsumed-150) * 2) + FixedCharge

    8.3 If UserType = 3:
    8.3.1 If UnitsConsumed <= 100, NetAmount = UnitsConsumed * 1.25 + FixedCharge
    8.3.2 If 100 < UnitsConsumed <=150, NetAmount = 125 + ((UnitsConsumed-100) * 2) + FixedCharge
    8.3.3 If UnitsConsumed > 150, NetAmount = 125 + 100 + ((UnitsConsumed-150) * 4) + FixedCharge

    8.4 If UserType = 4:
    8.4.1 If UnitsConsumed <= 100, NetAmount = UnitsConsumed * 2.5+ FixedCharge
    8.4.2 If 100 < UnitsConsumed <=150, NetAmount = 250 + ((UnitsConsumed-100) * 2.75) + FixedCharge
    8.4.3 If UnitsConsumed > 150, NetAmount = 250 + 137.5 + ((UnitsConsumed-150) * 5) + FixedCharge

    9. Print the values in format as shown below:
    User ID:
    User Name:
    User Type:
    Units Consumed:
    Net Amount:
    10. End program

    ReplyDelete
  9. 1.5 Psuedo code to introduce different types of user with different slabs
    TASK LIST:
    READ user id,user name,unit consumed,user type
    is userid=set of characters and numbers?
    yes:validate user name
    no:exit
    is user name=set of characters?
    yes:validate last month meter reading
    no:exit
    is last month meter reading =number?
    yes:validate current month meter reading
    no:exit
    is current month meter reading=number?
    yes:validate user type
    no:exit
    is user type=set of characters?
    yes:calculate the unit consumed
    no:exit
    CREATE function bplhome(int fixed charge,int unit consumed,float net amount)
    {
    fixed charge=100
    if(unit consumed is<=100)
    net amount=(unit consumed*0.75)+fixed charge
    else
    if(unit consumed is >100&&<=150)
    net amount=(unit consumed*0.75+fixed charge)+(unitconsumed-100)*1.25+fixed charge
    else
    if(unit consumed is >150)
    net amount=(unit consumed*0.75+fixed charge)+(unitconsumed-100)*1.25+fixed charge
    (unit consumed-50)*2.00)+fixes charge

    }
    end function
    CREATE function aplhome(int fixed charge,int unit consumed,float net amount)
    {
    fixed charge=120
    if(unit consumed is <=100)
    net amount=(unit consumed*0.95)+fixed charge
    else
    if(unit consumed is >100&&<=150)
    net amount=(unit consumed*0.95+fixed charge)+(unitconsumed-100)*1.50+fixed charge
    else
    if(unit consumed is >150)
    net amount=(unit consumed*0.95+fixed charge)+(unitconsumed-100)*1.50+fixed charge
    (unit consumed-50)*2.00)+fixes charge
    }
    end function
    CREATE function commercial small(int fixed charge,int unit consumed,float net amount)
    {fixed charge=200
    if(unit consumed is <=100)
    net amount=(unit consumed*1.25)+fixed charge
    else
    if(unit consumed is >100&&<=150)
    net amount=(unit consumed*1.25+fixed charge)+(unitconsumed-100)*2.00+fixed charge
    else
    if(unit consumed is >150)
    net amount=(unit consumed*1.25+fixed charge)+(unitconsumed-100)*2.00+fixed charge
    (unit consumed-50)*4.00)+fixes charge
    }
    end function
    CREATE function commercilal large(int fixed charge,int unit consumed,float net amount)
    {
    fixed charge=500
    if(unit consumed is <=100)
    net amount=(unit consumed*2.50)+fixed charge
    else
    if(unit consumed is >100&&<=150)
    net amount=(unit consumed*2.50+fixed charge)+(unitconsumed-100)*2.75+fixed charge
    else
    if(unit consumed is >150)
    net amount=(unit consumed*2.50+fixed charge)+(unitconsumed-100)*2.75+fixed charge
    (unit consumed-50)*5.00)+fixes charge
    }
    end function

    PSUEDO CODE
    net amount=0,unit consumed=0,fixed charge=0,net amount
    unit consumed=current month meter reading-last month meter reading
    if(user type= bpl home)
    {
    call function bpl home
    }
    endif
    else
    if(user type= apl home)
    {
    call function apl home
    }
    endif
    else
    if(user type=commercial small)
    {
    call function commercial small
    }
    endif
    else
    if(user type=commercial large)
    {
    call function commercila large
    }
    endif
    WRITE
    user id:
    user name:
    unit consumed:
    net amount:




    ReplyDelete
  10. READ UserId,UserName,LastMonthMeterReading,CurrentMonthReading
    Enter valid UserId
    FixedCharge=100

    Lable 1:READ UserTtyoe
    IF usertype is not string
    WRITE type valid usertype
    goto lable 1
    else

    Lable 2:READ username
    IF username is not valid
    WRITE valid username
    goto lable 2
    else
    Lable 3 READ lastmonth readind
    IFlastmonthreading not integer
    WRITE enter valid lastmonthreading
    goto lable 3
    else

    lable 4 READ currentmonthreading
    IF currentmonthreading is notb integer
    WRITE enter valid currentmonthreading
    goto lable 4
    else


    UnitConsumed=CurrentReading-LastMonthReading
    NetAmount=UnitConsumed*1.15+FixedCharge
    WRITE UserID
    WRITE UserName
    WRITE UnitConsumed
    WRITE NetAmount

    ReplyDelete