Wednesday, 27 March 2019

Switch Statement in C

Switch case statements are a substitute for long if statements that compare a variable to several integral values. A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each switch case.

Syntax:
switch(expression){
    case  value1 :              // code to be executed if expression = value1;
              statement 1;
               ……………
              ………………
              Break;//optional

    case  value2 :     // code to be executed if expression = value2;
              statement 1;
               ……………
              ………………
              Break;//optional

    case  value3 :   //// code to be executed if expression = value3;
              statement 1;
               ……………
              ………………
              Break;//optional

    case  value4 :      // code to be executed if expression = value4;
              statement 1;
               ……………
              ………………
              Break;//optional

    case  value5 :        // code to be executed if expression = value5;
              statement 1;
               ……………
              ………………
              Break;//optional

    case  valueN :  //// code to be executed if expression = valueN;
              statement 1;
               ……………
              ………………
              Break;//optional

  Default :   // code to be executed if value of expression doesn't match any cases
             Statement1;
            ………………………
           
}

Important Points about Switch Case Statements:
  1. The expression provided in the switch should result in a constant value otherwise it would not be valid.
  2. Duplicate case values are not allowed.
  3. The default statement is optional.
  4. The break statement is used inside the switch to terminate a statement sequence. When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement.
  5. The break statement is optional. If omitted, execution will continue on into the next case. The flow of control will fall through to subsequent cases until a break is reached.
  6. The constant-expression for a case must be the same data type as the variable in the switch, and it must be a constant or a literal.
  7. When the variable being switched on is equal to a case, the statements following that case will execute until a break statement is reached.
  8. A switch statement can have an optional default case, which must appear at the end of the switch. The default case can be used for performing a task when none of the cases is true. No break is needed in the default case.
Example:
//program to create simple calculator
#include<stdio.h>
int main(){
   int num1,num2,result;
   char operator; 
  
   printf("Enter operator(+,-,*,/,%):\n");
   scanf("%c",&operator);
   printf("Enter two positive integers:\n");
   scanf("%d %d",&num1,&num2);

  switch(operator){
    case '+' :
               result=num1+num2;
               break;
    case '-' :
               result=num1-num2;
               break;

    case '*' :
               result=num1*num2;
               break;

    case '/' :
               result=num1/num2;
              
               break;
    case '%' :
               result=num1%num2;
              
               break;
    default :
               printf("Invalid operator!");
  }
   
  printf("Result of %c on %d and %d is %d.\n",operator,num1,num2,result);
  return 0;
}
Output:

Please comment if you find anything incorrect, or you want to improve the topic discussed above.

Tuesday, 26 March 2019

C Program to Check Whether a Character is Vowel or Consonant


This program demonstrates the functioning of if else statements. Program asks user for an alphabet and gives the output whether alphabet is vowel or consonant.
The five alphabets A, E, I, O and U are called vowels. All other alphabets except these 5 vowel letters are called consonants.
//C Program to Check Whether a alphabet is Vowel or Consonant
#include <stdio.h>
int main()
{
    char ch;
    printf("Enter an alphabet: ");
    scanf("%c",&ch);
     if((ch=='a' || ch=='A') || (ch=='e' || ch=='E') || (ch=='i' || ch=='I') || (ch=='o' || ch=='O') || (ch=='u' || ch=='U'))
        printf("%c is a vowel.\n", ch);
    else
        printf("%c is a consonant.\n", ch);
    return 0;
}
Output:



Please comment if you find anything incorrect, or you want to improve the topic discussed above.

C Program to check whether a number is even or odd

This program demonstrates the functioning of if else statements. Program asks user for an integer number and gives the output whether number is even or odd.
An integer is even if it is divisible by 2 meaning if by dividing number by 2, remainder is zero, then number is even otherwise it is an odd number.
//C Program to demonstrate the functioning of if else statements
//check if number is divisible by 2. If it is, then number is even otherwise it is odd number
#include <stdio.h>
int main()
{
    int num;
    printf("Enter an integer: ");
    scanf("%d", &num);
    if(num % 2 == 0)
        printf("%d is even number.\n", num);
    else
        printf("%d is odd number.\n", num);
    return 0;
}
Output:



Please comment if you find anything incorrect, or you want to improve the topic discussed above.

Decision making in C (If else,nested if)


There comes situation when we need to make some decisions based on certain conditions and based on these decision we will perform a particular job.

Decision making statements in programming languages decides the direction of flow of program execution.
Following decision making constructs are available in C:
1.      If statements
2.      If Else statements
3.      Switch Case

In this article, you will learn about if statements, if else statements and nested ifs.

1. if statement
if statement is the most simple decision making statement. It is used to decide whether a certain statement or block of statements will be executed or not i.e if a certain condition is true then a block of statement is executed otherwise not.
Syntax:
if(condition){
  //body of if ; it will be executed if condition in true
}
Example:
#include<stdio.h>
int main(){
 int num=100;
if(num<150){
  printf(“%d is smaller than 150.”);
  }
return 0;
}

2.if else statement
The if statement alone tells us that if a condition is true it will execute a block of statements and if the condition is false it won’t. But what if we want to do something else if the condition is false. Here comes the else statement. We can use the else statement with if statement to execute a block of code when the condition is false.
Syntax:
if(condition){
  //body of if; it will be executed when condition is true
}
else{
  //body of else; it will be executed only when condition is false
}
Example:
//Program to check whether an integer is positive or negative
#include<stdio.h>
int main(){
   int num;
   printf(“Enter an integer value except 0”);
scanf(“%d”,&num);

  if(num>0){
    printf(“%d is a positive integer.”);
  }
  else{
    printf(“%d is a negative integer.”);
   }

return 0;
}

If else ..if else..else statement(if else ladder)
When you have multiple options to check, if else ladder comes into picture.
The if statements are executed from the top down. As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the ladder is bypassed. If none of the conditions is true, then the final else statement will be executed.

Syntax:
if(condition 1){

  }
else if(condition 2){

 }
else if(condition 3){

  }
.
.
.
else if(condition n){

}
.
.
.
else{

}
Example:
#include<stdio.h>
int  main()
{
    int num = 14;
    if (num == 10)
         printf(“%d is equal to 10”,num);
    else if (num == 14)
        printf(“%d is equal to 10”,num);
    else if (num == 20)
        printf(“%d is equal to 10”,num);
    else
        printf(“%d is not present",num);
return 0;
}

Nested if statements
Nested if statements means an if statement inside another if statement.
Syntax:
if(condition 1){
   if(condition 2){

    }
  else{

  }
 }
else{


   }
Example
//Program to find smaller number between two numbers
#include<stdio.h>
int main(){
  int num1,num2;
  printf(“Enter two integer numbers\n”);
  scanf(“%d %d”,&num1,&num2);

  if(num1!=num2){
    if(num1<num2)
      printf(“%d is smaller than %d.”,num1,num2);
    else
      printf(“%d is smaller than %d.”,num2,num1);
    }
  else
     printf(“Both numbers are equal”);

 return 0;
}

Please comment if you find anything incorrect, or you want to improve the topic discussed above

Saturday, 23 March 2019

How to create new database connection in SQL Developer

Oracle SQL Developer is a free, integrated development environment(IDE) that simplifies the development and management of Oracle Database in both traditional and Cloud deployments. SQL Developer offers complete end-to-end development of your PL/SQL applications, a worksheet for running queries and scripts, a DBA console for managing the database, a reports interface, a complete data modeling solution, and a migration platform for moving your 3rd party databases to Oracle.
In this article, we will walk through on creating a new database connection in SQL Developer.
Step 1: Open SQL Developer and Click on + sign on left pane of working window.


Step 2: Once you click on + sign, following popup will come up.


Enter values for following parameters:
Connection Name  :  Meaningful database connection name. I prefer username_databaseName
Username : database user you are going to connect to database
Password : password for above username
Connection Type: choose connection type based on your configuration.
  If connection  type is TNS, Select Network Alias from dropdown list.


If connection type is basic, Enter SID.


Once all the necessary inputs are done, click Test button. If everything is OK, connection should be successful.
Once connection is successful, Click Connect button. Your newly created connection should be available in left pane of working window.



Friday, 22 March 2019

How to automatically shut down computer using Task scheduler(without batch file)

You might forget to shut down your computer while leaving office or your students might forget to shut down computers in lab. In either case, you may want to shut down the computers after they are in use and you know they won’t be in use for a specific period of time. So you plan to shut down all computers at a specific period of time. Here is a simple tutorial to do so.
In this first method, I will do the job without creating a BAT file.

Step 1: Open Task Scheduler. Click on Create Basic Task


Step 2: Give a name to your task say, “ShutdownPC” and press Next button.


Step 3: In next window, Choose Daily option. And click Next button.


Step 4: In next windows, set time when computer should shut down.Leave everything as it is. And press Next button.


Step 5: Now choose action from next windows. In our case, choose first option, Start a program and press Next button.


Step 6: Next window asks to provide program to start . In this case, it is shutdown.exe. After browsing shutdown.exe program and adding ‘/s’ in Add arguments field, click Next button.


Step 7: Next window gives a summary of our scheduled task. Click Finish button.

Done!

In next post, i will post about the same topic but using batch file.