Printing Strong Numbers in the given Range | C Language Tutorial

preview_player
Показать описание
Printing Strong Numbers in the given Range
C Language Tutorial Videos

? Visit Our Website for Classroom Training:
? For Online Training:
--------------------------

? About NareshIT:

"Naresh IT is having 14+ years of experience in software training industry and the best Software Training Institute for online training, classroom training, weekend training, corporate training of Hadoop, Salesforce, AWS, DevOps, Spark, Data Science, Python, Tableau, RPA ,Java, C#.NET, ASP.NET, Oracle, Testing Tools, Silver light, Linq, SQL Server, Selenium, Android, iPhone, C Language, C++, PHP and Digital Marketing in USA,Hyderabad, Chennai and Vijayawada,Bangalore India which provides online training across all the locations

--------------------------

? Our Online Training Features:
1.Training with Real-Time Experts
2.Industry Specific Scenario’s
3.Flexible Timings
4.Soft Copy of Material
5. Share Videos of each and every session.

--------------------------

+1404-232-9879 or India: +918179191999

** Check The Below Links**
? Follow us on Linkedin:
Рекомендации по теме
Комментарии
Автор

here's the solution(with correction):-

#include<stdio.h>
#include<conio.h>
void main()
{
long int i, j, range, index, sum=0, fact=1, temp;
clrscr();
printf("Enter any number here:");
scanf("%ld", &range);
for(i=1;i<=range;i++)
{
temp=i;
sum=0;
while(temp!=0)
{
fact=1;
index=temp%10;
for(j=1;j<=index;j++)
{
fact=fact*j;
}
temp=temp/10;
sum=sum+fact;
}

if(sum==i)
{
printf("%ld\n", i);
}
}
getch();
}

I've tried it so many times and it giving me the perfect and correct result. So you've to try it too.

bossysmaxx
Автор

here is the correct program
//program to check whether a number is a strong number or not in a given limit
#include <stdio.h>
main()
{
int n, r, i, sum, fact, k, limit;
printf("Enter a value: ");
scanf("%d", &limit);
for(n=1; n<=limit; n++)
{
sum = 0;
k=n;
while(k>0)
{
r=k%10;
k=k/10;
fact = 1;
for(i=1; i<=r; i++)
{
fact = fact*i;
}
sum = sum + fact;
}
if(sum == n)
{
printf("%d is a strong number\n", n);
}
else
{
printf("%d is not a strong number\n", n);
}
}
}

dhruvjain
Автор

i refered many codes not only this but different logics, i never felt i got it, complete concept
but you made it clear sir, thank you so much keep going

deepakreddyt
Автор

solution:-
#include<stdio.h>
void main()
{
int limit, n, i, r, sum, fact, tmp;
printf("enter the limits=");
scanf("%d", &limit);
for(n=1;n<=limit;n++)
{
sum = 0;
tmp = n;
while(n>0)
{
r=n%10;
fact=1;
for(i=r;i>=1;i--)
{
fact=fact*i;
}
sum=sum+fact;
n=n/10;
}
n=tmp;
if(n==sum)
{
printf("%d is strong number\n", n);
}else
{
printf("%d is not the strong number\n", n);
}
}
}
if we put the limit=145 then it will give strong number between 1 to 145.

smitamohite
Автор

I think we need to assign 'n' value to some 'temp' variable before while loop, and we have to check with sum .

--Asha--
Автор

Nice strong explanation sir for strong num program thank u so much sir

rajeshbe
Автор

'n' value is lost while checking if it is equal to 'sum'. So, assigning the original number to another variable and checking this variable against 'sum' is needed.

craiguser
Автор

Here is the complete code:
#include<stdio.h>
main()
{

int n, r, sum=0, i, t, temp;
printf("enter the value of n");
scanf("%d", &n);
temp=n;
while(n>0)
{
r=n%10;
t=r;
n=n/10;
for(i=1;i<t;i++)
{
r=r*i;
}
sum=sum+r;
}
if(sum==temp)
printf("it is a strong number");

else
printf("it is not");
}

goyaldeekshant
Автор

I think
for(n=1;n<=limit;n++)
Is correct
#Naresh I technologies

kistala_saikumar
Автор

/*strong number in given range*/

#include <stdio.h>

int main()
{
long int limit, r, n, temp, fact, i, sum=0;
printf("enter the value of the limit=");
scanf("%ld", &limit);


for(n=1;n<=limit;n++)
{
sum=0;
temp=n;
while(n>0)
{
r=n%10;
fact=1;
for(i=r; i>=1;i--)
{
fact=fact*i;
}
sum=fact+sum;
n=n/10;
}
n=temp;
if(n==sum)
{
printf("strong numbers in given range are =%ld \n", n);
}

}

return 0;
}

parthiksonagara
Автор

The correct program is as bellow:
int main()
{
int n, i, count=0;
printf("Enter any integer to check whether it is prime number or not:\n");
scanf("%d", &n);
for(i=2; i<=n/2; i++)
{
if(n%i==0)
{
count=1;
}
}
if(count==0)
printf("%d is a prime number", n);
else
printf("%d is not a prime number", n);
return 0;
}

wolverinelogan
Автор

Sir the value of n is changing, so temp variable is required?

arjunv
Автор

sir in first for loop you passed i in condition checking i or n it wiil be

samratchatterjee
Автор

thank you sir for excellent teaching, temp=n may required ?

nravikiran
Автор

In last you are comparing sum with n, this statement will be always false as n becomes 0. So we need to declare one temp variable before the while loop and we can assign num value to temp so that we can compare sum with the temp.

Fudu
Автор

I've just stored the i's value in temp var where i stores its value in temp and used temp to prevent loop infinite occurrence. and temp gets reset everytime when loop starts from beginning. That's all. The snippet is in the next comment.

bossysmaxx
Автор

You have to store n value in another variable before loop

amitjaiswal
Автор

there is a problm...i think in the first loop (i<=limit )it is wrong

subhrajitroy
Автор

You didn't stored n value in temp.. So whenever you check in last while printing the number.. No number will be perfect number.. As n's value would be 0

vaibhavgarg
Автор

there is a mistake in the code.
it should be like this
//printing strong number in the given range....strong number:(sum of the idividual factorial of individual digits is called strong number)

#include<stdio.h>
void main()
{
int sum=0, fact=1, r=0, temp, limit;
printf("enter lmit:");
scanf("%d", &limit);
for(int n=1;n<=limit;n++)
{ temp=n;
while(n>0)
{
r=n%10;
if(r!=0)
{
while(r!=0)
{
fact=fact*r;
r--;
}
sum=sum+fact;
}
fact=1;

n=n/10;

}
n=temp;
if(n==sum)
{
printf("%d ", sum);
}
sum=0;
}
}

Praveentechintelugu