তথ্য ও যোগাযোগ প্রযুক্তি
Number System Conversion:
The number system is a form of expressing the numbers. In number system conversion, we will study to convert a number of one base, to a number of another base. There are a variety of different number systems such as binary numbers, decimal numbers, hexadecimal numbers, octal numbers, which can be exercised.
1×23 + 0x22 + 1×21+ 0x20 + 0x2 -1 + 1×2 -2 = 8+0+2+0+0+0.25 = 10.25
To know more, Please text to our Facebook page
The syntax of the if statement in C programming is:
// statements to be executed if the test expression is true
The if statement evaluates the test expression inside the parenthesis ().
· If the test expression is evaluated to true, statements inside the body of if are executed.
· If the test expression is evaluated to false, statements inside the body of if are not executed.
To learn more about when test expression is evaluated to true (non-zero value) and false (0), check relational and logical operators.
// Program to display a number if it is negative
printf("Enter an integer: ");
// true if number is less than 0
printf("You entered %d.\n", number);
printf("The if statement is easy.");
The if statement is easy.
When the user enters -2, the test expression number<0 is evaluated to true. Hence, You entered -2 is displayed on the screen.
The if statement is easy.
When the user enters 5, the test expression number<0 is evaluated to false and the statement inside the body of if is not executed.
The if statement may have an optional else block. The syntax of the if..else statement is:
// statements to be executed if the test expression is true
// statements to be executed if the test expression is false
How if...else statement works?
If the test expression is evaluated to true,
· statements inside the body of if are executed.
· statements inside the body of else are skipped from execution.
If the test expression is evaluated to false,
· statements inside the body of else are executed
· statements inside the body of if are skipped from execution.
Example : if...else statement
// Check whether an integer is odd or even
printf("Enter an integer: ");
// True if the remainder is 0
printf("%d is an even integer.",number);
printf("%d is an odd integer.",number);
When the user enters 7, the test expression number%2==0 is evaluated to false. Hence, the statement inside the body of else is executed.
The if...else statement executes two different codes depending upon whether the test expression is true or false. Sometimes, a choice has to be made from more than 2 possibilities.
The if...else ladder allows you to check between multiple test expressions and execute different statements.
Syntax of if...else Ladder
else if(test expression2) {
else if (test expression3) {
Example : C if...else Ladder
// Program to relate two integers using =, > or < symbol
printf("Enter two integers: ");
scanf("%d %d", &number1, &number2);
//checks if the two integers are equal.
printf("Result: %d = %d",number1,number2);
//checks if number1 is greater than number2.
else if (number1 > number2) {
printf("Result: %d > %d", number1, number2);
//checks if both test expressions are false
printf("Result: %d < %d",number1, number2);
It is possible to include an if...else statement inside the body of another if...else statement.
Example : Nested if...else
This program given below relates two integers using either <, > and = similar to the if...else ladder's example. However, we will use a nested if...else statement to solve this problem.
printf("Enter two integers: ");
scanf("%d %d", &number1, &number2);
if (number1 >= number2) {
if (number1 == number2) {
printf("Result: %d = %d",number1,number2);
printf("Result: %d > %d", number1, number2);
printf("Result: %d < %d",number1, number2);
If the body of an if...else statement has only one statement, you do not need to use brackets {}.
In programming, a loop is used to repeat a block of code until the specified condition is met.
C programming has three types of loops:
We will learn about for loop in this tutorial. In the next tutorial, we will learn about while and do...while loop.
The syntax of the for loop is:
for (initialization Statement; test Expression; update Statement)
// statements inside the body of loop
· The initialization statement is executed only once.
· Then, the test expression is evaluated. If the test expression is evaluated to false, the for loop is terminated.
· However, if the test expression is evaluated to true, statements inside the body of for loop are executed, and the update expression is updated.
· Again the test expression is evaluated.
This process goes on until the test expression is false. When the test expression is false, the loop terminates.
To learn more about test expression (when the test expression is evaluated to true and false), check out relational and logical operators.
// Print numbers from 1 to 10
1. i is initialized to 1.
2. The test expression i < 11 is evaluated. Since 1 less than 11 is true, the body of for loop is executed. This will print the 1 (value of i) on the screen.
3. The update statement ++i is executed. Now, the value of i will be 2. Again, the test expression is evaluated to true, and the body of for loop is executed. This will print 2 (value of i) on the screen.
4. Again, the update statement ++i is executed and the test expression i < 11 is evaluated. This process goes on until i becomes 11.
5. When i becomes 11, i < 11 will be false, and the for loop terminates.
// Program to calculate the sum of first n natural numbers
// Positive integers 1,2,3...n are known as natural numbers
printf("Enter a positive integer: ");
// for loop terminates when num is less than count
for(count = 1; count <= num; ++count)
Enter a positive integer: 10
The value entered by the user is stored in the variable num. Suppose, the user entered 10.
The count is initialized to 1 and the test expression is evaluated. Since the test expression count<=num (1 less than or equal to 10) is true, the body of for loop is executed and the value of sum will equal to 1.
Then, the update statement ++count is executed and the count will equal to 2. Again, the test expression is evaluated. Since 2 is also less than 10, the test expression is evaluated to true and the body of for loop is executed. Now, the sum will equal 3.
This process goes on and the sum is calculated until the count reaches 11.
When the count is 11, the test expression is evaluated to 0 (false), and the loop terminates.
Then, the value of sum is printed on the screen.
Relationship Between Arrays and Pointers-
An array is a block of sequential data. Let's write a program to print addresses of array elements.
printf("&x[%d] = %p\n", i, &x[i]);
printf("Address of array x: %p", x);
Address of array x: 1450734448
There is a difference of 4 bytes between two consecutive elements of array x. It is because the size of int is 4 bytes (on our compiler).
Notice that, the address of &x[0] and x is the same. It's because the variable name x points to the first element of the array.
From the above example, it is clear that &x[0] is equivalent to x. And, x[0] is equivalent to *x.
· &x[1] is equivalent to x+1 and x[1] is equivalent to *(x+1).
· &x[2] is equivalent to x+2 and x[2] is equivalent to *(x+2).
· Basically, &x[i] is equivalent to x+i and x[i] is equivalent to *(x+i).
printf("Enter 6 numbers: ");
// Equivalent to scanf("%d", &x[i]);
// Equivalent to sum += x[i]
When you run the program, the output will be:
Here, we have declared an array x of 6 elements. To access elements of the array, we have used pointers.
In most contexts, array names decay to pointers. In simple words, array names are converted to pointers. That's the reason why you can use pointers to access elements of arrays. However, you should remember that pointers and arrays are not the same.
There are a few cases where array names don't decay to pointers.
int x[5] = {1, 2, 3, 4, 5};
// ptr is assigned the address of the third element
printf("*ptr = %d \n", *ptr); // 3
printf("*(ptr+1) = %d \n", *(ptr+1)); // 4
printf("*(ptr-1) = %d", *(ptr-1)); // 2
When you run the program, the output will be:
In this example, &x[2], the address of the third element, is assigned to the ptr pointer. Hence, 3 was displayed when we printed *ptr.
And, printing *(ptr+1) gives us the fourth element. Similarly, printing *(ptr-1) gives us the second element.