Monday, December 31, 2018

My new Year Plan

Hi
Happy New Year 2019!

This year I wish do a course on Big Data Computing. Among various Java frameworks, I like Hadoop framework, which is a Big data framework.

Best wishes for a Happy, rewarding new Year 2019!

Advanced Java programming

Saturday, September 1, 2018

Queue : Array Implementation in C

#include <stdio.h>
#define QUEUE_LENGTH 50
struct queue
{
    int element[QUEUE_LENGTH];
    int front,rear,choice,x,y;
};
void traverse(struct queue q)
 {  printf("The elements of the queue are\n");
     for(int i=0;i<=q.rear;i++)
     {
         printf("%d\n",q.element[i]);
     }
 }
void add(struct queue *p,int y)
{
    if((*p).rear<QUEUE_LENGTH)
    {   (*p).rear +=1;
        (*p).element[(*p).rear]=y;
        printf("rear =  %d\n",(*p).rear);
        printf("type -1 to exit\n");
        scanf("%d",&y);
        if(y==(-1))
        {
            return;
        }
        add(p,y);
    }
    else
    {
        printf("Queue overflow\n");
    }
}
void delete(struct queue *p)
{
    int x=0,y=0;
    if((*p).front>(*p).rear)
    {
        printf("Queue empty\n");
    }
    else
    {
        x=(*p).element[(*p).front];
        (*p).element[(*p).front]=0;
        (*p).front=(*p).front+ 1;
    }
    printf("type 1 to delete or 2 to exit\n");
        scanf("%d",&y);
        if(y==(2))
        {
            return ;
        }
        if(y==(1))
        {
            traverse(*p);
            printf("%d is deleted\n",(*p).element[(*p).front]-1);
            delete(p);

        }
}
int main()
{   int choice=0,x=0;
    struct queue q;
    q.rear= -1,q.front=0;
        printf("\nEnter element to be added:\n");
        scanf("%d",&x);
        add(&q,x);
        traverse(q);
        delete(&q);
        printf("Deletion completed \n");
    return 0;
}

Output:

  1. $gcc -o main *.c
  2. $main
  3.  
  4. Enter element to be added:
  5. 1
  6. rear = 0
  7. type -1 to exit
  8. 2
  9. rear = 1
  10. type -1 to exit
  11. 3
  12. rear = 2
  13. type -1 to exit
  14. 4
  15. rear = 3
  16. type -1 to exit
  17. 5
  18. rear = 4
  19. type -1 to exit
  20. 6
  21. rear = 5
  22. type -1 to exit
  23. 7
  24. rear = 6
  25. type -1 to exit
  26. -1
  27. The elements of the queue are
  28. 1
  29. 2
  30. 3
  31. 4
  32. 5
  33. 6
  34. 7
  35. type 1 to delete or 2 to exit
  36. The elements of the queue are
  37. 0
  38. 2
  39. 3
  40. 4
  41. 5
  42. 6
  43. 7
  44. 1 is deleted
  45. type 1 to delete or 2 to exit
  46. The elements of the queue are
  47. 0
  48. 0
  49. 3
  50. 4
  51. 5
  52. 6
  53. 7
  54. 2 is deleted
  55. type 1 to delete or 2 to exit
  56. The elements of the queue are
  57. 0
  58. 0
  59. 0
  60. 4
  61. 5
  62. 6
  63. 7
  64. 3 is deleted
  65. type 1 to delete or 2 to exit
  66. Deletion completed
 
 

Friday, August 31, 2018

Java Tutorial

C++ : ' string ' Data type

#include<iostream>
#include<string>
using namespace std;
int main(){
string arr[3][3] = {"Blue", "Red", "Orange", "night","ok","that","is","good","bye"};

for(int i=0;i<3;i++)
{

    for(int j=0;j<3;j++)
    {

        cout<< arr[i][j]<<" "<<i<<" "<<j<<endl;
       
    }

}
return 1;
}


Sunday, August 26, 2018

Flow of values of pointers in a Singly Linked-list as new nodes get added from front

#include<stdio.h>
#include<stdlib.h>
struct Node{
int value;
struct Node * next;
};
int insertNode(struct Node** head,int value){
    printf("Insert Node %d\n",value);
    struct Node* newNodePtr = (struct Node*)malloc(sizeof(struct Node));
    newNodePtr->value=value;
    newNodePtr->next=*head; // Now the 'next' of the NEW NODE holds what head held originally = NULL
    printf(" address in newNodePtr = %d = address OF the NEW NODE\n",newNodePtr);
    printf(" address in newNodePtr->next = %d \n",newNodePtr->next);
    printf(" address in *head = %d \n",*head);
    printf(" address in head = %d \n",head);
    *head=newNodePtr; // I am changing where head is POINTING TO, it now POINTS TO 'newNodePtr'
    // i.e. to the NEW NODDE (now holds the address of a POINTER pointing to the NEW NODE
    printf("Finally address OF head = %d \n",head);
    printf("Finally address in head = *head = %d = address OF the NEW NODE\n",*head);
    return 1;
}
void PrintList(struct Node* head){
        while(head){
        printf("value in nodes is %d \n",head->value);
        head = head->next;
        }
}
int main(){
    int a =10;
    int * p = &a; // 'p' points to 'a'
  struct Node * head = NULL;
   printf("In main, address IN 'head' = %d i.e. head POINTS TO NULL\n",head);
   printf("In main, But 'head' has ITS OWN ADDRESS (address of head = '&head') = %d \n",&head);
   printf("This address OF head '&head' = %d UNIQUELY IDENTIFIES this LIST and remains",&head);
   printf("UNCHANGED throughout\n");
   printf("In main, value at address of head '*(&head) = %d = address in 'head'\n",*(&head));
   printf("This means address IN 'head' == '*(&head)'\n");
   printf("NOTE: 'Node ** head == '&head', POINTER-2-POINTER i.e. ADRRESS of a POINTER\n");
   printf("Therefore, '*heaad' == Address held by 'head',i.e. where the head is POINTING TO");
   printf(" p = %d\n",p);
   printf(" &a = %d\n",&a);
   printf(" &p = %d\n",&p);
   printf(" *(&p) = %d\n",*(&p));
   printf(" *p = %d\n",*p);
   printf("(1) This means p == *(&p), where 'p' is any POINTER\n");
   printf("(2) This means if you print 'p' you will get '&a', because p=&a\n");
   printf("\nSee how the values of newNodePtr, newNodePtr->next\n");
   printf("addresses head holds(points to) changes, BUT  the address OF HEAD\n");
   printf("remains the SAME with each call to insertNode\n\n");
  int arr[5]={1,2,3,4,5};
    int i;
    for(i=0;i<5;i++){
     insertNode(&head,arr[i]);
    }
     PrintList(head);
    return 0;
}


Output:
$gcc -o main *.c
$main
In main, address IN 'head' = 0 i.e. head POINTS TO NULL
In main, But 'head' has ITS OWN ADDRESS (address of head = '&head') = -1625641784 
This address OF head '&head' = -1625641784 UNIQUELY IDENTIFIES this LIST and remainsUNCHANGED throughout
In main, value at address of head '*(&head) = 0 = address in 'head'
This means address IN 'head' == '*(&head)'
NOTE: 'Node ** head == '&head', POINTER-2-POINTER i.e. ADRRESS of a POINTER
Therefore, '*heaad' == Address held by 'head',i.e. where the head is POINTING TO p = -1625641768
 &a = -1625641768
 &p = -1625641776
 *(&p) = -1625641768
 *p = 10
(1) This means p == *(&p), where 'p' is any POINTER
(2) This means if you print 'p' you will get '&a', because p=&a

See how the values of newNodePtr, newNodePtr->next
addresses head holds(points to) changes, BUT  the address OF HEAD
remains the SAME with each call to insertNode

Insert Node 1
 address in newNodePtr = 10371104 = address OF the NEW NODE
 address in newNodePtr->next = 0 
 address in *head = 0 
 address in head = -1625641784 
Finally address OF head = -1625641784 
Finally address in head = *head = 10371104 = address OF the NEW NODE
Insert Node 2
 address in newNodePtr = 10371136 = address OF the NEW NODE
 address in newNodePtr->next = 10371104 
 address in *head = 10371104 
 address in head = -1625641784 
Finally address OF head = -1625641784 
Finally address in head = *head = 10371136 = address OF the NEW NODE
Insert Node 3
 address in newNodePtr = 10371168 = address OF the NEW NODE
 address in newNodePtr->next = 10371136 
 address in *head = 10371136 
 address in head = -1625641784 
Finally address OF head = -1625641784 
Finally address in head = *head = 10371168 = address OF the NEW NODE
Insert Node 4
 address in newNodePtr = 10371200 = address OF the NEW NODE
 address in newNodePtr->next = 10371168 
 address in *head = 10371168 
 address in head = -1625641784 
Finally address OF head = -1625641784 
Finally address in head = *head = 10371200 = address OF the NEW NODE
Insert Node 5
 address in newNodePtr = 10371232 = address OF the NEW NODE
 address in newNodePtr->next = 10371200 
 address in *head = 10371200 
 address in head = -1625641784 
Finally address OF head = -1625641784 
Finally address in head = *head = 10371232 = address OF the NEW NODE
value in nodes is 5 
value in nodes is 4 
value in nodes is 3 
value in nodes is 2 
value in nodes is 1 

Monday, July 9, 2018

 ; x8086 alp to convert a BCD into its BINARY equivalent
DATA_SEG SEGMENT
    BCD DB 25H                  ; STORAGE FOR A BCD VALUE
    BIN DB ?                    ; STORAGE FOR BINARY VALUE
DATA_SEG ENDS

CODE_SEG SEGMENT
    ASSUME CS:CODE_SEG,DS:DATA_SEG
    START:
    MOV AX,DATA_SEG
    MOV DS,AX
   
    MOV AH,BCD    
    MOV BH,AH
    AND BH,0FH
    AND AH,0F0H
    ROR AH,04
    MOV CL,10
    MOV AL,AH
    AND AX,00FFH
    MUL CL
    ADD AL,BH
    MOV BIN,AL 
    MOV AH,04CH
    INT 21H
    CODE_SEG ENDS
END START

output, variable BIN gets binary value 00011001 = 25 in decimals.

Tuesday, July 3, 2018

Java: Standard Input / Output

case1: input from same line, out in different lines
import java.util.*;

public class Stdio1 {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        for(int i=0;i<3;i++)
        {
            int a = scan.nextInt();  //Takes input from the same line
            System.out.println(a);   //but gives out each number in separate lines
        }
      
    }
}

case 2: input from same line and output in same line without gap
import java.util.*;

public class Stdio2 {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        for(int i=0;i<3;i++)
        {
            int a = scan.nextInt();  //Takes input from the same line
            System.out.print(a);   //gives out each number in same line without any space

           
        }
      
    }
}

case 3: input from same line and output in same line , with space
import java.util.*;

public class Stdio3 {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        for(int i=0;i<3;i++)
        {
            int a = scan.nextInt();  //Takes input from the same line
            System.out.print(a+" ");   //gives out each number in same line with  space

           
        }
      
    }
}

case 4: Input from different lines and output also on different lines
import java.util.*;

public class Stdio4 {

    public static void main(String[] args) {
        int a=0;
        int arr[] = new int[3];
        Scanner scan = new Scanner(System.in);
        for(int i=0;i<3;i++)
        {
             a = scan.nextInt();  //Takes input from separate lines
             arr[i]=a;

           
        }
        for(int i=0;i<3;i++)
        {
             System.out.println(arr[i]);   //outputs in separate lines also
        }
       
    }
}

case 5 Input from different lines , but output in same line with space
import java.util.*;

public class Stdio5 {

    public static void main(String[] args) {
        int a=0;
        int arr[] = new int[3];
        Scanner scan = new Scanner(System.in);
        for(int i=0;i<3;i++)
        {
             a = scan.nextInt();  //Takes input from separate lines
             arr[i]=a;

           
        }
        for(int i=0;i<3;i++)
        {
             System.out.print(arr[i]+" ");   //outputs in same line with space
        }
       
    }
}


Monday, July 2, 2018

C program to replace a given line in a file



/*This C program is used to replace a specific line in a text file. */

#include <stdio.h>
int main(void) {
    FILE *fp1, *fp2;
    //'filename'is a 40 character string to store filename
    char filename[40];
    char c;
    int del_line, temp = 1;
    //asks user for file name
    printf("Enter file name: ");
    //receives file name from user and stores in 'filename'
    scanf("%s", filename);
    fp1 = fopen(filename, "r");
    //open file in read mode
    c = getc(fp1);
    //print the contents of file .
    while (c != EOF) {
        printf("%c", c);
        c = getc(fp1);
    }
    //ask user for line number to be deleted.
    printf(" Enter line number to be deleted and replaced");
    scanf("%d", &del_line);
    //take fp1 to start point.
    rewind(fp1);
    //open copy.c in write mode
    fp2 = fopen("copy.c", "w");
    c = getc(fp1);
while (c != EOF) {
        if (c == '') {
            temp++;
        }
        //till the line to be deleted comes,copy the content from one file to other
        if (temp != del_line){
        putc(c, fp2);
        }
        else //when the line to be deleted comes
        {
            while ((c = getc(fp1)) != '') {
            }
            //read and skip the line ask for new text
            printf("Enter new text");
            //flush the input stream
            fflush(stdin);
            putc('', fp2);  //put '' in new file
            while ((c = getchar()) != ''){
                putc(c, fp2);
                //take the data from user and place it in new file
                fputs("
                ", fp2);
                temp++;
            }
            //continue this till EOF is encountered
            c = getc(fp1);
            }
        //close both files
        fclose(fp1);
        fclose(fp2);
        //remove original file
        remove(filename);
        //rename new file with old name opens the file in read mode
        rename("copy.c", filename);
        fp1 = fopen(filename, "r");
        //reads the character from file
        c = getc(fp1);
        //until last character of file is encountered
        while (c != EOF){
            printf("%c", c);
            //all characters are printed
            c = getc(fp1);
        }
        //close the file pointer
        fclose(fp1);
        return 0;
} 
}

C program to show how user authentication is done

    /*

     * C program is to illustrate how user authentication is done.

     * Program asks for the user name and password and displays

     * the password as '*' character

     */

    #include <stdio.h>



    void main()

    {

        char password[31], username[31], ch;

        int i;



        printf("Enter User name: ");

        gets(username);

        printf("Enter the password < any exact 30 characters>: ");

        for (i = 0; i < 31; i++)

        {

                ch = getchar();

                password[i] = ch;

                ch = '*' ;

                printf("%c", ch);

        }

            password[i] = '\0';

        /*  Original password can be printed, if needed */

        printf("\n Your password is :");

        for (i = 0; i < 31; i++)

        {

                printf("%c", password[i]);

        }

    }

C program to find nCr and nPr



#include <stdio.h>

long factorial(int);
long find_ncr(int, int);
long find_npr(int, int);

int main()
{
   int n, r;
   long ncr, npr;

   printf("Enter the value of n and r\n");
   scanf("%d%d",&n,&r);

   ncr = find_ncr(n, r);
   npr = find_npr(n, r);

   printf("%dC%d = %ld\n", n, r, ncr);
   printf("%dP%d = %ld\n", n, r, npr);

   return 0;
}

long find_ncr(int n, int r) {
   long result;

   result = factorial(n)/(factorial(r)*factorial(n-r));

   return result;
}

long find_npr(int n, int r) {
   long result;

   result = factorial(n)/factorial(n-r);

   return result;
}

long factorial(int n) {
   int c;
   long result = 1;

   for (c = 1; c <= n; c++)
      result = result*c;

   return result;
}

C to find power of a number using recurrsion

#include <stdio.h>

int power(int n1, int n2);

int main()
{
    int base, powerRaised, result;

    printf("Enter base number: ");
    scanf("%d",&base);

    printf("Enter power number(positive integer): ");
    scanf("%d",&powerRaised);

    result = power(base, powerRaised);

    printf("%d^%d = %d", base, powerRaised, result);
    return 0;
}

int power(int base, int powerRaised)
{
    if (powerRaised != 0)
        return (base*power(base, powerRaised-1));
    else
        return 1;
}

C program to generate graycode

    /*

     * C Program to Convert Binary Code of a Number into its Equivalent

     * Gray's Code without using Recursion

     */

    #include <stdio.h>

    #include <math.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;



        while (bin != 0)

        {

            a = bin % 10;

            bin = bin / 10;

            b = bin % 10;

            if ((a && !b) || (!a && b))

            {

                result = result + pow(10, i);

            }

            i++;

        }

        return result;

    }

C program to remove duplicate elements from an array

#include <stdio.h>

int main()
{
    int arr[10], i, j, k, Size;

    printf("\n Please Enter Number of elements in an array  :   ");
    scanf("%d", &Size);

    printf("\n Please Enter %d elements of an Array \n", Size);
    for (i = 0; i < Size; i++)
    {
        scanf("%d", &arr[i]);
       }

    for (i = 0; i < Size; i++)
    {
        for(j = i + 1; j < Size; j++)
        {
            if(arr[i] == arr[j])
            {
                for(k = j; k < Size; k++)
                {
                    arr[k] = arr[k + 1];
                }
                Size--;
                j--;
            }
        }
    }

     printf("\n Final Array after Deleteing Duplicate Array Elements is:\n");
     for (i = 0; i < Size; i++)
      {
         printf("%d\t", arr[i]);
      }
     return 0;
}

C program to draw staircase

#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>

void staircase(int n) {
    
    char c='#';
    int x=n-1,y=1;

    for(int i=0;i<n;i++)
    {
        for(int j=x;j>0;j--)
      {
        printf(" ");

      }

        x--;

        for(int k=0;k<y;k++)
        {
            printf("%c",c);

        }
            y++;
            if(y==n+1)
            exit(0);
         printf("\n");
    }

}

int main() {
    int n;
    scanf("%i", &n);
    staircase(n);
    return 0;
}






C program to draw pascals triangle



#include <stdio.h>

long factorial(int);

int main()
{
   int i, n, c;

   printf("Enter the number of rows you wish to see in pascal triangle\n");
   scanf("%d",&n);

   for (i = 0; i < n; i++)
   {
      for (c = 0; c <= (n - i - 2); c++)
         printf(" ");

      for (c = 0 ; c <= i; c++)
         printf("%ld ",factorial(i)/(factorial(c)*factorial(i-c)));

      printf("\n");
   }

   return 0;
}

long factorial(int n)
{
   int c;
   long result = 1;

   for (c = 1; c <= n; c++)
         result = result*c;

   return result;
}

C palindrome test using pointers

/* program tests a string for palindrome using pointer notation */
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

int main(void)
{
  char *palin,c;
  int i=0, count=0;
  printf("Please enter a word \n");
  short int palindrome(char *,int);       /* Function prototype *  has a bug first argument should be char */
  palin = (char*)malloc(20*sizeof(char));
  printf("Enter a word\n");

  do
  {
     c=getchar();
     palin[i]=c;
     i++;
  }while(c != '\n');
  printf("i=%d\n",i );
  i=i-1;
  palin[i]='\0';
  count =i;

  if(palindrome(palin,count)==1)
        printf("Entered word is not a palindrome\n");
  else
        printf("Entered word is  a palindrome\n");


}

short int palindrome(char * palin, int len)
{
    short int i=0,j=0;
    for(i=0;j=len-1,i<len/2;i++,j--)
    {
        if(palin[i]==palin[j])
            continue;
        else
            return(1);
    }
    return(0);

}

C program to Reversing String without using array

//REVERSING A KNOWN STRING without using ARRAYs

/*If you use sizeof()then a char *str and char str[] will return different answers.
char str[] will return the length of the string(including the string terminator)
while char *str will return the size of the pointer(differs as per compiler).


*/

#include<stdio.h>
#include<string.h>
int main()
{

    char *c= "Hello world";
    int n=0,i=0;
    while(*(c+i)!='\0'){
        n++;
        i++;

    }

    for(int i=(n-1);i>=0;i--)
    {
        printf("%c",*(c+i));
    }
return 0;
}

C program to create transpose of a matrix

    /*

     * C program to accept a matrix of order MxN and find its transpose

     */

    #include <stdio.h>



    void main()

    {

        static int array[10][10];

        int i, j, m, n;



        printf("Enter the order of the matrix \n");

        scanf("%d %d", &m, &n);

        printf("Enter the coefiicients of the matrix\n");

        for (i = 0; i < m; ++i)

        {

            for (j = 0; j < n; ++j)

            {

                scanf("%d", &array[i][j]);

            }

        }

        printf("The given matrix is \n");

        for (i = 0; i < m; ++i)

        {

            for (j = 0; j < n; ++j)

            {

                printf(" %d", array[i][j]);

            }

            printf("\n");

        }

        printf("Transpose of matrix is \n");

        for (j = 0; j < n; ++j)

        {

            for (i = 0; i < m; ++i)

            {

                printf(" %d", array[i][j]);

            }

            printf("\n");

        }

    }


C program if character is a Vowel



#include <stdio.h>

int main()
{
  char ch;

  printf("Input a character\n");
  scanf("%c", &ch);

  if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' &&ch <= 'Z'))
{
    if (ch=='a' || ch=='A' || ch=='e' || ch=='E' || ch=='i' || ch=='I' || ch=='o' || ch=='O' || ch== 'u' || ch=='U')
      printf("%c is a vowel.\n", ch);
    else
      printf("%c is a consonant.\n", ch);
  }
  else
    printf("%c is neither a vowel nor a consonant.\n", ch);

  return 0;
}

C program to check if given matrix is a upper triangle

// Function to check matrix is in upper triangular
// form or not.
#include<stdio.h>
int N,N;
int isUpperTriangularMatrix(int mat[N][N])
{
    for (int i = 1; i < N; i++)
        for (int j = 0; j < i; j++)
            if (mat[i][j] != 0)
                return 0;
    return 1;
}

// Driver function.
int main()
{
    int N=N=4;
    int mat[4][4] = { { 1, 3, 5, 3 },
                      { 0, 4, 6, 2 },
                      { 0, 0, 2, 5 },
                      { 0, 0, 0, 6 } };
    if (isUpperTriangularMatrix(mat))
        printf( "Yes");
    else
        printf("No");
    return 0;
}

C program convert a decimal number into binary number using reccursion

    /*

     * C Program to Convert a Number Decimal System to Binary System using Recursion

     */

    #include <stdio.h>



    int convert(int);



    int main()

    {

        int dec, bin;



        printf("Enter a decimal number: ");

        scanf("%d", &dec);

        bin = convert(dec);

        printf("The binary equivalent of %d is %d.\n", dec, bin);



        return 0;

    }



    int convert(int dec)

    {

        if (dec == 0)

        {

            return 0;

        }

        else

        {

            return (dec % 2 + 10 * convert(dec / 2));

        }

    }

C program passing 2D array

/* When both dimensions are available globally (either as a macro or as a global constant).*/
#include <stdio.h>
const int M = 3;
const int N = 3;

void print(int arr[M][N])
{
    int i, j;
    for (i = 0; i < M; i++)
      for (j = 0; j < N; j++)
        printf("%d ", arr[i][j]);
}

int main()
{
    int arr[][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
    //int arr[][N] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};  // error : variable-sized object may not be initialized
    print(arr);
   // print(&arr[0][0]); //works with warning expected 'int(*)[(sizetype)N]' but argument is of type ' int *'
    //print((int *)arr); //works with warning expected 'int(*)[(sizetype)N]' but argument is of type ' int *'
   // print((int*)arr[0][0]); // error : abrupt termination of program
    return 0;
}

C program to count number of bits in a decimal number

#include <stdio.h>

#include<stdint.h>

void main()

{

  unsigned long  long int num, decimal_num, remainder, base = 1, binary = 0, no_of_1s = 0,no_of_0s=0;
    //int64_t num, decimal_num, remainder, base = 1, binary = 0, no_of_1s = 0,no_of_0s=0;



    printf("Enter a decimal integer \n");

    scanf("%lld", &num);

    decimal_num = num;

    while (num > 0)

    {

        remainder = num % 2;

        /*  To count no.of 1s */

        if (remainder == 1)

        {

            no_of_1s++;

        }
        else{
            no_of_0s++;
        }

        binary = binary + remainder * base;

        num = num / 2;

        base = base * 10;

    }

    printf("Input number is = %lld\n", decimal_num);

    printf("Its binary equivalent is = %lld\n", binary);

    printf("No.of 1's in the binary number is = %lld\n", no_of_1s);

    printf("No.of bits's in its binary equivalent is = %lld\n", no_of_1s+no_of_0s);
}

C program to insert element in an array

#include <stdio.h>

int main()

{

    int  a[25];

    int  i, j, num, m, t, key, pos;



    printf("Please enter number of elements\n");

    scanf("%d", &num);



    printf("Enter the elements one by one\n");

    for(i=0; i<num; i++)

    {

         scanf("%d", &a[i]);

    }



    printf("Input array elements:\n");

    for(i=0; i<num; i++)

    {

         printf("%d\n", a[i]);

    }



    for(i=0; i< num; i++)

    {

         for(j=i+1; j<num; j++)

         {

               if (a[i] > a[j])

               {

                      t = a[i];

                      a[i] = a[j];

                      a[j] = t;

               }

         }

    }



    printf("Sorted array elements (list):\n");

    for(i=0; i<num; i++)

    {

         printf("%d\n", a[i]);

    }



    printf("Enter the element to be inserted\n");

    scanf("%d",&key);



    for(i=0; i<num; i++)

    {

         if ( key < a[i] )

        {

               pos = i;

               break;

        }

    }



    m = num - pos + 1 ;



    for(i=0; i<= m ; i++)

    {

           a[num-i+2] = a[num-i+1] ;

    }



    a[pos] = key;



    printf("Array elements (list) after inserting:\n");

    for(i=0; i<num+1; i++)

    {

          printf("%d\n", a[i]);

    }

}

C program to find LCM and HCF



#include <stdio.h>

int main() {
  int a, b, x, y, t, gcd, lcm;

  printf("Enter two integers\n");
  scanf("%d%d", &x, &y);

  a = x;  // 34
  b = y;   //4

  while (b != 0) {
    t = b;    // t =12,5
    b = a % b;    // b= 65/12=0,2
    a = t;    // a= 12,5
  }

  gcd = a;
  lcm = (x*y)/gcd;

  printf("Greatest common divisor of %d and %d = %d\n", x, y, gcd);
  printf("Least common multiple of %d and %d = %d\n", x, y, lcm);

  return 0;
}

C program to find frequency of each element in array

/**
 * C program to count frequency of each element of array
 */

#include <stdio.h>

int main()
{
    int arr[100], freq[100];
    int size, i, j, count;

    /* Input size of array */
    printf("Enter size of array: ");
    scanf("%d", &size);

    /* Input elements in array */
    printf("Enter elements in array: ");
    for(i=0; i<size; i++)
    {
        scanf("%d", &arr[i]);

        /* Initially initialize frequencies to -1 */
        freq[i] = -1;
    }


    for(i=0; i<size; i++)
    {
        count = 1;
        for(j=i+1; j<size; j++)
        {
            /* If duplicate element is found */
            if(arr[i]==arr[j])
            {
                count++;

                /* Make sure not to count frequency of same element again */
                freq[j] = 0;
            }
        }

        /* If frequency of current element is not counted */
        if(freq[i] != 0)
        {
            freq[i] = count;
        }
    }

    /*
     * Print frequency of each element
     */
    printf("\nFrequency of all elements of array : \n");
    for(i=0; i<size; i++)
    {
        if(freq[i] != 0)
        {
            printf("%d occurs %d times\n", arr[i], freq[i]);
        }
    }

    return 0;
}

C program to generate Fibonacci Numbers

     /*

     * C program to generate Fibonacci Series. Fibonacci Series

     * is 0 1 1 2 3 5 8 13 21 ...

     */

    #include <stdio.h>



    void main()

    {

        int  fib1 = 0, fib2 = 1, fib3, limit, count = 0;



        printf("Enter the limit to generate the Fibonacci Series \n");

        scanf("%d", &limit);

        printf("Fibonacci Series is ...\n");

        printf("%d\n", fib1);

        printf("%d\n", fib2);

        count = 2;

        while (count < limit)

        {

            fib3 = fib1 + fib2;

            count++;

            printf("%d\n", fib3);

            fib1 = fib2;

            fib2 = fib3;

        }

    }

C program to draw Dimond pattern



#include <stdio.h>

int main()
{
  int n, c, k, space = 1;

  printf("Enter number of rows\n");
  scanf("%d", &n);

  space = n - 1;

  for (k = 1; k <= n; k++)
  {
    for (c = 1; c <= space; c++)
     {

      printf(" ");
     }

    space--;

    for (c = 1; c <= 2*k-1; c++)
     {

      printf("*");
     }

    printf("\n");
  }

  space = 1;

  for (k = 1; k <= n - 1; k++)
  {
    for (c = 1; c <= space; c++)
      printf(" ");

    space++;

    for (c = 1 ; c <= 2*(n-k)-1; c++)
      printf("*");

    printf("\n");
  }

  return 0;
}

C program to draw a Digonal of entered numbre

#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>

void staircase(int n) {
   
    char c='#';


        for(int j=n;j>0;j--)
      { printf("%*c\n",j,c);

      }


}

int main() {
    int n;
    scanf("%i", &n);
    staircase(n);
    return 0;
}

C program to count number of lines in a file

#include <stdio.h>
#define MAX_FILE_NAME 100

int main()
{
    FILE *fp;
    int count = 0;  // Line counter (result)
    char filename[MAX_FILE_NAME];
    char c;  // To store a character read from file

    // Get file name from user. The file should be
    // either in current folder or complete path should be provided
    printf("Enter file name: ");
    scanf("%s", filename);

    // Open the file
    fp = fopen(filename, "r");

    // Check if file exists
    if (fp == NULL)
    {
        printf("Could not open file/file does not exist %s", filename);
        return 0;
    }

    // Extract characters from file and store in character c
    for (c = getc(fp); c != EOF; c = getc(fp))
        if (c == '\n') // Increment count if this character is newline
            count = count + 1;

    // Close the file
    fclose(fp);
    printf("The file %s has %d lines\n ", filename, count);

    return 0;
}

C program to count number of letters in a word

#include<stdio.h>

int main(){
    int count=0,i=0;
printf("Type a word  ");
char word[50];
scanf("%s",&word);
printf("\nYou typed the word  \'%s\' \n",word);
while(word[i]!='\0'){
    count++;
    i++;
}
printf("\nTotal number of letters in %s are %d\n",word,count);
return 0;
}

C program to concatanate Strings



#include <stdio.h>

void concatenate(char [], char []);

int main()
{
   char p[100], q[100];

   printf("Input a string\n");
   gets(p);

   printf("Input a string to concatenate\n");
   gets(q);

   concatenate(p, q);

   printf("String obtained on concatenation: \"%s\"", p);

   return 0;
}

void concatenate(char p[], char q[]) {
   int c, d;

   c = 0;

   while (p[c] != '\0') {
      c++;
   }

   d = 0;

   while (q[d] != '\0') {
      p[c] = q[d];
      d++;
      c++;
   }

   p[c] = '\0';
}

C program to check if number is Palindrome

#include <stdio.h>
int main()
{
    int n, reversedInteger = 0, remainder, originalInteger;

    printf("Enter an integer: ");
    scanf("%d", &n);

    originalInteger = n;

    // reversed integer is stored in variable
    while( n!=0 )
    {
        remainder = n%10;
        reversedInteger = reversedInteger*10 + remainder;
        n /= 10;
    }

    // palindrome if orignalInteger and reversedInteger are equal
    if (originalInteger == reversedInteger)
        printf("%d is a palindrome.", originalInteger);
    else
        printf("%d is not a palindrome.", originalInteger);

    return 0;
}

C program to check if Leap year

    /*

     * C program to find whether a given year is leap year or not

     */
     #include<stdio.h>

    void main()

    {

        int year;



        printf("Enter a year \n");

        scanf("%d", &year);

        if ((year % 400) == 0)

            printf("%d is a leap year \n", year);

        else if ((year % 100) == 0)

            printf("%d is a not leap year \n", year);

        else if ((year % 4) == 0)

            printf("%d is a leap year \n", year);

        else

            printf("%d is not a leap year \n", year);

    }

C program to check if Prime number and Arstrong Number

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

int checkPrimeNumber(int n);
int checkArmstrongNumber(int n);

int main()
{
    int n, flag;

    printf("Enter a positive integer: ");
    scanf("%d", &n);

    // Check prime number
    flag = checkPrimeNumber(n);
    if (flag == 1)
        printf("%d is a prime number.\n", n);
    else
        printf("%d is not a prime number.\n", n);

    // Check Armstrong number
    flag = checkArmstrongNumber(n);
    if (flag == 1)
        printf("%d is an Armstrong number.", n);
    else
        printf("%d is not an Armstrong number.",n);
    return 0;
}

int checkPrimeNumber(int n)
{
    int i, flag = 1;

    for(i=2; i<=n/2; ++i)
    {

    // condition for non-prime number
        if(n%i == 0)
        {
            flag = 0;
            break;
        }
    }
    return flag;
}

int checkArmstrongNumber(int number)
{
    int originalNumber, remainder, result = 0, n = 0, flag;

    originalNumber = number;

    while (originalNumber != 0)
    {
        originalNumber /= 10;   //originalNumber = originalNumber/10
        ++n;
    }

    originalNumber = number;

    while (originalNumber != 0)
    {
        remainder = originalNumber%10;
        result += pow(remainder, n);
        originalNumber /= 10;
    }

    // condition for Armstrong number
    if(result == number)
        flag = 1;
    else
        flag = 0;

    return flag;
}

C program for Matrix multiplicatiion

// C program to multiply two square matrices.
#include <stdio.h>
#define N 4

// This function multiplies mat1[][] and mat2[][],
// and stores the result in res[][]
void multiply(int mat1[][N], int mat2[][N], int res[][N])
{
    int i, j, k;
    for (i = 0; i < N; i++)
    {
        for (j = 0; j < N; j++)
        {
            res[i][j] = 0;
            for (k = 0; k < N; k++)
                res[i][j] += mat1[i][k]*mat2[k][j];
        }
    }
}

int main()
{
int mat1[N][N]=   { {1, 1, 1, 1},
                    {2, 2, 2, 2},
                    {3, 3, 3, 3},
                    {4, 4, 4, 4}};

int mat2[N][N] = {  {1, 1, 1, 1},
                    {2, 2, 2, 2},
                    {3, 3, 3, 3},
                    {4, 4, 4, 4}};

    int res[N][N]; // To store result
    int i, j;
    multiply(mat1, mat2, res);

    printf("Result matrix is \n");
    for (i = 0; i < N; i++)
    {
        for (j = 0; j < N; j++)
           printf("%d ", res[i][j]);
        printf("\n");
    }

    return 0;
}

C program to check if number is Automorphic

#include<stdio.h>
  int main()
  {
    long int n,num,sqr,rem=0;
    int temp,ans;
    printf("Enter a number: ");
    scanf("%ld",&n);
    num=n;
    sqr=n*n;
    temp=10;
    printf("square of %ld is %ld ",n,sqr);
    while(n>0)
    {
       rem=sqr%temp;   //gives units place digit of square of given number
       if(num==rem)
       {
           ans=1;
           break;
      }
      n=n/10;
       temp=temp*10;
    }
    if(ans==1)
    {
       printf("\n %d is automorphic",num);
    }
    else
    {
       printf("\n %d is not automorphic",num);
    }
   return 0;
 }

Sacred Thought

26 April 2024  Dear friends, I write the explanation of two verses of Geets for all of you, I hope you all will like it and benefit from it....