Blog

  • Java Program to Convert Binary to Octal

    In this tutorial, we will write a java program to convert a binary number into octal using while loop. Before that, you must have knowledge of the following topics in C++.

    Binary number

    The binary numbers are based on 0 and 1, so it is a base 2 number. They are the combination of 0 and 1. For example, 1001, 110101, etc.

    Octal number

    The octal numbers are the numbers with base 8 and use the digits 0 to 7. Example: 8 in decimal is represented as 10 in octal, 25 as 31, and so on.

    Let us go through a program for the Binary to Octal Conversion in java.


    Java Program to Convert Binary to Octal using while loop

    import java.util.Scanner;
    
    public class Main
    {
      public static void main(String args[])
      {
        int bin, rem, i = 0;
        int octalNum = 0, deciNum = 0;
        Scanner sc = new Scanner(System.in);
    
        System.out.print("Enter a Binary Number: ");
        bin = sc.nextInt();
    
        while (bin != 0)
        {
          deciNum += (bin % 10) *Math.pow(2, i);
          ++i;
          bin /= 10;
        }
    
        i = 1;
    
        while (deciNum != 0)
        {
          octalNum += (deciNum % 8) *i;
          deciNum /= 8;
          i *= 10;
        }
        
        System.out.print("Equivalent Octal Value: " + octalNum);
        
      }
    }

    Output:

    Enter a Binary Number: 1010
    Equivalent Octal Value: 12


  • Java Program to Convert Octal to Binary Number

    In this tutorial, we will write a java program to convert a octal number into binary using while loop. Before that, you must have knowledge of the following topics in C++.

    Binary number

    The binary numbers are based on 0 and 1, so it is a base 2 number. They are the combination of 0 and 1. For example, 1001, 110101, etc.

    Octal number

    The octal numbers are the numbers with base 8 and use the digits 0 to 7. Example: 8 in decimal is represented as 10 in octal, 25 as 31, and so on.

    Let us go through a program for the Ocatal to Binary Conversion in java.


    Java Program to Convert Octal number to Binary Number

    import java.util.Scanner;
    
    public class Main
    {
      public static void main(String args[])
      {
        int octalNum, rem, i = 0;
        int bin = 0, deciNum = 0;
        Scanner sc = new Scanner(System.in);
    
        System.out.print("Enter a Octal Number: ");
        octalNum = sc.nextInt();
    
        while (octalNum != 0)
        {
          deciNum += (octalNum % 10) *Math.pow(8, i);
          ++i;
          octalNum /= 10;
        }
    
        i = 1;
    
        while (deciNum != 0)
        {
          bin += (deciNum % 2) *i;
          deciNum = deciNum / 2;
          i *= 10;
        }
    
        System.out.print("Equivalent Binary Value: " + bin);
    
      }
    }

    Output:

    Enter a Octal Number: 12
    Equivalent Binary Value: 1010


  • Java Program to Convert Binary to Decimal

    In this tutorial, we will write a java program to convert binary to decimal using while loop. Before that, you must have knowledge of the following topics in java.

    Binary number

    The binary numbers are based on 0 and 1, so it is a base 2 number. They are the combination of 0 and 1. For example, 1001, 110101, etc.

    Decimal Number

    These are the numbers with a base of 10, which ranges from 0 to 9. These numbers are formed by the combination of 0 to 9 digits such as 24, 345, etc.

    Here is the chart where for the equivalent value of decimal and binary numbers.

    Decimal to binary

    Example:

    Input: 101
    Output: 5

    Input: 111
    Output: 7

    Now let us go through a program for the conversion of binary to decimal in java using while loop.


    Before using a while loop, let us find the conversion of binary number to decimal number by using the parseInt() method of the Integer class.

    import java.util.Scanner;
    
    public class Main
    {
      public static void main(String args[])
      {
        String bin;
        Scanner sc = new Scanner(System.in);
    
        System.out.println("Enter a binary number: ");
        bin = sc.nextLine();
    
        System.out.println("Equivalent Decimal Value: " + Integer.parseInt(bin, 2));
      }
    }

    Enter a binary number:
    1001
    Equivalent Decimal Value: 9


    Java Program to Convert Binary to Decimal using while loop

    import java.util.Scanner;
    
    public class Main
    {
      public static void main(String args[])
      {
        int bin, decimal = 0, i = 0;
        Scanner sc = new Scanner(System.in);
    
        System.out.println("Enter a binary number: ");
        bin = sc.nextInt();
    
        while (bin != 0)
        {
          decimal += ((bin % 10) *Math.pow(2, i));
          bin /= 10;
          i++;
        }
    
        System.out.println("Equivalent Decimal Value: " + decimal);
      }
    }

    Output:

    Enter a binary number:
    1010
    Equivalent Decimal Value: 10


  • Java Program to Convert Decimal to Binary using Loop

    In this tutorial, we will write a java program to convert decimal to binary without using array. Before that, you must have knowledge of the following topics in java.

    Binary number

    The binary numbers are based on 0 and 1, so it is a base 2 number. They are the combination of 0 and 1. For example, 1001, 110101, etc.

    Decimal Number

    These are the numbers with a base of 10, which ranges from 0 to 9. These numbers are formed by the combination of 0 to 9 digits such as 24, 345, etc.

    Here is the chart where for the equivalent value of decimal and binary numbers.

    Decimal to binary

    Example:

    Input: 5
    Output: 101

    Input: 7
    Output: 111

    Now let us go through a program for the conversion of decimal to binary in java using a while loop.


    Java Program to convert Decimal to Binary using while loop

    import java.util.Scanner;
    
    public class Main
    {
      public static void main(String[] args)
      {
        int i = 1, decimal, binary = 0, rem;
        Scanner sc = new Scanner(System.in);
    
        System.out.println("Enter a decimal number: ");
        decimal = sc.nextInt();
    
        while (decimal != 0)
        {
          rem = decimal % 2;
          binary += i * rem;
          decimal /= 2;
    
          i *= 10;
        }
    
        System.out.println("Equivalent Binary Value: " + binary);
      }
    }

    Output:

    Enter a decimal number:
    10
    Equivalent Binary Value: 1010


  • Java Program to Convert Decimal to Binary using Recursion

    In this tutorial, we will write a program to convert a decimal number into binary in java using recursion. Before that, you must have knowledge of the following topics in java.

    Binary number

    The binary numbers are based on 0 and 1, so it is a base 2 number. They are the combination of 0 and 1. For example, 1001, 110101, etc.

    Decimal Number

    These are the numbers with a base of 10, which ranges from 0 to 9. These numbers are formed by the combination of 0 to 9 digits such as 24, 345, etc.

    Here is the chart where for the equivalent value of decimal and binary numbers.

    Decimal to binary

    Example:

    Input: 5
    Output: 101

    Input: 7
    Output: 111

    Now let us go through a program for the conversion of decimal to binary in java using recursive function.


    Java Program to Convert Decimal to Binary using Recursion

    Source code:

    import java.io.*;
    import java.util.Scanner;
    
    public class Main
    {
       public static void main(String args[])
       {
          int dec;
          Scanner in = new Scanner(System.in);
    
          System.out.println("Enter a decimal number: ");
          dec = in .nextInt();
    
          System.out.print("Equivalent Binary Value: ");
          System.out.println(conversionFunc(dec));	//calling recursive function
    
       }
    
       //recursive function
       public static int conversionFunc(int decNumber)
       {
          if (decNumber == 0)
             return 0;
          else
             return (decNumber % 2 + 10* conversionFunc(decNumber / 2));
       }
    }

    Output:

    Enter a decimal number:
    7
    Equivalent Binary Value: 111


  • C Program to Count the Number of Characters Words and Lines in a file

    In this tutorial, we will write a C program to count the number of characters words, and lines in a text file. Before that, you may go through the following C topics.

    Explanation: The program takes the user input for the file name or file path (if not present in the same directory). Then computes using while loop.

    There are three integer variables in a program whose values are incremented by one whenever the condition is fulfilled. When the file reaches the end of the file, the execution control comes out of the loop and finally displays the counted result.


    C Program to count Number of Characters Words and Lines in a file

    Source code: Write a C program to count characters, words, and lines in a text file.

    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
       FILE * file;
       char fileName[100], ch;
       int characters, words, lines;
    
       printf("Enter the file name: ");
       scanf("%s", fileName);
    
       // open file in read mode
       file = fopen(fileName, "r");
       if (file == NULL)
       {
          printf("Unable to open the file");
          exit(EXIT_FAILURE);
       }
    
       characters = words = lines = 0;
       while ((ch = fgetc(file)) != EOF)
       {
          characters++;
    
          //check for lines
          if (ch == '\n' || ch == '\0')
             lines++;
    
          //check for words
          if (ch == ' ' || ch == '\t' || ch == '\n' || ch == '\0')
             words++;
       }
    
       if (characters > 0)
       {
          words++;
          lines++;
       }
    
       //Display
       printf("\nTotal Number of characters: %d", characters);
       printf("\nTotal Number of words:  %d", words);
       printf("\nTotal Number of lines:  %d", lines);
    
       fclose(file);
    
       return 0;
    }

    Output:

    calculate the characters words and lines in C

    The content inside the test.txt:

    Hello!
    This is simple2code.com
    You can learn coding tutorials and programs from the beginning.


  • C Program to Count the Number of Lines in a File

    In this tutorial, we will write a C Program to find the number of lines in a text file. Before that, you may go through the following C topics.

    Explanation: The program asks the user to enter the file name or file path (if not present in the same directory). If the file is not present then it will throw a message else it will continue the counting process.

    First, the file checks if the starting is EOF or not, if not then the count will increase to 1 (if this part is not checked then the count will show one less result than the actual number of lines present), and then the counting begins using a while loop. And it increases the count when a newline is found every time inside a file.


    C Program to Count the Number of Lines in a File

    Source code: Count the number of lines in a file in C

    #include <stdio.h>
    #define FILE_NAME_SIZE 100
    
    int main()
    {
       FILE * fptr;
       int count = 0;
       char fileName[FILE_NAME_SIZE], ch;
    
       printf("Enter the name of the file: ");
       scanf("%s", fileName);
    
       // Opening the file and check for its existence
       fptr = fopen(fileName, "r");
       if (fptr == NULL)
       {
          printf("Could not open file.");
          return 0;
       }
    
       ch = getc(fptr);
       //if file is not empty then count the first line as 1
       if (ch != EOF)
          count++;
    
       // extracting the lines
       while (ch != EOF)
       {
          // if character id newline
          if (ch == '\n' || ch == '\0')
             count = count + 1;
    
          ch = getc(fptr);
       }
    
       fclose(fptr);
       printf("Number of line present in %s: %d ", fileName, count);
    
       return 0;
    }

    Output:

    read lines in c

    The content inside the test.txt:

    Hello!
    This is simple2code.com
    You can learn coding tutorials and programs from the beginning.


  • C Program to Merge Two Files

    In this tutorial, we will write a C Program to merge the contents of two files into a third file. Before that, you may go through the following C topics.

    Explanation: The program will ask for the two names of the file from where the content is copied and then ask for the third file name where both the files are concatenated and copied there. If the third file is not present then the program will automatically create a file with the entered name.

    Lastly, check the third file which will be present in the same directory as the source code.


    C Program to Merge Two Files

    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
       FILE *fp1, *fp2, *fp3;
    
       char ch, file1[20], file2[20], file3[20];
    
       printf("Enter the first file name: ");
       gets(file1);
    
       printf("Enter the second file name: ");
       gets(file2);
    
       printf("Enter the file name where you want ot store: ");
       gets(file3);
    
       fp1 = fopen(file1, "r");
       fp2 = fopen(file2, "r");
    
       if (fp1 == NULL || fp2 == NULL)
       {
          perror("Error ");
          exit(0);
       }
    
       // Opening in write mode
       fp3 = fopen(file3, "w");
    
       if (fp3 == NULL)
       {
          perror("Error ");
          exit(0);
       }
    
       while ((ch = fgetc(fp1)) != EOF)
          fputc(ch, fp3);
    
       while ((ch = fgetc(fp2)) != EOF)
          fputc(ch, fp3);
    
       printf("\n%s and %s are merges to %s successfully.\n", file1, file2, file3);
    
       fclose(fp1);
       fclose(fp2);
       fclose(fp3);
    
       return 0;
    }

    Output:

    copy file in C

    Before and after the execution, all the files will look like following:

    Before:
    file1.txt:
    Hello World!
    file2.txt
    This is simple2code.com.

    After:
    file3.txt
    Hello World!
    This is simple2code.com.

    The above program to merge the content of two files in C is successfully tested in a machine.


  • C Program to Append Data to a Text file

    In this tutorial, we will write a C program to append data into a file. Before that, you may go through the following C topics.

    Appending data means adding an additional data at the end of the file such as:

    test.txt: Hello!
    New Data: This is simple2code.com

    After appending the new data to the file, it will look something like this,
    test.txt:
    Hello!
    This is simple2code.com


    C Program to append data to a text file

    Explanation: We will first open the file, if the file does not exist then the program will automatically create a file for you. Then the program will ask for the data to enter.

    If there is some data already present in the file then the newly entered data will be added at the end of the file.

    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
       FILE * fp;
       char data[1000];
    
       fp = fopen("test.txt", "a");
    
       printf("Enter data to append: ");
       fgets(data, 1000, stdin);
    
       fputs(data, fp);
    
       printf("File appended successfully");
       fclose(fp);
    
       // Again open the file to display
       fp = fopen("test.txt", "r");
    
       printf("\nNew Content:\n");
       char ch;
       while ((ch = fgetc(fp)) != EOF)
       {
          printf("%c", ch);
       }
    
       fclose(fp);
    
       return 0;
    }

    Output: After the successful execution of the program for appending data into a file in C, we get,

    append file in C

    Before and after the execution of the program, the data present in the test.txt:

    Before:
    Hello!

    After:
    Hello!
    This is simple2code.com


  • C Program to Copy Contents of one file to another

    This is a file tutorial where we will write a program to copy the content from one file to another file in C. Before that, you may go through the following C topics.

    Before the program:

    Before executing a program you need to create or have a file in the same directory as the source code. If the file from where the program needs to be copied does not exist then the program will show an error message.

    So let us create a file with the name “test.txt” and has the following content in it.

    Hi! Everyone…
    This is simple2code.com


    Copy content from one file to another in C program

    The program will ask for two file names, one from where the content should be read and to where the content should be written.

    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
       FILE *fptr1, *fptr2;
       char filename[50], ch;
    
       printf("Enter the file name to be read: ");
       scanf("%s", filename);
    
       fptr1 = fopen(filename, "r");
       if (fptr1 == NULL)
       {
          printf("File does not exists!");
          exit(0);
       }
    
       printf("Enter the file name where to be copied: ");
       scanf("%s", filename);
    
       fptr2 = fopen(filename, "w");
       if (fptr2 == NULL)
       {
          printf("Cannot open file %s \n", filename);
          exit(0);
       }
    
       ch = fgetc(fptr1);
       while (ch != EOF)
       {
          fputc(ch, fptr2);
          ch = fgetc(fptr1);
       }
    
       printf("\nContent is copied to %s successfully.", filename);
    
       fclose(fptr1);
       fclose(fptr2);
    
       return 0;
    }

    Output:

    copy content from one file to another in C

    Note: If the file to where the content should be copied is not created before the program then the program will automatically create that file for you.

    After successful execution of the program, check the copy.txt file, you will find the same content present in the test.txt file.