C Online Compiler
Example: Find Multiples Up To N (Using Function) in C
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// Find Multiples Up To N (Using Function) #include <stdio.h> // Function to find and print multiples of a given number up to a limit void findAndPrintMultiples(int num, int lim) { if (num <= 0 || lim <= 0) { printf("Error: Both the number and limit must be positive.\n"); return; } if (num > lim) { printf("Error: The number (%d) cannot be greater than the limit (%d).\n", num, lim); return; } printf("Multiples of %d up to %d: ", num, lim); for (int i = num; i <= lim; i += num) { printf("%d ", i); } printf("\n"); } int main() { // Step 1: Declare variables for the number and the limit int userNumber, userLimit; // Step 2: Prompt user for input printf("Enter the integer whose multiples you want to find: "); scanf("%d", &userNumber); printf("Enter the upper limit (N): "); scanf("%d", &userLimit); // Step 3: Call the function to find and print multiples findAndPrintMultiples(userNumber, userLimit); return 0; }
Output
Clear
ADVERTISEMENTS