Posts

Comments in C

Comments : Comments are explanatory information that you can include in a program to benefit the reader of your code. The compiler ignores comments, so they have no affect on a program. A comment starts with a slash asterisk /* and ends with an asterisk slash */ and can be anywhere in your code. Comments can be on the same line as a statement, or they can span several lines. For example: #include <stdio.h> /* A simple C program  * Version 1.0  */ int main() {   /* Output a string */   printf("Hello World!");   return 0; } Note :  As you can see, comments clarify the program's intent to the reader. Use comments to clarify the purpose and logic behind segments of code. Single-line Comments :  C++ introduced a double slash comment // as a way to comment single lines. Some C compilers also support this comment style. For example:  #include <stdio.h> int main() {   int x = 42; //int for a who...

Input and Output in C

Input : C supports a number of ways for taking user input. getchar() Returns the value of the next single character input. For example : #include <stdio.h> int main()  {   char a = getchar();   printf("You entered: %c", a);   return 0; } The input is stored in the variable a. The gets() function is used to read input as an ordered sequence of characters, also called a string. A string is stored in a char array. For example :  #include <stdio.h> int main()  {   char a[100];   gets(a);   printf("You entered: %s", a);   return 0; } Here we stored the input in an array of 100 characters. scanf() scans input that matches format specifiers. For example : #include <stdio.h> int main()  {   int a;   printf("Please enter an element : ", a);   scanf("%d", &a);    return 0; } The " &" sign before the variable name is th...

Increment and Decrement Operators in JAVA

Increment Operators :  An increment or decrement operator provides a more convenient and compact way to increase or decrease the value of a variable by one . For example, the statement x=x+1; can be simplified to ++x; Example: int test = 5; ++test; // test is now 6 Decrement Operators :  The decrement operator (--) is used to decrease the value of a variable by one. Example :  int test = 5; --test; // test is now 4 Prefix & Postfix : Two forms, prefix and postfix , may be used with both the increment and decrement operators. With prefix form, the operator appears before the operand, while in postfix form, the operator appears after the operand. Below is an explanation of how the two forms work: Prefix  : Increments the variable's value and uses the new value in the expression. Example of increment operator : int x = 34; int y = ++x; // y is 35 The value of x is first incremented to 35, and is then assigned to y, so the val...

Primitive Data Types

The Math Operators : Java provides a rich set of operators to use in manipulating variables. A value used on either side of an operator is called an operand . For example, in the expression below, the numbers 6 and 3 are operands of the plus operator: int x = 6 + 3; Java arithmetic operators: + addition - subtraction * multiplication / division % modulo Addition : The + operator adds together two values, such as two constants, a constant and a variable, or a variable and a variable. Here are a few examples of addition: int sum1 = 50 + 10; int sum2 = sum1 + 66; int sum3 = sum2 + sum2; Subtraction : The - operator subtracts one value from another. int sum1 = 1000 - 10; int sum2 = sum1 - 5; int sum3 = sum1 - sum2; Multiplication : The * operator multiplies two values. int sum1 = 1000 * 2; int sum2 = sum1 * 10; int sum3 = sum1 * sum2; Division : The / operator divides one value by another. int sum1 = 1000 / 5; int sum...

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