Adsense 728 * 15

Search

Custom Search

Java Program to Check Perfect Number

Write a java program to check whether a number is perfect or not. Given an input number, we have to write a java code which check whether an input number is perfect number or not.

Before solving this problem, let's first understand what is a perfect number.

What is Perfect Number?


A perfect number is a positive integer that is equal to the sum of it's positive divisors  excluding the number itself.

For example -

6  is a perfect number. It's positive divisor is 1, 2 and 3 and sum of it's positive divisor is 6 (1 + 2 + 3).

Similarly, 28 is a perfect number. Sum of the positive divisor is 28 (1 + 2 + 4 + 7 + 14).

10 is not a perfect number. Sum of the positive divisor of 10 is 8 ( 1 + 2 + 5).

Java Program to Check Perfect Number



Java Program to Convert Binary to Decimal Number

Java program to find second smallest number in an array

Java Programming Questions with Answers




Java Program to Check Perfect Number or Not


We have discussed what is a perfect number? Let's write a java code which takes an input number and check whether an input number is a perfect number or not.


package perfectnumber;

import java.util.*;

public class PerfectNumber {

    public static void main(String[] args) {

        int num, sum = 0;
        
        System.out.println("Enter a number");
            
        Scanner in = new Scanner(System.in);
        num = in.nextInt();
            
        /* If number is greater than zero */
        if (num > 0) {

          for (int i = 1; i < num; i++) {

            /* sum of it's factors */
            if ( num % i == 0) {
                sum = sum + i;
            }
        }
    
          if ( sum == num) {
              System.out.println(num + " is a perfect number ");

          } else {
              System.out.println (num + " is not a perfect number ");
          }
            
       }   else {
                
            System.out.println (" Please enter positive number ");
       }
   }
    
}




Output : 

Enter a number  :    28

28 is a perfect number

Sorting algorithms and their time complexity

C++ Program to Insert an Element in an Array

Write a c++ program to insert an element in an array. In this program, we are going to write a c++ code which takes an input array, position and value of an element and insert it an array.


C program to insert an element in an array


C++ Program to Insert an Element in an Array



C++ Program to Insert an Element in an Array


#include <iostream>
using namespace std;

 int main() {
 
   int arr[100], size, pos, val, i, temp;
 
   cout << "Enter the size of an array (MAX 100) \n";
   cin  >> size;
 
   // Create an input array
   cout << "Enter an elements in an array \n";
 
   //Input the value of an array
   for(i = 0; i < size; i++) {
      cin >> arr[i];
   }
 
   // Take a position of a new element
   cout << "Enter a position\n";
   cin  >> pos;
 
   //Input value of an element to be inserted
   cout << "Enter a value to insert\n";
   cin  >> val;
 
   //Shift element by one position
   for(i = size; i >= pos; i--) {
      arr[i] = arr[i-1];
   }
 
   //Increase the length of an array
   size++;
 
   //Insert new value in an array
   arr[pos-1] = val;
 
   cout << "Array after inserting a new value \n";
 
   for(i = 0; i < size; i++) {
      cout << arr[i] << " ";
   }
 
 
   return 0;
 }



C++ code for practice

C Program to Insert an Element in an Array

Write a c program to insert an element in an array at specified position.  This question can also be asked as, Write a c program to insert an element at a given position.

In this program, we are going to write a code which takes an input array, position and value of a new element from a user and insert it at a specified position.

For example -

Input array - {4, 3, 1, 5}

Input position - 3

New element - 7

After inserting an element - {4, 3, 7, 1, 5}

C Program to Insert an Element in an Array



C Program to delete an element from an array

Programming questions on Arrays

Program to print smallest element of an array


Logic of a Inserting an Element in an Array

i) Take an array, position and new element as an input from a user.

ii) Move an existing element one position up until  the position is reached where a new element is need to be inserted.

Algorithm to insert an element in an array


C Program to Insert an Element in an Array


Let's write a c code to insert an element in an array. First, we take an input array, position and new element from a user.


#include <stdio.h>

int main() {
    
    int n, pos, arr[100], i, num;
    
    printf(" Enter the size of an array (Max 100) \n");
    scanf( " %d ", &n);
    
    printf( " Enter the element of an array \n");
    
    //Input value of an array
    for(i = 0; i < n; i++) {
        scanf(" %d ", &arr[i]);
    }
    
    //Position to insert a new element
    
    printf(" Enter the position \n");
    scanf(" %d ", &pos);
    
    // Number to be inserted in an array
    
    printf(" Enter number to be inserted \n");
    scanf(" %d ", &num);
    
    for( i = n; i >= pos; i--) {
        arr[i] = arr[i-1];
    }
    
    arr[pos-1] = num;
    
    //Increment the size of an array
    n++;
    
    
    printf(" Array after inserting new element \n");
    
    //Array after inserting new element
    for( i = 0; i < n; i++) {
        printf(" %d ", arr[i]);
    }
    return 0;
}




Programming question on Arrays

Programming question on Linked List

Sorting algorithm and their time complexity

Stack implementation

Programming questions using Recursion

C, C++ interview questions

Programming Books

C++ Program to Check Leap Year - Whether a Entered Year is Leap Year or Not

Write a c++ program to check whether a entered year is leap year or not. In this programming tutorial, We are going to write a c++ code which takes an input year and check whether a entered year is leap year or not.

In my previous tutorial, I have explained what is a leap year and algorithm to check leap year.

C program to check leap year

Programming videos

C++ Program to Check Leap Year



C++ Program to Check Leap Year using Function


A Leap year is a year which has 366 days. It means it has one additional day. How do we check whether a entered year is a leap year or not.

 i) If a year is divisible by 4 and not divisible by 100 then it's a leap year. For example - 2012, 2008 etc.

 ii) If a century year (1200, 1600) is divisible by 400 then it's a leap year. For example - 1200, 1600 etc.


C++ program to print factorial of a number using recursion


#include <iostream>

using namespace std;

bool checkLeapYear(int year) {
    
    /* Leap year is divisible by 4 but not by 100
         or it is divisible by 400
    */
      
    if( ( (year%4 == 0) && (year%100!=0) ) 
            || (year%400==0) ) 
    {
        return true;
    }
    else
    {
        return false;
    }
}

int main(void) {
 
      int year;
   
      //Input year
      cout << "Enter a year\n";
      cin  >> year;
      
      
      //Method call to check leap year
      if( checkLeapYear(year) ) 
      {
           cout << year << " is a leap year.";
      }
      else
      {
           cout << year << " is not a leap year.";
      }

     return 0;     
}


Output : 

Enter a year 2008

2008 is a leap year

C interview questions with solutions

Sorting algorithm and their time complexities

Programming questions on arrays




C Program to Check Leap Year

Write a C program to check whether a entered year is a leap year or not. In this tutorial,  we are going to write a code which take a year as an input and print whether a input year is a leap year or not.

What is Leap Year?


A Leap year is a year which has 366 days, it means it has one additional day. Each leap year has 366 days instead of the usual 365 days.

Leap Year Program in C, C++


Algorithm to Check Leap Year


i) If a year is divisible by 4 and not divisible by 100 then it's a leap year.  For example - 2012, 2008 etc.

ii) If a century year (1200, 1600) is divisible by 400  then it's a leap year. For example - 1200, 1600 etc.

C, C++ Interview Questions

C program to print factorial of a number

C program to calculate power of a number


C Program to Check Leap Year


C Program to Check Leap Year using Conditional Operator


We have discussed what is leap and algorithm to check leap year. Let's write a code which takes an input (year) and check whether a input year is a leap year or not.

#include <stdio.h>

int main(void) {
 
      int year;
   
      printf("Enter a year\n");
      scanf("%d",&year);
      
      /* Leap is divisible by 4 but not by 100
         or it is divisible by 400
      */
      if( ( (year%4 == 0) && (year%100!=0) ) 
             || (year%400==0) )
      {
           printf("%d is a leap year.", year );
      }
      else
      {
           printf("%d is not a leap year.", year);
      }

     return 0;     
}


Another way of writing above program.

C program to check whether a number is Palindrome or not

#include <stdio.h>

int main(void) {
 
      int year;
   
      printf("Enter a year \n");
      scanf("%d",&year);
      
      /* Check whether a entered year 
         is divisible by 4 */
      
      if(year%4 == 0)
      {
         /* For century year 
           such as 500,1200 etc */
         
          if( year%100 == 0) 
          {
             /* If it's divisible by 400 
                then it's a leap year */
             
              if ( year % 400 == 0)
              {
                 printf("%d is a leap year.", year);
              }
              else
              {
                 printf("%d is not a leap year.", year);
              }
          }
          else
             printf("%d is a leap year.", year );
      }
      else
      {
         printf("%d is not a leap year.", year);
      }
      
      return 0;
      
}




C Program to Check Leap Year using Function



In this programming example, let's create a function which checks whether an input year is leap year or not.

#include <stdio.h>

bool checkLeapYear(int year) {
    
    /* Leap is divisible by 4 but not by 100
         or it is divisible by 400
    */
      
    if( ( (year%4 == 0) && (year%100!=0) ) 
            || (year%400==0) ) 
    {
        return true;
    }
    else
    {
        return false;
    }
}

int main(void) {
 
      int year;
   
      printf("Enter a year\n");
      scanf("%d", &year);
      
      
      if( checkLeapYear(year) ) 
      {
           printf(" %d is a leap year.", year );
      }
      else
      {
           printf("%d is not a leap year.", year);
      }

     return 0;     
}



Output:


Enter a year  :  2008

2008 is a leap year

Java Program to Convert Binary to Decimal Number

Write a java program to convert binary to decimal number. In this tutorial, we are going to write a program which takes an input binary number and convert them into decimal number.

How to Convert Binary to Decimal Number


Let's assume, we have to convert 1000 to decimal number.

Decimal Number Conversion : 1 * 23   + 0 * 2 2   +  0 * 2 +  0 * 2 0 = 8

In this way, we can convert any binary number to decimal number.


Java program to find largest number in an array

C program to convert binary to decimal number



Java Program to Convert Binary to Decimal Number


Java Program to Convert Binary to Decimal Number


We have discussed an algorithm to convert binary to decimal number. Let's write a code to implement them.


import java.util.Scanner;

public class BinaryToDecimal {

  public static void main(String[] args) {
  
    Scanner in = new Scanner(System.in);

    //Input binary number
    System.out.println("Enter a binary number");
    Long num = in.nextLong();
  
    Long count = 0L, sum = 0L, rem = 0L;
  
    //Whether a number is greater than 0
    while(num > 0) {

      //find remainder
      rem = num % 10;
   
      //In case of binary number,
     //Remainder should be either 0 or 1
     if(rem == 0 || rem == 1) {
    
        sum = (long) (sum + rem * Math.pow(2, count));
        num = num / 10;
    
      } else {
        System.out.println("Invalid binary number");
        break;
      }
   
      count++;
   }

   System.out.println(" Decimal number is " + sum);
 }

}


Output



Java Program

Java program to count number of digits in a number

Java program to check whether a number is palindrome or not

C++ Program to Convert Binary to Decimal Number

Write a C++ code to convert binary to Decimal number. Given an input binary number, we have to write a code to convert binary to decimal number.


C program to convert binary to decimal number


C++ Program to Convert Binary to Decimal Number


C++ Program to Convert Binary to Decimal Number



In this program, we write a code which takes an input binary number and print it's decimal representation.


#include <iostream>
#include <math.h>
using namespace std;

int main() {
 
    int bnum, rem, count = 0, sum = 0, flag = 1;
    
    cout << "Enter a binary number \n";
    cin  >> bnum;
    
    while(bnum > 0) {
    
         rem = bnum % 10;
         
         // Binary number only contains 0 and 1
         if(rem == 0 || rem == 1) {
             
             sum = sum + rem * pow(2, count);
             
         } else {
             
             flag = 0;
             break;
         }
         
         //Increment count variable
         count++;
         bnum = bnum / 10;
    }
    
    if(flag) {
     cout << "Decimal number is " << sum;
    } else {
     cout << "Invalid binary number";
    }
    
    return 0;
}




C Program to Convert Binary to Decimal Number

Write a C program to convert binary to decimal number. In this programming question, we are going to write a code which takes binary number as an input and print their decimal representation.

This question is very important in terms of technical interview.

C interview questions and answers

For example -

Input      : 100 (Binary Number)
Output    : 4    (Decimal Number)

Input     : 111 (Binary Number)
Output  :  7    (Decimal Number)

C Program to Convert Binary to Decimal Number



C++ program to convert binary to decimal number

How to Convert Binary to Decimal Number


Let's assume, we have to convert 100 to decimal number.

Decimal Number Conversion : 1 * 2 2   +  0 * 2 +  0 * 2 0 = 4

Similarly, we can convert any binary representation into decimal number.

Program to Convert Decimal to Binary Number

Program to count number of digits of a number


C Program to Convert Binary to Decimal Number


#include <stdio.h>
#include <math.h>

int main() {
 
  int bnum, sum, rem, count=0;
 
  printf("Enter a binary number \n");
  scanf("%d",&bnum);
 
  /* Initialize a sum variable to 0 */
 
  sum = 0;
 
  while(bnum > 0){
  
      /* Take a Remainder */
  
      rem = bnum%10;
  
      /* If remainder is other than 0 and 1,
       then it is not a binary number. */

      if(rem==0 || rem==1 ){

        /* calculate sum. */
  
        sum = sum + rem *pow(2,count);
 
        /* Divide to reduce the number length. */
 
        bnum = bnum/10;
  
        /* Increment the count. */

        ++count;
   
      } else {

         printf(" Please enter valid binary number \n");
         return 0;    
      }  

  }
 
   printf("\n Decimal number is %d",sum);
  
   return 0;
}


Output:

Enter binary number 1000

Decimal number is 8

Programming question on Arrays

C++ Program to Count the Number of Vowels and consonants in a String

Write a C++ program to count the number of vowels and consonants in a string. In this programming question, We have to write a code which takes a string as an input and count number of vowels and consonants in a string.

C program to count the number of vowels and consonants in a string

C++ Program to Count the Number of Vowels and consonants in a String



How to read string with spaces using scanf

Programming Questions on Strings

C program to find the length of string without using strlen


C++ Program to Count Number of Vowels and Consonants in a String


#include <iostream>
#include <string.h>

using namespace std;

int main() {
 
     char str[300];
     int  vowel = 0, consonant = 0;
 
     cout << "Enter a string \n";
     cin.getline(str, sizeof(str));

      /* Length of a string. */
    
      int len = strlen(str);
    
     /* Traverse a string */
     for(int i = 0; i < len; i++){
          
       /* Check for vowel and increment the count. */

       if( str[i]=='a' || str[i]=='A' 
           || str[i]=='e' || str[i]=='E' 
           || str[i]=='i' || str[i]=='I' 
           ||str[i]=='o' ||str[i]=='O' 
           ||str[i]=='u' ||str[i]=='U' ) {
             
                vowel++;
                
             /* Check for consonant. */

       } else if((str[i] >= 'a' && str[i] <= 'z') 
                  || (str[i] >= 'A' && str[i] <= 'Z')) {
             
               consonant++;
       }
     }
     
     cout << "Vowels = " << vowel << " Consonants = " << consonant;
 
    return 0;
}



Output -

Enter a string  - I love cprogrammingcode.com

Vowels = 9 and Consonants = 15


C Program to Find the Length of a String without using Strlen

Write a c program to find length of a string without using strlen() function.  In this programming questions, we have to write a code to find the length of a string.

We can easily find the length of string using inbuilt strlen() function. Let's first find the length of a string using strlen() function.

C program to compare two strings using strcmp() function

C Program to Find the Length of a String using Strlen


In this programming example, we first take an input string from a user and then we print the length of a string using an inbuilt strlen() function.

#include<stdio.h>

int main() {
 
   char str[100];
  
   //Input string
   printf("Enter a string \n");
   gets(str);
 
   //Print the length of a string
   printf("\n String length is %d", strlen(str));
 
   return 0;
}


How to Find the Length of a String without using Strlen


In C, Every String is terminated with Null Character ('\0'). We can use this concept to traverse a string until null character is encountered.   Once the null character is encountered we terminate the loop and print a count.


C program to check whether two strings are anagrams of each other

How to Read a String with Spaces using Scanf


C Program to Find the Length of a String without using Strlen


C Program to Find the Length of a String without using Strlen



In the above example, we have written a code to print the length of a string using strlen(). In this example, we print the length of a string using while loop.


#include<stdio.h>

int main() {
 
  char str[100];
  int count=0;
 
  printf("Enter a string \n");
  gets(str);
 
  /* Run a loop until null('\0')
    character is encountered. */

  while(str[count]!='\0'){
         
    /* Increment count. */
    count++;
  }
 
  printf("\n String length is %d", count);
 
  return 0;
}


Programming questions on strings

Find Length of String using For Loop


#include<stdio.h>

int main() {
 
 char str[100];
 int i;
 
 printf("Enter string \n");
 gets(str);
 
 for(i=0;str[i]!='\0';i++);
 
 printf("\n String length is %d", i);
 
 return 0;
}

Output:

Enter a string    cprogrammingcode

String length is 16

Program to reverse a string using stack

C program to check whether a character is vowel or consonant