This repository has been archived by the owner on May 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 772
Create armstrong.c #1311
Open
dakshbamola
wants to merge
2
commits into
MakeContributions:main
Choose a base branch
from
dakshbamola:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Create armstrong.c #1311
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// This program is used to check if a given no is armstrong number or not in C | ||
// an armstrong number is a number that equals the sum of its digits, each raised to a power. | ||
// power is the number of digits in the number eg. 123 has three digits, so power will be 3. | ||
|
||
#include<stdio.h> | ||
#include<math.h> | ||
|
||
int main(){ //main function | ||
int num,digit=0,temp,sum=0; //define variables | ||
printf("enter the number: "); | ||
scanf("%d",&num); //taking inputs | ||
temp=num; | ||
while(temp>0){ | ||
digit++; //counting number of digits in the input number | ||
temp=temp/10; | ||
} | ||
printf("Number of digits: %d\n",digit); | ||
temp=num; | ||
while(temp>0){ | ||
sum=sum+ (int)(pow(temp%10, digit)+0.5); // checking the armstrong property of the number | ||
temp/=10; // we extract a digit from number using (temp%10) then | ||
} // raise that digit to number of digits in the number using pow function | ||
// (pow()+0.5) is used to increase precision | ||
printf("Sum of powered digits: %d\n",sum); | ||
if(num==sum){ //checking if the new number is equal to the input number | ||
printf("Armstrong"); | ||
} | ||
else{ | ||
printf("Not Armstrong"); | ||
} | ||
return 0; | ||
} | ||
|
||
// Input || Output | ||
// 153 || Armstrong | ||
// 371 || Armstrong | ||
// 1634 || Armstrong | ||
// 123 || Not Armstrong | ||
|
||
// This program has time complexity of O(N). |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You have to return something, if your return type is int.
return 0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added
return 0;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added more clarity in print statements.