Posts

Showing posts from July, 2020

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...

The #include directive

The #include directive : C programs are divided into modules or functions. Some functions are written by users, likeus, and many others are stored in the C library. Library functions are grouped category-wise and stored in different files known as Header Files . If we want to access the functions stored in the library, it is necessary to tell the compiler about the files to be accessed. This is achieved by using the preprocessor directive #include as follows :                 #include<filename> filename is the name of the library file that contains the required function definition. Pre-processor directives are placed at the beginning of a program.

The #define directive

The #define directive : A #define is a preprocessor compiler directive and not a statement. Therefore #define lines should not end with a semicolon. Symbolic constants are generally written in uppercase so that they are easily distinguished from lowercase variable names. #define instructions are usually placed at the beginning before the main() function. Symbolic constants are not declared in declared in declaration section.

The main function

The main function : The main function is a part of every C program. C Permits different froms of main statement. Following froms are allowed : main()  int main()  void main()  main(void)  void main(void)  int main(void)  The empty pair of parentheses indicates that the function has no arguments. This may be explicitly indicated by using the keyword void inside the parentheses. We may also specify the keyword int or void before the word main. The keyword void means that the function does not return any information to the operating system and int means that the function returns an integer value to the operating system. When int is specified, the last statement in the program must be "return 0".

How to print Hello World! in JAVA language

How to print Hello World! : Let's start by creating a simple program that prints “Hello World” to the screen. class MyClass {     public static void main(String[ ] args) {         System.out.println("Hello World");     } } In Java, every line of code that can actually run needs to be inside a class. In our example, we named the class MyClass. You will learn more about classes in the upcoming modules. In Java, each application has an entry point, or a starting point, which is a method called main. Along with main, the keywords public and static will also be explained later. The main Method To run our program, the main method must be identical to this signature: public static void main(String[ ] args) -> public: anyone can access it -> static: method can be run without creating an instance of the class containing the main method -> void: method doesn't return any value -> main: the name of the method For examp...

How to print Hello World! in C language

How to print Hello World! As when learning any new language, the place to start is with the classic "Hello World!" program: #include <stdio.h> int main() {     printf("Hello, World!\n");     return 0; } Let's break down the code to understand each line: #include <stdio.h> The function used for generating output is defined in stdio.h. In order to use the printf function, we need to first include the required file, also called a header file. int main() The main() function is the entry point to a program. Curly brackets { } indicate the beginning and end of a function (also called a code block). The statements inside the brackets determine what the function does when executed. The printf function is used to generate output: printf(); Here, we pass the text "Hello World!" to it. The \n escape sequence outputs a newline character. Escape sequences always begin with a backslash \. The semicolon ; indicates the end of the...

Variables in C Programming Language

Variables in C : A variable is a name for an area in memory. The name of a variable (also called the identifier) must begin with either a letter or an underscore and can be composed of letters, digits, and the underscore character. Variable naming conventions differ, however using lowercase letters with an underscore to separate words is common (snake_case). Variables must also be declared as a data type before they are used. The value for a declared variable is changed with an assignment statement. For example, the following statements declare an integer variable my_var and then assigns it the value 42: int my_var; my_var = 42; You can also declare and initialize (assign an initial value) a variable in a single statement: int my_var = 42; Let's define variables of different types, do a simple math operation, and output the results: #include <stdio.h> int main() {     int a, b;     float salary = 56.23;     char letter = 'Z'...

Data type in C Programming Language

Data Types in C : C supports the following basic data types: int: integer, a whole number. float: floating point, a number with a fractional part. double: double-precision floating point value. char: single character. The amount of storage required for each of these types varies by platform. C has a built-in sizeof operator that gives the memory requirements for a particular data type. For example: #include <stdio.h> int main() {     printf("int: %ld \n", sizeof(int));     printf("float: %ld \n", sizeof(float));     printf("double: %ld \n", sizeof(double));     printf("char: %ld \n", sizeof(char));     return 0; } The program output displays the corresponding size in bytes for each data type. The printf statements in this program have two arguments. The first is the output string with a format specifier (%ld), while the next argument returns the sizeof value. In the final output, the %ld (for long decimal) ...

Introduction of C programming language

C is a general-purpose programming language that has been around for nearly 50 years. C has been used to write everything from operating systems (including Windows and many others) to complex programs like the Python interpreter, Git, Oracle database, and more. The versatility of C is by design. It is a low-level language that relates closely to the way machines work while still being easy to learn.

Constants in C Programming Language

Constants in C : A constant stores a value that cannot be changed from its initial assignment. By using constants with meaningful names, code is easier to read and understand. To distinguish constants from variables, a common practice is to use uppercase identifiers. One way to define a constant is by using the const keyword in a variable declaration: #include <stdio.h> int main() {     const double PI = 3.14;     printf("%f", PI);     return 0; } The value of PI cannot be changed during program execution. For example, another assignment statement, such as PI = 3.141 will generate an error. Another way to define a constant is with the #define preprocessor directive. The #define directive uses macros for defining constant values. For example: #include <stdio.h> #define PI 3.14 int main() {     printf("%f", PI);     return 0; } Before compilation, the preprocessor replaces every macro identifier i...

Introduction of the Java Programming Language

Welcome to Java! Java is a high level, modern programming language designed in the early 1990s by Sun Microsystems, and currently owned by Oracle. Java is Platform Independent, which means that you only need to write the program once to be able to run it on a number of different platforms! Java is portable, robust, and dynamic, with the ability to fit the needs of virtually any type of application. More than 3 billion devices run Java. Java is used to develop apps for Google's Android OS, various Desktop Applications, such as media players, antivirus programs, Web Applications, Enterprise Applications (i.e. banking), and many more!