-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Create sum_of_digits.c * Create product_of_2_numbers.c * Update README.md
- Loading branch information
1 parent
af75f9c
commit 16cf02f
Showing
3 changed files
with
137 additions
and
1 deletion.
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,37 @@ | ||
/* | ||
* @author: ashwek | ||
* @date: 8/1/2019 | ||
*/ | ||
|
||
#include <stdio.h> | ||
|
||
int product(int a, int b){ | ||
|
||
if( a == 0 || b == 0 ) | ||
return 0; | ||
else if( a == 1 ) | ||
return b; | ||
else if( b == 1 ) | ||
return a; | ||
|
||
if( a < 0 && b < 0 ) | ||
return product(-a, -b); | ||
else if( a < 0 ) | ||
return product(b, a); | ||
else | ||
return b + product(a-1, b); | ||
|
||
} | ||
|
||
void main(){ | ||
|
||
int a, b; | ||
|
||
printf("Enter 1st number = "); | ||
scanf("%d", &a); | ||
printf("Enter 2nd number = "); | ||
scanf("%d", &b); | ||
|
||
printf("%d x %d = %d\n", a, b, product(a, b)); | ||
|
||
} |
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,24 @@ | ||
/* | ||
* @author: ashwek | ||
* @date: 8/1/2019 | ||
*/ | ||
|
||
#include <stdio.h> | ||
|
||
int sum(int num) { | ||
if( num <= 0 ){ | ||
return 0; | ||
} | ||
return (num%10) + sum(num/10); | ||
} | ||
|
||
void main(){ | ||
|
||
int num; | ||
|
||
printf("Enter a number = "); | ||
scanf("%d", &num); | ||
|
||
printf("Sum of digits = %d\n", sum(num)); | ||
|
||
} |
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