Do you know you can solve mathematical concept using c programming ?

in hive-175254 •  2 years ago 

I took a break and didn't shared anything about coding yesterday but i am here with another coding activity done by myself.

Printing factors input by the user is what my task is for today. Let us see how it can be done in c programming language.

Below is the program i wrote.

Screenshot 2022-12-22 200812.png

Same can be copied from below :

#include <stdio.h>

int main(void) {
int n, i;

// Get the number from the user
printf("Enter a number: ");
scanf("%d", &n);

// Find and print the factors of the number
printf("The factors of %d are: ", n);
for (i = 1; i <= n; i++) {
if (n % i == 0) {
printf("%d ", i);
}
}
printf("\n");

return 0;
}

This program prompts the user to enter a number, and then it uses a loop to find and print all of the factors of that number. The loop starts at 1 and goes up to the number itself, checking if each number is a factor by using the modulus operator (%) to check if the number is divisible by i with no remainder. If the number is divisible, it is printed as a factor.

Here is an example of how the program might be used:

Screenshot 2022-12-22 200956.png

The user enters the number 12, and the program prints out all of the factors of that number: 1, 2, 3, 4, 6, and 12.

I hope this will help understand the program well. Try out a program yourself and play with it.

Let us see what will happen if user inputs a negative number.

Screenshot 2022-12-22 201140.png

Note that the factors of a negative number and the factors of its corresponding positive number are the same, except that they are all negative. For example, the factors of 12 are 1, 2, 3, 4, 6, and 12, and the factors of -12 are -1, -2, -3, -4, -6, and -12.

If you want to handle negative numbers in a different way, you can add an additional check to the program to handle them differently. For example, you could modify the program to only consider positive factors by adding an if statement to skip any negative values of i :

Screenshot 2022-12-22 201236.png

This would allow the program to only consider positive factors when finding the factors of a negative number.

If you also want to learn coding then find your teacher according to your language. For me i am comfortable with hindi language and follow hindi teachers.

You can refer to my teacher below in case you are also comfortable with hindi.

Source

I will soon call myself a trader and a small programmer together.

Happy trading and keep learning what you love.

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!