Google Add

Search

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 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