int a,b,n;
printf("Input Natural Number n (n<2,100,000,000) : ");
scanf("%d",&n);
for(a=1;a<=100;a++)
for(b=1;b<=100;b++)
if(a<b && a*a + b*b == n*n)
{
printf("(%d, %d, %d)\n",a,b,n);
}
/*else
{
printf("impossible \n");
}
*/
return 0;
if I delete 'else' the program runs correctly. But I want to make another function which can check the number has pythagorean numbers or not by using 'else' paragraph. But when I put 'else' paragraph in that code, the result is dizzy.... plz help me!!
Put braces around the nested code blocks.
int a, b, n;
int impossible = 1;
printf("Input Natural Number n (n<2,100,000,000) : ");
scanf("%d", &n);
for (a = 1; a <= 100; a++) {
for (b = 1; b <= 100; b++) {
if (a < b && a * a + b * b == n * n) {
printf("(%d, %d, %d)\n", a, b, n);
impossible = 0;
}
}
}
if (impossible == 1) printf("impossible \n");
return 0;
Here is a possible answer
#include <stdio.h>
int power(int base, int power);
int main(){
int N;
printf("INput the Num: ");
scanf("%d", &N);
int a, b, c;
for(a = 0; a < N ; a++) {
for(b = 0; b< N; b++) {
if ((a < b) && (b < N - a - b)) {
if (power(a, 2) + power(b, 2) == power(N - a - b, 2)) {
printf("%d^2 + %d^2 = %d^2 \n", a, b, N-a-b);
}
}
}
}
}
int power(int base, int power) {
int result = 1;
for(int i = 0; i < power ; i++) {
result *= base;
}
return result;
}
Related
So, here is my code,
int
is_prime(int m) {
int i, prime;
if (m == 2)
prime = 1;
if (!(m % 2))
prime = 0;
if (m < 2)
prime = 0;
for (i = 2; i <= sqrt(m); i++) {
if (m % i == 0) {
prime = 0;
break;
}
}
return prime;
}
int
main() {
int m, n, t_m, t_n;
while (scanf("%d %d", &n, &m) != EOF) {
t_n=n;
t_m=m;
while(1) {
int d = gcd(t_n, t_m);
t_n = t_n - 1;
if (d > 1) {
t_m = t_m / d;
if (t_n < t_m && is_prime(t_m)) {
printf("%d does not divide %d!\n", m, n);
break;
}
}
}
}
return 0;
}
It should break out from that if, right? I've tried putting it in a function and using the return statement, but that doesn't work, either.
I have this program to generate armstrong numbers upto a given range. But the problem is that the range variable (n in this case) is somehow acting like a const. I cannot assign a value nor increment it... There are errors during compilation with gcc. The power function works fine (Same issue with pow() defind in math.h). I would like to know why this is happening to this code and the possible fix(es). Thank you
#include <stdio.h>
//#include <math.h>
int power(int a, int b) {
int c = 1;
for(int i = 0; i < b; i++) {
c *= a;
}
return c;
}
void main(void) {
int sum, y, temp;
printf("temp, y, sum, n\n");
for(int n = 1; n < 100; n++) {
temp = n;
printf("%d ", temp);
y = 0; // y to hold the number of digits of n
while (n > 0) {
n = n / 10;
y++;
}
printf("%d ", y);
n = temp;
sum = 0;
while(n > 0) {
sum += power((n % 10), y);
n = n / 10;
}
if (temp == sum) {
printf("%d ", sum);
}
printf("%d\n", n);
}
}
Output:
temp, y, sum, n
1 1 1 0
1 1 1 0
1 1 1 0
1 1 1 0
1 1 1 0
1 1 1 0
1 1 1 0
.
.
.
Are you not constantly dividing n by 10?
As n is an integer that starts as 1, and not a float, it would constantly set to 0.
Your second while(n > 0) loop effectively sets n=0 within
the outer for loop.
Did you want to use a second n = temp?
before the last printf statement add: n = temp;
try you code like this:
#include <stdio.h>
//#include <math.h>
int power(int a, int b) {
int c = 1;
for(int i = 0; i < b; i++) {
c *= a;
}
return c;
}
void main(void) {
int sum, y, temp;
printf("temp, y, sum, n\n");
for(int n = 1; n < 100; n++) {
temp = n;
printf("%d ", temp);
y = 0; // y to hold the number of digits of n
while (n > 0) {
n = n / 10;
y++;
}
printf("%d ", y);
n = temp;
sum = 0;
while(n > 0) {
sum += power((n % 10), y);
n = n / 10;
}
if (temp == sum) {
printf("%d ", sum);
}
n = temp;
printf("%d\n", n);
}
}
It is because the value of n is set to 0 outside the loop. Try the below code.
#include <stdio.h>
//#include <math.h>
int power(int a, int b) {
int c = 1;
for(int i = 0; i < b; i++) {
c *= a;
}
return c;
}
void main(void) {
int sum, y, temp;
printf("temp, y, sum, n\n");
for(int n = 1; n < 100; n++) {
temp = n;
printf("%d ", temp);
y = 0; // y to hold the number of digits of n
while (n > 0) {
n = n / 10;
y++;
}
printf("%d ", y);
n = temp;
sum = 0;
while(n > 0) {
sum += power((n % 10), y);
n = n / 10;
}
if (temp == sum) {
printf("%d ", sum);
}
n = temp;
printf("%d\n", n);
}
}
Given some natural numbers n and k, my goal is to write a C program that outputs a number formed by every k-th digit of n. I wrote a program as follows:
#include <stdio.h>
#include <math.h>
#define MAX 100
void printDigit(int n, int k)
{
int arr[MAX];
int i = 0;
int j, r;
while (n != 0) {
r = n % (int)pow(10,k);
arr[i] = r;
i++;
n = n / pow(10,k);
}
for (j = i - 1; j > -1; j--) {
printf("%d ", arr[j]);
}
}
int main()
{
int n = 12345678;
int k = 2;
printDigit(n,k);
return 0;
}
My code outputs the same number but partitioned into substrings of length k. Why is that and how can I fix it so that I get the number I wanted?
Your logic is too complicated, but if you want to stick to it:
int intpow(int x, int y)
{
int result = 1;
while(y--) result *= x;
return result;
}
void printDigit(int n, int k)
{
int arr[MAX];
int i = 0;
int j, r, powv;
while (n != 0) {
powv = intpow(10,k -1);
n /= powv;
if(!n) break;
r = n % 10;
arr[i] = r;
i++;
n = n / 10;
}
for (j = i - 1; j > -1; j--) {
printf("%d ", arr[j]);
}
}
n % (int)pow(10,k) is the low-order k digits of n. If you just want one digit, use n % 10.
Since pow(10, k) returns a double, it might not be an exact integer. You should round it to the nearest integer to do proper division.
void printDigit(int n, int k)
{
int arr[MAX];
int i = 0;
int j, r;
int divisor = lround(pow(10, k));
while (n != 0) {
r = n % 10;
arr[i] = r;
i++;
n = n / divisor;
}
for (j = i - 1; j > -1; j--) {
printf("%d ", arr[j]);
}
}
How to integrate the for loop with the existing function or write a new function that runs from 0 to n where n is the input from user?
//Write a program in C or C++ to Print Fibonacci Series using recursion.
#include <stdio.h>
int fib(int n)
{
if (n == 0 || n == 1)
return n;
else
return fibonacci(n - 1) + fibonacci(n - 2);
}
int main()
{
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
printf("Fibonacci Series: ");
for (int i = 0; i < n; i++)
{
printf("%d", fib(i))
}
return 0;
}
My try:
//Write a program in C or C++ to Print Fibonacci Series using recursion.
#include <stdio.h>
int fib(int n)
{
if (n == 0 || n == 1)
return n;
else
return fib(n - 1) + fib(n - 2);
}
void rec(int n)
{
if(n>0)
rec(n-1);
else
printf("%d", fib(n));
}
int main()
{
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
printf("Fibonacci Series: ");
// for (int i = 0; i < n; i++)
// {
// printf("%d", fib(i))
// }
rec(n);
return 0;
}
This took my input correctly but output was 0.
I know I can print the Fibonacci series in a single function using static int or global variables but I am not allowed to use them. So is there any other way to print the Fibonacci series using recursion in a single function? If not, that's fine, 2 different recursive functions also works.
The code to print fibonacci series using recursion and static int variables is:
void fib(int n)
{
static int n1 = 0, n2 = 1, n3;
if (n > 0)
{
n3 = n1 + n2;
n1 = n2;
n2 = n3;
printf("%d ", n3);
fib(n - 1);
}
}
int main()
{
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
printf("Fibonacci Series: ");
printf("0 1 ");
fib(n - 2);
return 0;
}
UPDATE: Figured out the correct answer, thanks for replies. The code for this task is:
//Write a program in C to Print Fibonacci Series using recursion.
#include <stdio.h>
int fib(int n)
{
if (n == 0 || n == 1)
return n;
else
return fib(n - 1) + fib(n - 2);
}
void rec(int n)
{
if (n >= 0)
{
rec(n - 1);
printf("%d ", fib(n));
}
}
int main()
{
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
printf("Fibonacci Series: ");
// for (int i = 0; i < n; i++)
// {
// printf("%d ", fib(i));
// }
rec(n-1);
return 0;
}
I think you've almost done. Use this recursion
#include<stdio.h>
int rec(int n, int a = 0, int b = 1){
if (n == 0)
return a;
if (n == 1)
return b;
return rec(n - 1, b, a + b);
}
int main(){
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
printf("Fibonacci Series: ");
for (int i = 0; i < n; i++)
{
printf("%d", rec(i));
}
return 0;
}
I have make this program that calculates number factorization such as 60 = 2^2 * 5 * 3.
How can i modify my code in order to print POWERFUL NUMBERS such as 9000 = 2^3 * 3^2 * 5^3 without using math.h library and without using arrays?
Thank you very much!!
#include<stdio.h>
#define MAX 1000
int main(){
int num;
int counter;
int number;
char factorizationOutput;
int isAchiles = 0;
int factor=2;
for(counter=2;counter<=MAX;counter++){
isAchiles = 1;
number=counter;
int factor=2;
while(factor<number){
int power=0;
if(number%factor==0){
while(number%factor==0){
number=number/factor;
power++;
}
if(power == 1){
isAchiles = 0;
}
printf("%d^%d",factor,power);
if(number!=1)
printf(" X ");
}
factor++;
}
if(number!=1)
printf("%d^1.\n",factor);
if(isAchiles == 1){
printf("factorazation of number %d is: ",counter);
}
}
}
#include<stdio.h>
int main(void)
{
int n;
scanf("%d", &n);
printf("%d = ", n);
for(int i = 1; i <= n; i++)
{
int count = 0;
for(int j = 1; j <= i; j++)
{
if(i % j == 0)
{
count++;
}
}
int l = 0;
if(count == 2)
{
while(n % i == 0)
{
l++;
n = n/i;
}
printf("%d^%d*", i, l);
}
}
}