Posts

Showing posts from August, 2020

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