Simple arithmetic calculations in C

By: Jeffrey Emailed: 1598 times Printed: 2044 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

This simple C program shows simple arithmetic calculations.

#include <stdio.h>
// main() function…
int main( )
{
    // variables declaration
    int  x, y, z;
    // variables initialization, assign 20 to variable x…
    // or put the value of 20 in memory location labeled by x
    x = 20;
    y = 2;
    printf("Given x = 20, y = 2\n");
    printf("\nx / y = %d", x / y);
    // do some basic calculations
    x = x * y;
    y = y + y;
    printf("\nx = x * y");
    printf("\ny = y + y");
    printf("\nNew value for x / y = %d", x / y);
    z = 20 * y / x;
    printf("\nz = 20 * (y / x) = %d\n", z);
   return 0;
}


C Home | All C Tutorials | Latest C Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(2)


1. View Comment

Dear Gurus, I have arrived at your site lloking for an understanding of scanf and printf. To my horror at first sight the assosciated characters d i o u x c s e,f,g % are used with the assumption that to discuss them and how to use them is so basic that they are not discussed.
In embedded applications e.g. pic uCs I program quite well in integer arithmetic. This of course has a big problem when you want decimals. Eg. 355/113 is a good approximation to PI. There are work arounds to preserve the decimals, but C as used in uC would seem to be a better way.
Which brings me back to the C language and scanf, printf....
where do I find an article which step by step shows the usage of the formaters , hopefully with examples?
Bye and thanks
Fred
Madrid


View Tutorial          By: Fred Mah at 2012-09-22 07:40:27
2. View Comment

Hi Fred, I found this link which has more explanation on it. http://www.cdf.toronto.edu/~ajr/209/notes/printf.html

View Tutorial          By: Boulvat at 2012-09-25 08:36:48

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Jeffrey
Command-line arguments in C
Simple arithmetic calculations in C
Find square and square root for a given number in C
Calculating total based on the given quantity and price in C++
The Object (compound) Type in PHP

More Tutorials in C
Sum of the elements of an array in C
Printing a simple histogram in C
Sorting an integer array in C
Find square and square root for a given number in C
Simple arithmetic calculations in C
Command-line arguments in C
Calculator in C
Passing double value to a function in C
Passing pointer to a function in C
Infix to Prefix And Postfix in C
while, do while and for loops in C
Unicode and UTF-8 in C
Formatting with printf in C
if, if...else and switch statements in C with samples
Statements in C

More Latest News
Most Viewed Articles (in C )
Using memset(), memcpy(), and memmove() in C
UNIX read and write system calls sample program in C
Constants and escape sequences in C
A Storage Allocator sample program in C
ERRNO.H Header File in C
Formatting with printf in C
Calculator in C
Passing double value to a function in C
Printing a simple histogram in C
Getting Started with C
File Copying in C
Pointers and Arrays in C
Address Arithmetic and pointers in C
Table Lookup - hashtab - example program in C
Using printf function in C
Most Emailed Articles (in C)
Conditional Expressions in C
fgets(), fputs() - Line Input and Output - sample program in C
Symbolic Constants using #define in C
#if, #elif, #ifndef, #ifdef in C (Conditional Inclusion)
Address Arithmetic and pointers in C
Character Pointers and Functions in C
Initialization of Pointer Arrays in C
Pointers to Structures example program in C
Using printf function in C
A Storage Allocator sample program in C
Using free() Function in C
The C Character Set
Writing The First C program
Formatting with printf in C
while, do while and for loops in C