Sunday, March 1, 2020

Intriduction to Java

Java is develope by James Ghosling in 1995. Its first version has realeased in 1996.
latest version is Jdk13.2.0 in 10 sep 2019.


What is Java?

Java is a programming language and a platform. Java is a high level object oriented and secure programming language.
  Generally a language is either compiled or interpreted, but Java is both compiled as well as interpreted. First a Java program is compled and comes in the form of "Java byte code" (Which is intermediate code). Then this Java byte code is run on interpreter to execute a program.

Monday, February 17, 2020

program to shut down or turn off computer

C program to shut down your computer, i.e., to turn off a computer system. System function of "stdlib.h" is used to run an executable file "shutdown.exe" which is present in C:\WINDOWS\system32 folder in Windows 7 & XP. See Windows XP and Linux programs at the bottom of this page.

C program for Windows 7

#include <stdio.h>
#include <stdlib.h>

int main()
{
   system("C:\\WINDOWS\\System32\\shutdown /s");

   return 0;
}
You can use various options while executing "shutdown.exe," for example, you can use /t option to specify the number of seconds after which shutdown occurs.
Syntax: "shutdown /s /t x"; where x is the number of seconds after which shutdown will occur.
By default, shutdown occurs after 30 seconds. To shut down immediately you can write "shutdown /s /t 0". If you wish to restart your computer, then you can use "shutdown /r."

C program for Windows XP

It will ask if you want to shutdown your computer, if you press 'y' then your computer will shutdown in 30 seconds.
#include <stdio.h>
#include <stdlib.h>
int main()
{
   char ch;
   printf("Do you want to shutdown your computer now (y/n)\n");
   scanf("%c", &ch);
   if (ch == 'y' || ch == 'Y')
      system("C:\\WINDOWS\\System32\\shutdown -s");
   return 0;
}
To shutdown immediately use "C:\\WINDOWS\\System32\\shutdown -s -t 0". To restart use "-r" instead of "-s".
If you are using Turbo C Compiler then execute your program from command prompt or by opening the executable file from the folder. Press F9 to build your executable file from source program. When you run program from within the compiler by pressing Ctrl+F9 it may not work.

C program for Ubuntu Linux

#include <stdio.h>
int main() {
  system("shutdown -P now");
  return 0;
}
You need to be logged in as root user for this program to execute otherwise you will get the message shutdown: Need to be rootnow specifies that you want to shut down immediately. '-P' option specifies you want to power off your machine. You can specify minutes as:
shutdown -P "number of minutes."
For more options or help type "man shutdown" in a terminal.

program to get IP address

C program to print IP (Internet Protocol) address of your computer, system function is used to execute the command "ipconfig" which prints IP address, subnet mask, and default gateway. The code given below works for Windows XP and Windows 7. If you are using Turbo C compiler then execute this program from the folder, it may not work when you are working in a compiler and trying to run it by Ctrl + F9.

C programming code

#include<stdlib.h>
int main()
{
   system("C:\\Windows\\System32\\ipconfig");
 
   return 0;
}

Output of program: (In Windows 7)
IP address C program output

program to print date


C program to print current system date. To print date, we will use getdate function.

C programming code (Works in Turbo C only)

#include <stdio.h>
#include <conio.h>
#include <dos.h>
int main()
{
   struct date d;
   getdate(&d);
   printf("Current system date: %d/%d/%d", d.da_day, d.da_mon, d.da_year);
   getch();
   return 0;
}
This code works in Turbo C only because it supports dos.h header file.

program to add two complex numbers

 program to add two complex numbers


C program to add two complex numbers: this program performs addition of two complex numbers which will be entered by a user and then prints it. A user inputs real and imaginary parts of two complex numbers. In our program we will add real parts and imaginary parts of complex numbers and prints the complex number, 'i' is the symbol used for iota. For example, if a user inputs two complex numbers as (1 + 2i) and (4 + 6 i) then the output of the program will be (5 + 8i). A structure is used to store a complex number.

Complex numbers program in C language

#include <stdio.h>
struct complex
{
   int real, img;
};
int main()
{
   struct complex a, b, c;
   printf("Enter a and b where a + ib is the first complex number.\n");
   scanf("%d%d", &a.real, &a.img);
   printf("Enter c and d where c + id is the second complex number.\n");
   scanf("%d%d", &b.real, &b.img);
   c.real = a.real + b.real;
   c.img = a.img + b.img;
   printf("Sum of the complex numbers: (%d) + (%di)\n", c.real, c.img);
   return 0;
}

C program to add two complex numbers output:
Output of C program to add two complex numbers

program to generate random numbers


C program to generate pseudo-random numbers using rand and random function (Turbo C compiler only). As the random numbers are generated by an algorithm used in a function they are pseudo-random, this is the reason that word pseudo is used. Function rand() returns a pseudo-random number between 0 and RAND_MAX. RAND_MAX is a constant which is platform dependent and equals the maximum value returned by rand function.

C programming code using rand

We use modulus operator in our program. If you evaluate a % b where a and b are integers then result will always be less than b for any set of values of a and b. For example
For a = 1243 and b = 100
a % b = 1243 % 100 = 43
For a = 99 and b = 100
a % b = 99 % 100 = 99
For a = 1000 and b = 100
a % b = 1000 % 100 = 0
In our program we print pseudo random numbers in range [0, 100]. So we calculate rand() % 100 which will return a number in [0, 99] so we add 1 to get the desired range.
#include <stdio.h>
#include <stdlib.h>
int main() {
  int c, n;
  printf("Ten random numbers in [1,100]\n");
  for (= 1; c <= 10; c++) {
    n = rand() % 100 + 1;
    printf("%d\n", n);
  }
  return 0;
}
If you rerun this program, you will get the same set of numbers. To get different numbers every time you can use: srand(unsigned int seed) function; here seed is an unsigned integer. So you will need a different value of seed every time you run the program for that you can use current time which will always be different so you will get a different set of numbers. By default, seed = 1 if you do not use srand function.

C programming code using random function (Turbo C compiler only)

Function randomize is used to initialize random number generator. If you don't use it, then you will get same random numbers each time you run the program.
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int main()
{
   int n, max, num, c;
   printf("Enter the number of random numbers you want\n");
   scanf("%d", &n);
   printf("Enter the maximum value of random number\n");
   scanf("%d", &max);
   printf("%d random numbers from 0 to %d are:\n", n, max);
 
   randomize();
   for (= 1; c <= n; c++)
   {
      num = random(max);
      printf("%d\n",num);      
   }
   getch();
   return 0;
}

program to delete a file

 


C program to delete a file whose name (with extension) a user will input. The file must be present in the directory in which the executable of this program is present. Macro "remove" is used to delete the file. If there is an error in deleting the file, then it will be displayed by perror function.

C file deletion program

#include <stdio.h>
int main()
{
  int status;
  char file_name[25];
  printf("Enter name of a file you wish to delete\n");
  gets(file_name);
  status = remove(file_name);
  if (status == 0)
    printf("%s file deleted successfully.\n", file_name);
  else
  {
    printf("Unable to delete the file\n");
    perror("Following error occurred");
  }
  return 0;
}

Output of program:
Output of C program to delete a file
The deleted file doesn't go to the trash or recycle bin, so you may not be able to recover it. To recover deleted files special recovery software is required. The file recovery is possible if it isn't overwritten on the storage medium by other data.