Skip to main content

Featured

Mastering Hollow Square Patterns in C: Stars, Numbers, Alphabets & Binary

πŸ”’ C Program to Print Hollow Continuous Number Square πŸ“„ Source Code: #include <stdio.h> int main() { int num, k = 0; printf("Enter the number:\n"); scanf("%d", &num); for(int i = 1; i <= num; i++) { for(int j = 1; j <= num; j++) { if(i == 1 || i == num || j == 1 || j == num) { // k increments sequentially only along the borders printf("%d ", k++); } else { printf(" "); } } printf("\n"); } return 0; } πŸ“‹ Copy Code πŸ’» Expected Output (Input: 5): Enter the number: 5 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 πŸ”’ C Program to Print Standard Hollow Binary Row Square πŸ“„ Source Code (Fixed Specifier): #include <stdio.h> int main() { ...

Add One to Number Represented by Array In C

Add One to Number Represented as Array in C

➕ Add One to Number Represented by Array

#include <stdio.h>

int main()
{
    int size;
    // printf("Enter a array size: ");
    scanf("%d", &size);
    
    int a[size];
    // printf("Enter a array elements: ");
    for (int i = 0; i < size; i++)
    {
        scanf("%d", &a[i]);
    }

    int num = 0;
    for (int i = 0; i < size; i++)
    {
        num = num * 10 + a[i];
    }

    num += 1;
    printf("\nNumber after adding 1: %d\n", num);

    int temp = num;
    int result[10];
    int index = 0;

    while (temp > 0)
    {
        result[index++] = temp % 10;
        temp /= 10;
    }

    printf("Result in array: ");
    for (int i = index - 1; i >= 0; i--)
    {
        printf("%d ", result[i]);
    }
    printf("\n");

    return 0;
}
  

πŸ“˜ Explanation:

πŸ”Ή This program reads digits of a number into an array.
πŸ”Ή Combines those digits to form the actual number using positional multiplication.
πŸ”Ή Adds 1 to the number.
πŸ”Ή Breaks the result back into digits and stores them in another array.
πŸ”Ή Finally, it prints the updated digits from the result array.

πŸ§ͺ Sample Output:

Input:
5
1 2 3 4 5

Output:
Number after adding 1: 12346
Result in array: 1 2 3 4 6
    

🏷️ Keywords:

add one to number in array, C program number to array, digit manipulation in C, array math in C, convert array to integer, beginner C array program

Comments

Popular Posts

πŸŒ™