Posts

Showing posts from October, 2020

BASIC TO INTERMEDIATE PROGRAMS IN C

 Basic To Intermediate level programs in C C is the basic, general-purpose programming language. C language is the best choice to learn programming for beginners. Coder Insect will guide you in this learning journey. The best way to learn programming is by practicing different examples by your own . Here, we have included  basic to intermediate level programs like Fibonacci series, Palindrome number, Armstrong number, strong number, Perfect number, Prime number, Decimal to binary, Decimal to octal, Binary code to gray code, Happy number, Neon number , Krishnamurthy Number , IP address program, program to add complex numbers  and many other programs in C programming language.  1 . Prime Number Program In  C In this example , you will learn a program to check whether a given number is prime number or not. Prime number  is greater than 1 and can be only divided by 1 and itself, is called a prime number. In short, Prime numbers can't be divided by other numbers than itself and 1.

KRISHNMURTHY NUMBER PROGRAM IN C

 In this example, you will learn a program to check whether entered number is Krishnamurthy number or not. Krishnamurthy Number: A krishnamurthty  number is a number whose sum of the factorial of digits is equal to the number itself.  Let's take one example: Suppose we want to check whether 40585 4! + 0! + 5! + 8! + 5! = 24 + 1 + 120 + 40320 + 120 =40585 i.e. equal to the  number itself. Hence, 40585 is a Krishnamurthy Number. Program: //Program to check whether entered number is krishnamurthy number or not. #include<stdio.h> #include<process.h> int main( ) { int i,n,a,r,c=0,s; printf (" Enter a number : "); scanf( "%d" , &n ); if(n==0) {      printf ("\n %d is not a Krishnamurthy Number.",n);        exit(0) ; } a=n; while(n>0) {      r=n%10;      s=1;      for(i=r;i>=1;i--)           s=s*i;      c=c+s;      n=n/10; } if(c==a) {      printf

PROGRAM TO CONVERT BINARY CODE TO GRAY CODE

Image
 In this example, you will learn a program to convert the Binary code into its equivalent Gray code. (To understand this program , you should have prior knowledge about binary codes , gray codes and its conversion algorithms. ) Here are some examples of binary and its equivalent gray code: Program: //Program to Convert Binary Code to Gray Code #include<stdio.h>  int bintogray(int); int main ( ) { int bin, gray; printf ("Enter a binary number: "); scanf( "%d" , &bin ); gray = bintogray(bin); printf ("The gray code of %d is %d\n", bin, gray); return 0; } int bintogray(int bin) { int a, b, result = 0, i = 0; if (!bin) {      return 0; } else {      a = bin % 10;      bin = bin / 10;      b = bin % 10;      if ((a && !b) || (!a && b))      {          

PROGRAM TO SWAP TWO NUMBERS USING BITWISE XOR

 In this example, you will learn a program to swap the contents of two numbers using Bitwise XOR operation. Program: //Program to swap the content of two numbers using Bitwise Xor operation #include<stdio.h>  int main( ) { long i, k; printf ("Enter two integers: \n"); scanf(" %ld %ld ", &i, &k ); printf ("\n Before swapping i= %ld and k = %ld", i, k); i = i ^ k; k = i ^ k; i = i ^ k; printf ("\n After swapping i= %ld and k = %ld ", i, k); return 0; } Output: Enter two integers: 71 90      Before swapping i= 71 and k = 90                After swapping i= 90 and k = 71    So that's it ! Just try this code by your own. If you have any doubts then feel free to drop a comment . I'll be happy to answer your questions . Keep coding... .

PROGRAM TO DISPLAY IP ADDRESS OF THE SYSTEM

 In this example,  you will learn a program to display the IP address of the system. IP Address: An IP (Internet Protocol) address is an identifier assigned to each computer and other device (ex. router, mobile etc)connected to IP network that is used to locate and identify the node in the communication with other nodes on the network. Program: //Program to display IP address of the system #include<stdio.h> #include<string.h> #include<sys/types.h> #include<sys/socket.h>  #include<sys/ioctl.h> #include<netinet/in.h> #include<net/if.h>  #include<unistd.h>  #include<arpa/inet.h>  int main( ) { int n; struct ifreq ifr; char array[] = "eth0"; n = socket(AF_INET, SOCK_DGRAM, 0); //Type of address to retrieve - IPv4 IP address ifr.ifr_addr.sa_family = AF_INET; //Copy the interface name in

PROGRAM TO SORT STRINGS IN LEXICOGRAPHICAL ORDER

In this example, you will learn a program to sort 5 strings in the lexicographical order i.e. dictionary order. Program: //Program to sort strings in lexicographical order #include<stdio.h>  #include<string.h> int main( ) { char   str[5][50],  temp[50]; printf ("Enter 5 words: "); // Getting strings input for (int i = 0; i = 5; ++i) {     fgets(str[i], sizeof(str[i]), stdin); } // storing strings in the lexicographical order for (int i = 0; i = 5; ++i) {      for (int j = i + 1; j = 5; ++j)     {              if (strcmp(str[i], str[j]) > 0)             {                     strcpy(temp, str[i]);                     strcpy(str[i], str[j]);                     strcpy(str[j], temp);             }        } } printf ("\n In the lexicographical order: \n"); for (

DECIMAL TO OCTAL CONVERSION IN C

 In this example, you will learn a program to convert decimal number to its equivalent octal number. Decimal numbers have base 10 , and they use numbers between o to 9. while, Octal  numbers have base 8 , and they use numbers between 0 to 7. ( To understand this program , you should have prior knowledge about decimal to octal conversion. ) Program: //Program to convert decimal number to octal number #include<stdio.h> #include<conio.h> int DectoOct( int num); int main( ) { int num; printf ("Enter a decimal number:"); scanf( "%d" , &num ); printf ("%d decimal number= %d in octal number", num, DectoOct(num) ); return 0; } int DectoOct(int num) { int OctalNumber=0, i=1; while(num !=0) { OctalNumber += (num %8)*i; num /= 8; i*=10; } return OctalNumber; } Output: Enter a decimal number: 10 10 decimal number= 12 in octal number   

PROGRAM TO ADD TWO COMPLEX NUMBERS

 In this example , you will  learn a program to add two complex numbers by passing structure to function. (To understand this program , you should have prior knowledge about complex numbers and its addition .) Program: //Program to add two complex numbers #include<stdio.h>  typedef struct complex { float real; float imagi; } complex ; complex add(complex n1, complex n2); int main( ) { complex n1, n2, result; printf ("For 1st complex number \n"); printf ("Enter the real and imaginary parts: "); scanf(" %f %f ", &n1.real, &n1.imagi ); printf ("\nFor 2nd complex number \n"); printf ("Enter the real and imaginary parts: "); scanf(" %f %f ", &n2.real, &n2.imagi ); result = add(n1, n2); printf ("Sum of complex numbers = %.1f + %.1fi", result.real, result.imagi); return 0; } complex add(complex n

PROGRAM TO FIND ROOTS OF QUADRATIC EQUATION IN C

 In this example, you will learn a program to find the roots of quadratic equations. The standard form of Quadratic equation is: ax²+bx+c = 0 , and a,b,c are the real numbers  such that a !=0 b²-4ac is known as the discriminant of the quadratic equation and it tells about the nature of roots of quadratic equation. 1. If discriminant is >0 , then roots are real and different. 2. If discriminant is =0 , then roots are real and equal. 3.If discriminant is <0 , then roots are not real(i.e. complex) and different.  Program: //Program to find roots of quadratic equation #include<math.h> #include<stdio.h> int main( ) { double a, b, c, disc, r1, r2 , real_part, imag_part; printf ("Enter coefficients a, b and c: "); scanf(" %lf %lf %lf ", &a, &b, &c ); disc = b * b - 4 * a * c; if (disc > 0) {      r1 = (-b + sqrt (disc)) / (2 * a);      r2 = (-b - sqrt (disc)) / (2 *

NEON NUMBER PROGRAM IN C

 In this example , you will learn a program to check whether entered number is Neon number or  not. Neon Number: Neon number is the number whose sum of digits of square of number is equal to the number itself. Lets consider one example,  suppose we want to check whether 9 is neon number or not. 9² = 81 and sum of digits of 81 = 8+1 = 9 i.e. the number itself. Thus, 9 is a neon number. Program: //Program to check whether given number is Neon number or not #include <stdio.h> int main( ) { int num, rem, sq, sum=0 ; printf ("Enter any number:"); scanf( "%d" , &num ); sq=num*num;                     while(sq !=0)      {            rem= sq%10;            sum=sum +rem;            num=num/10;       }                     if(sum==num)      {           printf ("%d is a neon number",num);        }      else      {      printf ("%d is not a neon number",num);      } return 0; } Output:  Enter the num

HAPPY NUMBER PROGRAM IN C

In this example, you will learn a program to check whether entered number is a happy number or not. Happy Number: In  number  theory,  a happy number is a number  which eventually reaches 1 when replaced by the sum of the square of each digit.  Lets consider one example! Suppose we want to check whether 19 is happy number or not. 19 => 1² + 9² = 82                                8² +  2² = 68 6² + 8² = 100 1² + 0² +  0² =1 And here the sum of squares of digits ends  up with 1. Thus, 19 is happy number. Program : //Program to check whether given number is happy number #include <stdio.h> int isHappy(int); int main( ) { int num, result; printf ("Enter any number:"); scanf(" %d ", &num ); result=num;      while (result != 1 && result !=4)      {           result = isHapppy(result);      }      if(result==1)      {           printf ("%d is a happy number",num);      }      e

STRONG NUMBER PROGRAM IN C

In this example , you will learn a program to check whether entered number is strong number or  not. Strong number: If sum of factorials of digits of a number is equal to the original number, then it is called as Strong number. Let's consider one example, suppose we have to check whether 145 is strong number or not . 1! + 4!+ 5! = 1 + 24 + 120 = 145 i.e. equal to the original number. Hence, 145 is a strong number. Algorithm to check for strong number: 1. Input any number from user to check for strong number. 2. Initiailize one variable sum to store the sum of factorials of digits. 3. Find last digit of number by using modulo operator (%). 4. Find the factorial of last digit and add it to the sum. 5. Then remove the last digit from the number as it is not required further. 6. Repeat steps 3-6 until number >0. 7. Check whether sum is equal to the number or not. If sum is equal to entered number then it is strong number otherwise it is not a strong number. Program to check for st