Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 months ago.
Improve this question
#include <stdio.h>
int main() {
int num;
printf("Enter the number you want multiplication table of:\n");
scanf("%d", &num);
printf("multiplication table of %d is as follow:\n", num);
//printf("%d X 1 = %d\n", num, num * 1);
//printf("%d X 2 = %d\n", num, num * 2);
//printf("%d X 3 = %d\n", num, num * 3);
//printf("%d X 4 = %d\n", num, num * 4);
//printf("%d X 5 = %d\n", num, num * 5);
//printf("%d X 6 = %d\n", num, num * 6);
//printf("%d X 7 = %d\n", num, num * 7);
//printf("%d X 8 = %d\n", num, num * 8);
//printf("%d X 9 = %d\n", num, num * 9);
//printf("%d X 10 = %d\n", num, num * 10);
//printf("%d X 11 = %d\n", num, num * 11);
//printf("%d X 12 = %d\n", num, num * 12);
//printf("%d X 13 = %d\n", num, num * 13);
//printf("%d X 14 = %d\n", num, num * 14);
//printf("%d X 15 = %d\n", num, num * 15);
//printf("%d X 16 = %d\n", num, num * 16);
//printf("%d X 17 = %d\n", num, num * 17);
//printf("%d X 18 = %d\n", num, num * 18);
//printf("%d X 19 = %d\n", num, num * 19);
//printf("%d X 20 = %d\n", num, num * 20);
for (int i = 1; i < 11; i++) {
printf("%d X %d = %d\n", num, i, num * i);
}
return 0;
}
I need a quick answer.
I am facing a problem in the table multiplication in C. And I have been stuck in it. I'm expecting that I will get the answer of the this problem.
The for loop you wrote is a good approach at simplifying the commented code.
It you want the full table, you should run the loop from i = 1 to i <= 20. You should also check the return value of scanf().
Here is a modified version:
#include <stdio.h>
int main() {
int num;
printf("Enter the number you want multiplication table of:\n");
if (scanf("%d", &num) != 1) {
printf("invalid input\n");
return 1;
}
printf("multiplication table of %d is as follows:\n", num);
for (int i = 1; i <= 20; i++) {
printf("%d X %d = %d\n", num, i, num * i);
}
return 0;
}
Related
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);
}
}
I am trying to show the sum of the prime factors of a given number and
I'm having difficulties displaying the prime factors in my output.
Sample Output:
Input number: 6
Factors are: 1 2 3
Sum of its factor: 1 +2 +3 =6
I am able to show the sum but I want to show the 1+2+3=6 like in the sample above where the factors are 1 2 3.
Can you help me correct my syntax to achieve this? Thanks in advance.
Here's my code:
#include <stdio.h>
int main() {
int i, j, num, isPrime, sum;
printf("Input number: ");
scanf("%d", &num);
printf("Factors are: ", num);
for (i = 1; i <= num; i++) {
if (num % i == 0) {
isPrime = 1;
for (j = 2; j <= i / 2; j++) {
if (i % j == 0) {
isPrime = 0;
break;
}
}
if (isPrime == 1) {
printf("%d ", i);
sum += i;
}
}
}
printf("\nSum of its factor : %d", sum);
return 0;
}
Your code actually has undefined behavior because sum is not initialized to 0. It produces the correct sum only by chance.
You can store the factors in an array, or even construct the expression as you go with sprintf. The maximum length of the expression is not very large as there can be at most 9 different prime factors (29!! > 232)
Here is a modified version:
#include <stdio.h>
int main() {
char expr[9 * 11 + 1];
int i, j, pos, num, isPrime, sum;
printf("Input number: ");
if (scanf("%d", &num) != 1)
return 1;
printf("Factors are: 1"); // always include 1
pos = 0;
expr[pos] = '\0';
sum = 1;
for (i = 2; i <= num; i++) {
if (num % i == 0) {
isPrime = 1;
for (j = 2; j * j <= i; j++) {
if (i % j == 0) {
isPrime = 0;
break;
}
}
if (isPrime == 1) {
pos += sprintf(expr + pos, "+%d", i);
printf(" %d", i);
sum += i;
}
}
}
printf("\nSum of its factors: 1%s = %d\n", expr, sum);
return 0;
}
Output:
Input number: 6
Factors are: 1 2 3
Sum of its factors: 1+2+3 = 6
Here is a more robust and much faster version that does not have undefined behavior for very large values of num:
#include <stdio.h>
int main() {
char expr[9 * 11 + 1];
int i, pos, num;
unsigned sum;
printf("Input number: ");
if (scanf("%i", &num) != 1)
return 1;
printf("Factors are: 1"); // always include 1
pos = 0;
expr[pos] = '\0';
sum = 1;
for (i = 2; num / i >= i; i++) {
if (num % i == 0) {
pos += sprintf(expr + pos, "+%d", i);
printf(" %d", i);
sum += i;
do { num /= i; } while (num % i == 0);
}
}
if (num != 1) {
pos += sprintf(expr + pos, "+%d", num);
printf(" %d", num);
sum += num;
}
printf("\nSum of its factors: 1%s = %u\n", expr, sum);
return 0;
}
Test:
Input number: 0x7fffffff
Factors are: 1 2147483647
Sum of its factors: 1+2147483647 = 2147483648
Since you want to print all the prime factors twice, you should do that in a way so that you can avoid duplicated code. Here is an idea:
#include <stdio.h>
/* Return the smallest prime that is smaller than or equal to n */
/* Assumes that the argument is greater than 1 */
int getFirst(int n)
{
int i;
for(i = 2; i <= n; i++)
if(n % i == 0)
return i;
}
int main()
{
int num, x, tmp, sum=0;
scanf("%d", &num);
tmp = num;
printf("Factors are: ");
while(1) {
x = getFirst(tmp);
printf("%d ", x);
if (x == tmp) /* If we are at the last prime */
break;
tmp /= x;
}
printf("\n");
printf("Sum of factors is: ");
tmp = num;
while(1) {
x = getFirst(tmp);
printf("%d ", x);
sum += x;
if(x == tmp) /* If we are at the last prime */
break;
printf("+ ");
tmp /= x;
}
printf("= %d\n", sum);
}
But as has been pointed out in the comments. 1 is not a prime, and that's why I excluded it.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I am supposed to write a program that reads a positive integer n from the input and outputs the nth element of this series(numbers that are divisible by the sum of their digits.). The first element of this series is 1, and the last is 10^6(First 20 entries: 1; 2; 3; 4; 5; 6; 7; 8; 9; 10; 12; 18; 20; 21; 24; 27; 30; 36; 40; 42;......). The code down works fine, but I am not supposed to use a function. So how can I put the code from the function in the for loop. I am getting some problems and I can't find a solution.
#include <stdio.h>
static int digsum(int n)
{
int sum = 0;
do{
sum += n % 10;
} while (n /= 10);
return sum;
}
int main(void)
{
int n, done, j, temp, temp1,i;
scanf("%d", &temp);
for (i = 1, j = 0; i < 1000; i++) {
if (i % digsum(i) == 0) {
if (j++ <= temp - 1) {
printf("%d ", i);
temp1 = i;
}
}
}
printf("temp1 %d\n", temp1);
return 0;
}
If I input 20, my output needs to be 42.
Try with the below code
#include <stdio.h>
int main(void)
{
int n, done, j, temp, temp1,i,sum,k;
scanf("%d", &temp);
for (i = 1, j=0; i<1000; i++) {
/*Implement the function digsum() right away inside main() and create local variables */
k = i; //Taking a copy of i as in the while() condition check the value changes
sum = 0;
do{
sum += k % 10;
}while (k /= 10);
if(i % sum == 0) {
if (j++ <= temp-1) {
printf("%d ", i);
temp1=i;
}
}
}
printf("temp1 %d\n", temp1);
return 0;
}
I am not sure why you do not want to use the function but here you go:
Place the snippet
n = i;
int sum = 0;
do{
sum += n % 10;
}while (n /= 10);
at the beginning of for loop in main function and change if (i % digsum(i) == 0) to if (i % sum == 0).
int main(void)
{
int n, done, j, temp, temp1, i;
scanf("%d", &temp);
for (i = 1, j=0; i<1000; i++) {
n = i;
int sum = 0;
do{
sum += n % 10;
}while (n /= 10);
if (i % sum == 0) {
if (j++ <= temp-1) {
printf("%d ", i);
temp1=i;
}
}
}
//printf("temp1 %d\n", temp1);
return 0;
}
I was tasked with creating a program that uses 4 different loops to print the same thing 4 times (x, x^2, x^3, x!). The issue I am having is that only one loop seems to run at a time. How can i make all loops run so that my output looks like:
0 0 0 1
1 1 1 1
0 0 0 1
1 1 1 1
0 0 0 1
1 1 1 1
0 0 0 1
1 1 1 1
Below is the code that I have written so far but i cannot get all of the loops to run simultaneously. My output will only print to the screen once, instead of printing 4 times. Can someone look over my code and let me know where i went wrong?
#include<stdio.h>
void main()
{
int i =1 ,num;
//Prompt user for an input
printf("Enter a number: ");
scanf( "%d", &num);
if ( num < 0)
printf("Error: Factorial of negative number doesn't exist.");
//Loop 1
for(i = 1; i <= num; i++)
printf("%d %d %d %d \n", i, (i) * (i), (i) * (i) * (i), factorial(i));
//Loop 2
while(i < num) {
printf("%d %d %d %d \n", i, (i) * (i), (i) * (i) * (i), factorial(i));
i++;
//Loop 3
do {
printf("%d %d %d %d \n", i, (i) * (i), (i) * (i) * (i), factorial(i));
i++;
}while( i < num);
}
//Loop 4 without for, while, do while
if ( i <= num)
printf("%d %d %d %d \n", i, (i) * (i), (i) * (i) * (i), factorial(i));
i++;
}
//Function to calculate a factorial
int factorial(int n)
{
int c;
int result = 1;
for (c = 1; c <= n; c++)
result = result * c;
return result;
}
The program is designed to take a number from a user and run 4 different loops with that number (for, while, do-while, and a constructed loop via if statement).
You need to reset your variable "i"
You have int i = 1;
Then after 1st for loop i will not be <= num;
int i = 1;
for loop...//1
i=1;
while loop...//2
Hi your question isn't as clear as we can get it, however you are searching to run loops in parallel way and this is can be done with threads you can check : https://computing.llnl.gov/tutorials/pthreads/ and i did a simple example without thread if this can help you to complete your work:
#include <stdio.h>
int factorial(int n) {
int c;
int result = 1;
for (c = 1; c <= n; c++)
result = result * c;
return result;
}
void forLoop(int num) {
static int i = 1;
int j;
for(j = 0 ; i <= num && j < 2; i++, j++)
printf("%d %d %d %d \n", i, (i) * (i), (i) * (i) * (i), factorial(i));
}
void whileLoop(int num) {
static int i = 1;
int j = 0;
while(i <= num && j < 2) {
printf("%d %d %d %d \n", i, (i) * (i), (i) * (i) * (i), factorial(i));
i++;
j++;
}
}
void doWhileLoop(int num) {
static int i = 1;
int j = 0;
do {
printf("%d %d %d %d \n", i, (i) * (i), (i) * (i) * (i), factorial(i));
i++;
j++;
} while(i <= num && j < 2);
}
void constructedLoop(int num) {
static int i = 1;
int j = 0;
loop:
if(i <= num && j < 2) {
printf("%d %d %d %d \n", i, (i) * (i), (i) * (i) * (i), factorial(i));
i++;
j++;
goto loop;
}
}
int main() {
int i =1 ,num;
//Prompt user for an input
printf("Enter a number: ");
scanf( "%d", &num);
if ( num < 0){
printf("Error: Factorial of negative number doesn't exist.");
return 0;
}
while(i <= num) {
forLoop(num);
whileLoop(num);
doWhileLoop(num);
constructedLoop(num);
i += 2;
}
return 0;
}
I have created the following program to find factorial factors, but I am not able to understand why the value of i becomes negative after a few iterations.
#include <stdio.h>
int main()
{
int a,b,i;
printf("enter the number: ");
scanf("%d", &a);
printf("entered value is %d\n", a);
for(i = 1; i < a; i++)
{
printf("iterating for a = %d\n", a);
b = a % i;
if(b == 0)
{
printf("%d\n", i);
}
else
{
printf("a = %d, i = %d, modulo = %d\n", b);
}
}
return (0);
}
Fix:
printf("a = %d, i = %d, modulo = %d\n", b);
to
printf("a = %d, i = %d, modulo = %d\n", a, i, b);
Also, your program doesn't find factorial!
b =1;
for(i = 1; i <= a; i++)
b*=i;
printf(" Factorial for a = %d \n", b);
you do not print i in last printf. change it to:
printf("a = %d, i = %d, modulo = %d\n", a, i, b);
No i not become 0. I try this code for 6 and 10 iteration. Its not giving negative value of i. In my case its giving value of i=1298 maybe garbage value. Maybe you are trying more in number of iteration thats why after some iterations negative value of i.