echo '' ;

C Examples – Addition

In this “C Examples Addition” example, we’ll explore a simple C program that demonstrates addition. Addition is one of the fundamental arithmetic operations, and understanding how to perform it in C can be useful for building more complex programs.
Our program will prompt the user to enter two integers, then it will add them together and display the result. We’ll achieve this using basic input/output functions (printf() and scanf()) for user interaction and a simple loop to perform the addition.
Let’s dive into the code and understand how it works!

uses of addition in real-time scenarios:

Real-Time ScenarioUse of Addition
Accounting and FinanceCalculating profits, expenses, taxes, and budgets
ShoppingCalculating total cost of items
Cooking and RecipesMeasuring ingredients, adjusting recipes
TimekeepingAdding durations, calculating future events
Distance and TravelCalculating distances traveled
Inventory ManagementTracking stock levels, incoming/outgoing shipments
Sports and FitnessKeeping track of scores, points, statistics
Construction and EngineeringCalculating measurements, materials needed
Temperature and WeatherCalculating temperature changes, averages
GamingScoring, calculating points, determining outcomes

Addition Two Integers

Code:

#include <stdio.h>

int main() {
    int first_value, second_value, sum;

    printf("Enter two integers: ");
    // Two integers entered by the user are stored using scanf() function
    scanf("%d %d", &first_value, &second_value);

    // Sum of two numbers is stored in the variable sum
    sum = first_value + second_value;

    // Display sum
    printf("%d + %d = %d", first_value, second_value, sum);

    return 0;
}

Output:

Enter two integers: 50
4
50 + 4 = 54
--------------------------------
Process exited after 6.697 seconds with return value 0
Press any key to continue . . .

Explanation:

Certainly! Here’s a breakdown of the provided C code along with explanations for each part:

#include <stdio.h>

This line includes the standard input-output header file, which provides functions like printf() and scanf().

int main() {

This line marks the beginning of the main() function, which is the entry point of the program.

    int first_value, second_value, sum;

These lines declare three integer variables: first_value, second_value, and sum. These variables will store the two integers entered by the user and their sum, respectively.

    printf("Enter two integers: ");

This line prints a message prompting the user to enter two integers.

    scanf("%d %d", &first_value, &second_value);

This line reads two integers from the user using the scanf() function. The %d format specifier indicates that the input should be treated as integers. The & operator is used to get the memory addresses of first_value and second_value variables for storing the input values.

    sum = first_value + second_value;

This line calculates the sum of the two input integers and assigns the result to the sum variable.

    printf("%d + %d = %d", first_value, second_value, sum);

This line prints the sum of the two input integers using the printf() function. The format string %d + %d = %d specifies the format in which the values will be printed. The variables first_value, second_value, and sum are substituted into the format string to complete the message.

    return 0;

This line indicates that the program execution was successful and returns an exit status of 0 to the operating system. It’s a common convention to return 0 from main() to indicate successful execution.

}

This line marks the end of the main() function.

This code, when executed, prompts the user to enter two integers, calculates their sum, and then prints the result.


Addition two integers without + operator

Code:

#include<stdio.h>

void main() {
    int a, b, i;
    printf("Enter the Two values to add\n");
    scanf("%d%d", &a, &b);
    
    for (i = 1; i <= b; i++) {
        a++;
    }
    
    printf("The added given value is %d", a);
}

Output:

Enter the Two values to add
6
8
The added given value is 14
--------------------------------
Process exited after 6.318 seconds with return value 27
Press any key to continue . . .

Code Explanation

Sure, let’s break down the provided C code:

#include<stdio.h>

This line includes the standard input-output header file, which is necessary for using input and output functions like printf() and scanf().

void main() {

This line declares the main() function, which serves as the entry point of the program. The void keyword indicates that the function does not return any value.

    int a, b, i;

These lines declare three integer variables: a, b, and i. These variables will be used to store the input values and for looping.

    printf("Enter the Two values to add\n");

This line prints a message prompting the user to enter two values.

    scanf("%d%d", &a, &b);

This line reads two integers from the user input and stores them in variables a and b. %d is the format specifier for integers. & is the address-of operator, used to get the memory addresses of variables a and b.

    for (i = 1; i <= b; i++) {
        a++;
    }

This loop runs b times, incrementing the value of a by 1 in each iteration. Essentially, this loop simulates the addition of b to a.

    printf("The added given value is %d", a);

This line prints the result of the addition, which is stored in variable a.

}

This line marks the end of the main() function.

Overall, this program takes two integers as input, adds the second integer to the first one using a loop, and then prints the result.


Example: Understanding of Addition in char type

What will happens if we add one maximum value with any other value in an unsigned character type?

Code:

#include<stdio.h>
void main()
{
	unsigned char a;
	
	a =255 + 1;
	printf("255 + 1 = %d.\n",a);
	
	a =255 + 2;
	printf("255 + 2 = %d.\n",a);
	
	a =255 + 3;
	printf("255 + 3	= %d.\n",a);
	
	a =255 + 4;
	printf("255 + 4 = %d.\n",a);
	
	a =255 + 255;
	printf("255 + 255 = %d.\n",a);
	
	a =255 + 254;
	printf("255 + 254 = %d.\n",a);
	
	a =255 + 253;
	printf("255 + 253 = %d.\n",a);
	
	a =255 + 0;
	printf("255 + 0	= %d.\n",a);
}

Output:

255 + 1 = 0.
255 + 2 = 1.
255 + 3 = 2.
255 + 4 = 3.
255 + 255 = 254.
255 + 254 = 253.
255 + 253 = 252.
255 + 0 = 255.

--------------------------------
Process exited after 1.1 seconds with return value 15
Press any key to continue . . .

Explanation:

DecimalBinaryOutput
255 + 1 = 0.11111111 + 00000001 = 00000001 00000000(0) – 0b00000000
255 + 2 = 1.11111111 + 00000010 = 00000001 00000001(1) – 0b00000001
255 + 3 = 2.11111111 + 00000011 = 00000001 00000010(2) – 0b00000010
255 + 4 = 3.11111111 + 00000100 = 00000001 00000011(3) – 0b00000011
255 + 255 = 254.11111111 + 11111111 = 00000001 11111110(254) – 0b11111110
255 + 254 = 253.11111111 + 11111110 = 00000001 11111100(253) – 0b11111100
255 + 253 = 252.11111111 + 11111101 = 00000001 11111011(252) – 0b11111011
255 + 0 = 255.11111111 + 00000000 = 00000000 11111111(255) – 0b11111111

C Program for Sum of the Array element

Code:

#include<stdio.h>
int main()
{
    int num_array[10];
    int i,sum=0;
    int *ptr;
    printf("\n Enter 10 Elements");
    for(i=0; i<=9; i++)
        scanf("%d", &num_array[i]);
    
    for(i=0; i<=9; i++)
        sum = sum + num_array[i];
    printf("\n ****** \nSum of the arrays's 10 element : %d", sum);
    return 0;
}

Output:

// gcc version 4.6.3
   
/*
 Enter 10 Elements 1
 1
 1
 1
 1
 1
 1
 1
 1
 1
Sum of the arrays's 10 element : 10
*/

Next:

https://aruneworld.com/programming-language/c/c-examples/

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from ArunEworld

Subscribe now to keep reading and get access to the full archive.

Continue reading