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;
}
Related
Here is my code so far for producing Fibonacci numbers. I keep getting a zero because it it adding a space bar at the very end, but I need a space between numbers.
#include<stdio.h>
void fib(int);
int main()
{
int n = 0;
while (n <= 0 || n > 70)
{
printf("enter the value(between 0 and 70)");
scanf("%d", &n);
if (n <= 0 || n >= 70)
printf("wrong input\n");
}
fib(n);
return 0;
}
void fib(int n)
{
/* Declare an array to store Fibonacci numbers. */
int f[n]; // 1 extra to handle case, n = 0
int i;
/* 0th and 1st number of the series are 0 and 1*/
f[0] = 0;
f[1] = 1;
printf("%d %d ", f[0], f[1]);
for (i = 2; i < n; i++)
{
f[i] = f[i - 1] + f[i - 2];
printf("%d ", f[i]);
}
}
Remove the trailing space in the first printf and print the space before instead of after the number in the loop.
printf("%d %d", f[0], f[1]);
for (i = 2; i < n; i++) {
f[i] = f[i - 1] + f[i - 2];
printf(" %d", f[i]);
}
I want to create a CountDownUP program but the code is not working, I want it to output 5 4 3 2 1 0 1 2 3 4 5 but it only outputs 5 4 3 2 1 0 1
void countDownUp (unsigned int k){
printf("\n");
for (int i = 0; i < k; i=1){
k = k - i;
printf("%d ", k);
}
for (int n = 0; n <= k; n++){
printf("%d ",n);
}
}
int main(){
int num;
fflush(stdout);
scanf("%d", &num);
countDownUp (num);
return 0;
}
The first for loop decrements k each time through the loop, and stops when k == 1. So the second loop just iterates from 0 to 1.
You shouldn't modify k in the first loop, you should decrement i.
for (int i = k; i > 1; i--) {
printf("%d ", i);
}
Just for fun, you can do it using one for with:
for(int i = k; i >= -k; i--)
if(i <= -1)
printf("%d ", -i);
else
printf("%d ", i);
i have to do.
instruction:
Write a C program which reads a number n from the user and in decreasing order
prints the numbers between 1 and n (included) that are divided by 3 and at the same time
are not divided by 2 and 5. Print also their summation.
if n is 100
expected output:
int main(void)
{
int i, j;
printf("Enter a number: ");
scanf("%d", &i);
for(j = i; j > 0; j--)
if (j % 3 == 0)
if (j % 2 != 0)
if (j % 5 != 0)
printf("%d ", j);
return 0;
}
i have to sum all of them.
Your code produces the correct output, but does not compute the sum.
It is easy to do this: add a variable sum initialized to 0 and increment it by the numbers that you print.
Here is a modified version:
#include <stdio.h>
int main(void) {
int i, j;
long sum = 0; // use type long to avoid overflow on large values.
printf("Enter a number: ");
if (scanf("%d", &i) == 1) {
for (j = i; j > 0; j--) {
if (j % 3 == 0) {
if (j % 2 != 0) {
if (j % 5 != 0) {
printf("%d ", j);
sum += j;
}
}
}
}
printf("\n\nthe sum is %ld\n", sum);
}
return 0;
}
Output:
Enter a number: 100
99 93 87 81 69 63 57 51 39 33 27 21 9 3
the sum is 732
Note that the nested if statements can be combined into a single test expression:
if (j % 3 == 0 && j % 2 != 0 && j % 5 != 0)
Here is a more obscure version with a single modulo operator per iteration and a lot less of them:
#include <stdio.h>
int main(void) {
int i, j;
long sum = 0; // use type long to avoid overflow on large values.
printf("Enter a number: ");
if (scanf("%d", &i) == 1) {
for (j = i / 3; j > 0; j--) {
if (650 >> j % 10 & 1) {
printf("%d ", j * 3);
sum += j * 3;
}
}
printf("\n\nthe sum is %ld\n", sum);
}
return 0;
}
my code works thanks for the all answers. i promise i will try harder before ask a question. Thanks for every little help.
#include <stdio.h>
int main(void)
{
int i, j;
long sum = 0;
printf("Enter a number: ");
scanf("%d", &i);
for(j =i; j >0; j--)
if (j % 3 == 0)
if (j % 2 != 0)
if (j % 5 != 0)
{
printf("%d ", j);sum += j;
}
printf ("\n\nthe sum is: %ld\n", sum);
return 0;
}
hey look at this code may help you
int main(void){int i, j;printf("Enter a number: ");scanf("%d", &i);for(j = i; j > 0; j--)
if (j % 3 == 0 || j % 2 != 0|| j % 5 != 0) printf("%d", j);
return 0;}
I want here to implement the value of array[i][j] into itself, but firstly I have to check if it is in range between example: -99 and 99. If the input is out of these boundaries, it should stop the program.
I tried it with a do-while loop and just now I tried while loop.
#include <stdio.h>
#include <stdlib.h>
int main(){
int array[2][2], i, n, j;
/*
do
{
printf("Value= ");
scanf("%d", &n);
array[i][j] = n;
i++;
j++;
}
while(n < 99 && n > -99);
*/
while(array[i][j] < 99 && array[i][j] > -99){
for(i = 0; i < 2; ++i){
for(j = 0; j < 2; ++j){
printf("Value= ");
scanf("%d", &array[i][j]);
}
}
}
// Print the result
for(i = 0; i < 2; ++i){
for(j = 0; j < 2; ++j){
printf("\n[%d][%d]: ", array[i][j]);
}
}
}
I got a endless loop which doesn't exit if the value is incorrect (out of these boundaries).
Try this, tested and works:
int *array, i, val, n, m;
printf("Put in array size in the form of n-m where n is number of rows and m is number of columns: ");
scanf("%d-%d", &n, &m);
array = (int *) malloc(sizeof(int) * n * m);
i = 0;
while (i < n * m) {
printf("Value for row: %d, column: %d: ", i / m + 1, i % m + 1);
scanf("%d", &val);
if (val > 99 || val < -99) continue;
*(array + i) = val;
i++;
}
for (i = 0; i < n * m; i++) {
if (i > 0 && i % n == 0) printf("\n");
printf("%d\t", *(array + i));
}
free(array);
Without pointers (Variable sized arrays does not work on C90, needs newer standard, or you may use fixed sized arrays):
int i, val, n, m;
printf("Put in array size in the form of n-m where n is number of rows and m is number of columns: ");
scanf("%d-%d", &n, &m);
int array[n][m];
i = 0;
while (i < n * m) {
printf("Value for row: %d, column: %d: ", i / m + 1, i % m + 1);
scanf("%d", &val);
if (val > 99 || val < -99) continue;
array[i / m][i % m] = val;
i++;
}
for (i = 0; i < n * m; i++) {
if (i > 0 && i % n == 0) printf("\n");
printf("%d\t", array[i / m][i % m]);
}
In your array undefeated values, in the first while you try to check random number in your memory. And so your i j will be random number.
Put if statement in your for loop to check the value in array, and delete while loop
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.