Tuesday, February 26, 2019

Demonistration of function using Call By Reference

Function : A function is a group of statements that together perform a task.A function declaration tells the compiler about a function's name, return type, and parameters. A function definition provides the actual body of the function.

File Name : callbyreference.c
//program to swap two numbers using functions - callbyreference

#include<stdio.h>
 void main()
{
  int a=10,b=50;
  void swap(int *,int *);
  clrscr();
  printf("Before Swapping: %d %d\n",a,b);
  swap(&a,&b);
  printf("After Swapping: %d %d\n",a,b);
}
void swap(int *a,int *b)
{
  int t;
  t=*a;
  *a=*b;
  *b=t;
}

Output :
Before Swapping: 10 50
After Swapping: 50 10

program to perform various String Operations - in C Language using Ubuntu

File Name : string.c

#include <stdio.h>
#include <string.h>
void main()
{
    char str1[30],str2[30];
    int choice,result=0;
    int len1=0,len2=0;
    printf("\n Enter First String : ");
    scanf("%[^\n]",str1);
    printf("\n Enter Second String : ");
    scanf("%s",str2);
    printf("\n Enter Your Choice : \n");
    printf("1. Copy Strings. \n");
    printf("2. Compare Strings. \n");
    printf("3. Concatenate Strings. \n");
    printf("4. Find Lengths. \n");
    printf("\n Enter Your Choice :");
    scanf("%d",&choice);
    switch(choice)
    {
       case 1 :
         {
        strcpy(str1,str2);
          //strcpy(pos1,pos2);
        printf("\n %s %s \n",str1,str2);
        break;
         }
       case 2 :
         {
        result = strcmp(str2,str1);
        if(result == 0)
           printf("\n Both Strings are Same. ");
        else
           printf("\n Both are different strings.");

        break;
         }
       case 3 :
         {
        strcat(str1,str2);
          //strcat(pos1,pos2);
        printf("\n %s %s \n",str1,str2);
        break;
         }
       case 4 :
         {
        len1 = strlen(str1);
        printf("\n Legth of First String is %d \n",len1);
        len2 = strlen(str2);
        printf("\n Legth of Second String is %d \n",len2);
        break;
         }
     default : printf("\n Wrong Choice .");
    }
   
}

Output1:
Enter First String : guru
Enter Second String : nath
 Enter Your Choice :
1. Copy Strings.
2. Compare Strings.
3. Concatenate Strings.
4. Find Lengths.
Enter Your Choice :1
nath nath

Note : here strcpy function copies string in position2 copies to string in position1

Output 2:
Enter First String : guru
Enter Second String : nath
Enter Your Choice :
1. Copy Strings.
2. Compare Strings.
3. Concatenate Strings.
4. Find Lengths.
 Enter Your Choice :2
 Both are different strings.

Enter First String : guru
Enter Second String : guru
Enter Your Choice :
1. Copy Strings.
2. Compare Strings.
3. Concatenate Strings.
4. Find Lengths.
Enter Your Choice :2
Both Strings are Same.

Output 3:
Enter First String : guru
Enter Second String : nath
Enter Your Choice :
1. Copy Strings.
2. Compare Strings.
3. Concatenate Strings.
4. Find Lengths.
Enter Your Choice :3
gurunath nath

Enter First String : nath
Enter Second String : guru
Enter Your Choice :
1. Copy Strings.
2. Compare Strings.
3. Concatenate Strings.
4. Find Lengths.
Enter Your Choice :3
nathguru guru

 Note : strcat() function string in position1 will be concatinated with string in position2

Output 4:
Enter First String : guru
Enter Second String : nammu
Enter Your Choice :
1. Copy Strings.
2. Compare Strings.
3. Concatenate Strings.
4. Find Lengths.
Enter Your Choice :4
Legth of First String is 4
Legth of Second String is 5

Friday, February 15, 2019

Multiplication of two Matrices in C - Language Using Ubuntu

File Name : mulmat.c

#include <stdio.h>
void main()
{
 int m,n,p,q,c,d,k,sum=0;
 int f[10][10],s[10][10],mul[10][10];
 printf("Enter the number of rows and columns of First Matrix\n");
 scanf("%d%d",&m,&n);
 printf("Enter the elements of First Matrix\n");
 for(c=0;c<m;c++)
    for(d=0;d<n;d++)
       scanf("%d",&f[c][d]);
 printf("Enter the number of rows and columns of Second Matrix\n");
 scanf("%d%d",&p,&q);
 if(n!=p)
    printf("The matrices multiplication is not possible.\n");
 else
 {
  printf("Enter the elements of Second Matrix\n");
  for(c=0;c<p;c++)
    for(d=0;d<q;d++)
        scanf("%d",&s[c][d]);    
  for(c=0;c<m;c++)
     {
      for(d=0;d<q;d++)
         {
          for(k=0;k<p;k++)
             {
               sum=sum+f[c][k]*s[k][d];
             }
          mul[c][d]=sum;
          sum = 0;
         }
     }
 printf("Product of Matrices is:\n");
 for(c=0;c<m;c++)
    {  
     for(d=0;d<q;d++)
         printf("%d\t", mul[c][d]);
         printf("\n");
    }
 }
}

Output 1:
Enter the number of rows and columns of First Matrix
2
2
Enter the elements of First Matrix
1
2
3
4
Enter the number of rows and columns of Second Matrix
2
2
Enter the elements of Second Matrix
1
2
3
4
Product of Matrices is:
7    10   
15    22

Output 2:
Enter the number of rows and columns of First Matrix
2
2
Enter the elements of First Matrix
1
2
3
4
Enter the number of rows and columns of Second Matrix
4
5
The matrices multiplication is not possible.

Sum of two Matrices - in C - Language Using Ubuntu

File Name : summat.c

#include <stdio.h>
void main()
{
 int r1,c1,r2,c2,c,d,f[10][10],s[10][10],sum[10][10];
 printf("Enter the number of rows and columns of First Matrix\n");
 scanf("%d%d",&r1,&c1);
 printf("Enter the number of rows and columns of Second Matrix\n");
 scanf("%d%d",&r2,&c2);
 if(r1==r2 && c1==c2)
 {
 printf("Enter the elements of First Matrix\n");
 for(c=0;c<r1;c++)
    for(d=0;d<c1;d++)
       scanf("%d",&f[c][d]);
 printf("Enter the elements of Second Matrix\n");
 for(c=0;c<r2;c++)
    for(d=0;d<c2;d++)
        scanf("%d",&s[c][d]);
 printf("Sum of Two Matrices:\n");
 for(c=0;c<r1;c++)  //we may use r1,c1 combinatio or r2,c2.
 {
  for(d=0;d<c1;d++)
     {
       sum[c][d]=f[c][d]+s[c][d];
       printf("%d\t", sum[c][d]);
     }
  printf("\n");
  }
 }
 else
 {
  printf("Matrix addition is not possible...\n");
 }
}

Output1 :
Enter the number of rows and columns of First Matrix
2
2
Enter the number of rows and columns of Second Matrix
2
2
Enter the elements of First Matrix
1
2
3
4
Enter the elements of Second Matrix
1
2
3
4
Sum of Two Matrices:
2    4   
6    8   

Output 2:
Enter the number of rows and columns of First Matrix
2
4
Enter the number of rows and columns of Second Matrix
4
6
Matrix addition is not possible...

Friday, February 8, 2019

Bubble Sort using "C-Language" in Ubuntu

Sorting : Arranging the elements(numbers) either in ascending or descending order .
Bubble Sort:  is a simple sorting algorithm that repeatedly steps through the list, compares adjacent pairs and swaps them if they are in the wrong order. The pass through the list is repeated until the list is sorted.

File Name : bobsrt.c
//12.Write a C program to sort a given list of integers in ascending order(Bubble Sort)
#include<stdio.h>
void main()
{
 int n,c,d,swap,a[10];
 printf("Input number of integers to sort:\n");
 scanf("%d",&n);
 printf("Enter %d integers:\n",n);
 for(c=0;c<n;c++)
    scanf("%d",&a[c]);
 for(c=0;c<(n-1);c++)
 {
  for(d=0;d<n-c-1;d++)
     {
      if(a[d]>a[d+1]) /* For descending order use < */
        {
          swap = a[d];
          a[d] = a[d+1];
          a[d+1] = swap;
        }
      }
 }
    printf("Sorted list of numbers:\n");
    for(c=0;c<n;c++)
        printf("%d\n",a[c]);
}


Output:
Input number of integers to sort:
4
Enter 4 integers:
23
5
225
1
Sorted list of numbers:
1
5
23
225

Binary Search - using "C-Language" in Ubuntu

Binary search is the most popular Search algorithm.It is efficient and also one of the most commonly used technique. it works only on a sorted set of elements. 
Search a sorted array by repeatedly dividing the search interval in half. Begin with an interval covering the whole array. If the value of the search key is less than the item in the middle of the interval, narrow the interval to the lower half. Otherwise narrow it to the upper half. Repeatedly check until the value is found or the interval is empty.
File Name : bsearch.c
 
#include<stdio.h>
void main()
{
 int n,i,l=0,h,m=0,f=0,sk,a[10];
 printf("Enter number of elements: ");
 scanf("%d",&n);
 h=n-1;
 printf("Enter %d integers :\n",n);
 //Loop to store each numbers in array
 for(i=0;i<n;i++)
     scanf("%d",&a[i]);
 printf("Enter the search value: ");
 scanf("%d",&sk);
 while(l<=h)
      {
    m=(l+h)/2;
    if(sk==a[m])
    {
     f=1;break;
    }
    else if(sk<a[m])
         h=m-1;
    else
             l=m+1;
      }
 if(f==1)
   printf("%d Found at Index %d",sk,m);
 else
  printf("Search Key Not Found");
}


Output1:
Enter number of elements: 4
Enter 4 integers :
12
34
56
78
Enter the search value: 78
78 Found at Index 3

Output2:
Enter number of elements: 4
Enter 4 integers :
12
354
567
2345
Enter the search value: 5
Search Key Not Found

Thursday, February 7, 2019

Linear or Sequential Search using "C-Language" in Ubuntu

File Name : lsearch.c

#include<stdio.h>
void main()
{
 int c,n,sv,a[50];
 printf("Enter number of elements: ");
 scanf("%d",&n);
 printf("Enter %d integers :\n",n);
 //Loop to store each numbers in array
 for(c=0;c<n;c++)
     scanf("%d",&a[c]);
printf("Enter the search value: ");
scanf("%d",&sv);
for(c=0;c<n;c++)
    {
     if(a[c]==sv)
       {
        printf("%d is present at location %d\n",a[c],c+1);
        break;
       }
    }
if(c==n)
   printf("%d doesn't exist in array.\n",c);
}

Output:
Enter number of elements: 4
Enter 4 integers :                                                                                                                                 
12                                                                                                                                                 
5                                                                                                                                                   
66                                                                                                                                                 
8                                                                                                                                                   
Enter the search value: 5
5 is present at location 2

Display Smallest and Largest Elements in an Array - using "C-Language" in Ubuntu.

File Name :largesmall.c

#include<stdio.h>
void main()
{
 int a[50],i,n,s,l;
 printf("Enter size of Array: ");
 scanf("%d",&n);
 printf("Enter %d elements into the Array:\n",n);
 for(i=0;i<n;++i)
     scanf("%d",&a[i]);
 l=s=a[0];
 for(i=1;i<n;++i)
    {
     if(a[i]>l)
        l=a[i];
     if(a[i]<s)
        s=a[i];
    }
 printf("\nThe smallest element is %d\n",s);
 printf("The largest element is %d\n",l);
}

Output:
Enter size of Array: 3                                                                                                                             
Enter 3 elements into the Array:
12                                                                                                                                                 
3                                                                                                                                                 
67                                                                                                                                                 
The smallest element is 3
The largest element is 67

Generating Prime numbers using "C-Language" in Ubuntu.

Prime Number :
A prime number is a natural number greater than 1 that cannot be formed by multiplying two smaller natural numbers. (or)
A number that is divisible only by itself and 1, prime numbers are very useful in cryptography.
Ex : 2, 3, 5, 7, 11, 13, 17, 19, 23..etc

File Name : prime.c
#include<stdio.h>
void main()
{
int n,c=0,i,j;
printf("Enter a number to generate Prime Numbers Series:");
scanf("%d",&n);
printf("Generated Prime Numbers Between 1 and %d are:\n",n);
for(i=1; i<=n; i++)
    {
     c=0;
     for(j=1;j<=n;j++)
        {
         if(i%j==0)
           c++;
        }
        if(c==2)
          printf("%d\n",i);
    }

}

Output:
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, etc.

Generating Fibonacci sequence using "C-Language" in Ubuntu.

Fibonacci Sequence :
The Fibonacci sequence is a series where each number is the sum of the two preceding ones, starting from 0 and 1.
Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89

File Name : fibonacci.c

#include <stdio.h>
void main()
{
 int t1=0,t2=1,t3,n,i;
 printf("Enter number of terms to generate sequence: ");
 scanf("%d",&n);
 printf("Generated Fibonacci Series Of %d terms is:\n",n);
 for(i=1;i<=n;++i)
 {
  printf("%d\n",t1);
  t3=t1+t2;
  t1=t2;
  t2=t3;
 }
}

Output1:
Enter number of terms to generate sequence: 3
Generated Fibonacci Series Of 3 terms is:
0                                                                                                                                                 
1                                                                                                                                                 
1
Output1:
Enter number of terms to generate sequence: 6
Generated Fibonacci Series Of 6 terms is:
0                                                                                                                                                 
1                                                                                                                                                 
1
2
3
5

Sum Of Digits of Given Positive Integer - using "C-Language" in Ubuntu.

File Name : sumdigits.c
void main()
{
 int n,s=0,d;
 printf("Enter Any Positive Number(Integer): ");         
 scanf("%d",&n);               
 while(n!=0)
 {
  d=n%10;       
  s=s+d;       
  n=n/10;       
 }
 printf("The sum of the digits of given Number is %d\n",s);
}

Output1:
Enter Any Positive Number(Integer):145
The sum of the digits of given Number is 10
Output2:
Enter Any Positive Number(Integer):34567
The sum of the digits of given Number is 25

Wednesday, February 6, 2019

Given Number is Armstrong Number or Not - using "C-Language" in Ubuntu.

Armstrong Number(Or Narcissistic Number):
An Armstrong number is a number such that the sum of its digits raised to the no.of digits power is equal to the number itself.
Explnation:To declare a number is an Armstrong number, first determine how many digits are in the number. Call that n. Then take every digit in the number and raise it to the n power. Add all those together, and if your answer is the original number then it is an "Armstrong Number".
FileName : armstrong.c

#include <stdio.h>
void main()
{
  int n, t, rm, res=0; 
  printf("Enter a three digit integer: ");
  scanf("%d",&&n);
  t=n;
  while (t!=0)
  {                                             
    rm=t%10;             
    res=res+rm*rm*rm;     
    t=t/10;                     
  }
  if(res==n)
     printf("%d Is an Armstrong Number.",n);
  else
     printf("%d Is not an Armstrong Number.",n);
}

Output1:
Enter a three digit integer:153
153 Is an Armstrong Number.
Output2:
Enter a three digit integer:370
370 Is an Armstrong Number.
Output3:
Enter a three digit integer:371
371 Is an Armstrong Number.
Output4:
Enter a three digit integer:125
125 Is not an Armstrong Number.

Note 1:The above code works exclusively with 3-digit numbers, however you may play with the code by replacing the line "res=res+rm*rm*rm;", with your requirement.
Ex:
For  3-digit  ---> res=res+rm*rm*rm;
For 4-digit --->res=res+rm*rm*rm*rm;
For 5-digit --->res=res+rm*rm*rm*rm*rm;
and so on....
Note 2:
All single digit numbers are Armstrong numbers. For example, test 6. One digit.
 6^1 = 6. Armstrong number.
There are no 2-digit Armstrong numbers.
There are four 3-digit Armstrong numbers.
Ex : 153, 370, 371, 407
Test 371. Three digits. 3^3 + 7^3 + 1^3 = 27 + 343 + 1 = 371. Armstrong number.
There are three 4-digit Armstrong numbers.
Ex: Test 1634. Four digits. 1^4 + 6^4 + 3^4 + 4^4 = 1 + 1296 + 81 + 256 = 1634. Armstrong number.
Ex : 1634, 8208, 9474
There are three 5-digit Armstrong numbers.
Ex : 54748, 92727, 93084
Test : 5^5 + 4^5 + 7^5 + 4^5 + 8^5 = 3125 + 1024 + 16807 + 1024 + 32768 = 54748

Given Number is Perfect Or Not - using "C-Language" in Ubuntu.

//This code works in unix environment using cc, or gcc commands. 
//It works on windows with "Turbo C", with small alterations in code.
Perfect Number :
A perfect number is a positive integer that is equal to the sum of its proper positive divisors, that is, the sum of its positive divisors excluding the number itself. 
File Name : perfect.c
#include <stdio.h>
void main()
{
  int n,i,s=0;
  printf("Enter a Number :");
  scanf("%d",&n);              
  for(i=1;i<n;i++)
  {
    if(n%i==0)                 
    s=s+i;                      
  }
  if(s==n)
       printf("Given no is a Perfect Number");
  else
       printf("Given no is not a Perfect Number");
  }

Command To Compile in Ubuntu Terminal:

cc perfect.c

Note : The above command generates a.out(executable) file which can be executed as follows.


Command To Execute a.out file:

./a.out

Output1:

Enter a Number :6
Given no is a Perfect Number
Output2:
Enter a Number :28
Given no is a Perfect Number
Output3:
Enter a Number :53
Given no is not a Perfect Number

Hadoop Commands

HADOOP COMMANDS OS : Ubuntu Environment Author : Bottu Gurunadha Rao Created: 31-Jan-2022 Updated: 31-Jan-2022 Release  : 1.0.1 Purpose: To ...

Search This Blog