Posts

Addition 2 number in C Language

Image
Sample Program : Addition of two numbers Consider the program, which performs addition on two numbers and displays the result. The program is following : This program when executed will product the following output : 100 106.10 The first two lines of the program are comment lines. It is a good practice to use comment lines in the beginning to give information such as name of the program, author, date etc. Comment characters are also used in other lines to indicate line numbers. The words number and amount are variable names that are used to store numeric data. The numeric data may be either in integer form or in real form. In C, all variables should be declared to tell the compiler what the variable names are and what type of data they hold. The variables must be declared before they are used. In lines 5 and 6, the declarations : int number; float amount; tell the compiler that number is an integer( int ) and amount is a floating( float...

Comments in JAVA language

Java comments : Single line comments : The purpose of including comments in your code is to explain what the code is doing. Java supports both single and multi-line comments. All characters that appear within a comment are ignored by the Java compiler. A single-line comment starts with two forward slashes and continues until it reaches the end of the line. For example: // this is a single-line comment x = 5; // a single-line comment after code Multi-Line comments : Java also supports comments that span multiple lines. You start this type of comment with a forward slash followed by an asterisk, and end it with an asterisk followed by a forward slash. For example: /*  This is also a     comment spanning     multiple lines */ Note that Java does not support nested multi-line comments. However, you can nest single-line comments within multi-line comments. /* This is a single-line comment:     // a single-line comment  */ ...

Variables in JAVA Language

Java Variables : Variables store data for processing. A variable is given a name (or identifier), such as area, age, height, and the like. The name uniquely identifies each variable, assigning a value to the variable and retrieving the value stored. Variables have types. Some examples: - int: for integers (whole numbers) such as 123 and -456 - double: for floating-point or real numbers with optional decimal points and fractional parts in fixed or scientific notations, such as 3.1416, -55.66. - String: for texts such as "Hello" or "Good Morning!". Text strings are enclosed within double quotes. You can declare a variable of a type and assign it a value. Example: String name = "David"; This creates a variable called name of type String, and assigns it the value "David". Examples of variable declarations: class MyClass {     public static void main(String[ ] args) {         String name ="David";         int age = ...

Basic structure of C program

Image
Basic structure of C program : The examples discussed so far illustrate that a C program can be viewed as a group of building blocks called functions. A function is a subroutine that may include one or more statements designed to perform a specific task. To write C program, we first create functions and then put them together. A C program may contain one or more sections as following : The documentation section consists of a set of comment lines giving the name of the program, the author and other details, which the programmer would like to use later. The link section defines all symbolic constants. There are some variables that are used in more than one function. Such variables are called global variables and are declared in the global declaration section that is outside of all the functions. This section also declares all the use-defined functions. Every C program must have one main() function section. This section contains two parts, d...

Use of Math function

Image
Sample program : use of math function We often use standard mathematical functions such as cos, sin, exp, etc. We shall see now the use of a mathematical function in a program. The standard mathematical functions are defined and kept as a part of C math library . If we want to use any of these mathematical functions, we must add an #include instruction in the program. Like #define , it is also a compiler directive that instructs the compiler to link the specified mathematical functions from the library. The instruction is of the form                 #include < math.h > math.h is the filename containing the required function. The program illustrates the use of cosine function. The program calculates cosine values for angles 0, 10, 20,….., 180 and prints out the results with headings. Output : Another #include instruction that is often required is     ...

Use of Subroutines

Image
Sample program : use of subroutines So far, we have used only printf function that has been provided by the C system. The program shown in following program uses a user-defined function. A function defined by the user is equivalent to a subroutine in FORTRAN or subprogram in BASIC. This program presents a very simple program that uses a mul( ) function. The program will print the following output. Multiplication of 5 and 10 is 50 The  mul( )  function multiplies the values of  x  and  y  and the result is returned to the  main( )  function when it is called in the statement.                  c = mul ( a, b ) ; The mul ( ) has two arguments x and y that are declared as integers. The values of a and b are passed on to x and y respectively when the function mul ( ) is called.

Interest calculation in C Language

Image
Sample program : interest calculation Consider the program, which calculates the value of money at the end of each year of investment, assuming an interest rate of 11 percent and prints the year and the corresponding amount, in two columns. The output is shown in fig 2 for period of 10 years with an initial investment of 5000.00.  The program uses the following formula : Value at the end of year = value at start of year (1+interest rate) In the program, the variable value represents the value of money at the end of the year while amount represents the value of money at the start of the year. The statement                 amount = value ; makes the value at the end of the current year as the value at start of the next year. Let us consider the new features introduced in this program. The second and third lines begin with  #define  instructions. A  #def...