-->

Thursday, February 27, 2014

C# Programming: How to find pow of given number

You can easily find power of given number using Pow, which is static method of Math class. Here we take an example with integer number. Also gives II method , which is performed by programming.

Both method include in single program

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = 2, b = 4;   

            

            Console.WriteLine(" The power of given number is {0}",Math.Pow(a, b));
            // Second method
        int result= Fun(2, 6);
        Console.WriteLine(" The power of given number is {0}", result);
            Console.ReadKey();

        }

        private static int Fun(int a, int b)
        {
            if (b >= 1)
            {
                return (a * Fun(a, --b));
            }
            else
                return 1;
        }
      

    }
}

Code generate the following output

C# Programming: How to find pow of given number

c# programming : How to find largest number in given two number

You can easily find largest number between two number using Max, which is static method of Math class. This method is overload with multiple data types, such as Max(Byte, Byte) , Max(Decimal, Decimal) and many more data types. Here we take an example with integer number.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = -3, b = 52;
            Decimal deci = -4, deci1 = 53;

            Console.WriteLine(" the maximum number in between a= {0} and b={1} are {2}",a,b, Math.Max(a, b));

            Console.WriteLine(" the maximum number in between a= {0} and b={1} are {2}", deci, deci1, Math.Max(deci, deci1));

            Console.ReadKey();

        }
    }
}

Code generate the following output

c# programming : How to find largest number in given two number

C Program to reverse an ordered linked list for Data Structure for C Program

C Program to reverse an ordered linked list:

struct node
{
inf info;
struct node *link;
};typedef struct node sn;
sn*insert(sn *root, int i)
{
sn *new,*temp,*ptemp;
new=(sn*)malloc(sizeof(sn));
if(new= =NULL)
{
printf(“Memory allocation error…”);
exit(0);
}
new->info=I;new->link=NULL;
if(root= =NULL)
root=new;
else
if(i<root->info)
{
new->link=root;  root=new;
}
else
{
temp=root;
while(temp=NULL&&i>temp->info)
{
ptemp=temp;
temp=temp->link;
}
new->link=temp;
ptemp->link=new;
}return root; /*address of first node is returned*/
}
void traverse(sn *ptr)
{
while(ptr!=NULL)
{
printf(“%d”,ptr->info); ptr=ptr->link;
}
}
sn*reversell(sn *root)
{
sn *first=root,*pptr=root,*nptr;
nptr=nptr->link;
while(nptr!=NULL)
{
root=nptr;
nptr=nptr->link;
root->link=pptr;
pptr=root;
}
first->link=NULL;
return root;
}
main()
{
sn *root=NULL,*ptr;int info; char ch;
while(1)
{
printf(“/nEnter node information :”);
scanf(“%d”,&info);
root=insert(root,info);
printf(“\n Do you want to continue…(y/n)”);
ch=getche();
if(ch!=’y’)
 break;
}
printf(“\nOrdered (Asc)Singly Linked List :\n\n”);
treverse(root);getch();
printf(“\n\n The reversed (desc)linked list :\n\n”);
root=reversell(root);
traverse(root); getch();
}

The same reversing function also can be applied to the circular linked list by just changing the required condition to test the last node’s link. In normal linked list it is NULL. In circular linked list it become the address of the first node itself, stored in external pointer(root).

Reversing the ordered Singly Linked List for Data Structure in'C'

Special Operation on linked lists:

Reversing the ordered Singly Linked List:
              In order to reverse the ordered singly linked list, the root must point to the last node of the list. The first node’s address must be copied to the link of second node. Similarly the address of each node is copied in the link of the next node. It works like exchanging the links of each node with the address of the previous node. The algorithm is simple and self-explanatory.
The same is given as follows:

REVERSELL (ROOT)
FIRST<--ROOT [To store the first node’s address]
PPTR<--ROOT        [To store the address in link]
NPTR<--ROOT-->LINK [To store address of previous node]
Repeat While NPTR< >NULL
  ROOT<--NPTR
  NPTR<--NPTR-->LINK
  ROOT-->LINK<--PPTR
  PPTR<--ROOT
[End of while]
FIRST-->LINK<--NULL [To make first node as last]
Return ROOT
Exit.

Wednesday, February 26, 2014

How to Use Operators with Strings in Java Programming

Operator + with strings

You have used the operator + with numbers. When you use + with numbers, the result is also a number. However, if you use operator + with strings, it concatenates them e.g.

5 + 6 results in to 11.
“5” + “6” results in to “56”.
“17” + “A, V. Vihar” result in to “17 A, V. Vihar”
“abc” + “123” results in to “abc 123”
“” + 5 + “xyz” results in to “5xyz”
“” + 5 results in to “5”

(In above two expressions Java would internally convert 5 in to “5” first and then concatenate with “xyz” and “” respectively.)

Increment/Decrement Operators (++, --)

Java includes two useful operators not generally found in other computer languages (expect C and C++). These are the increment and decrement operators, ++ and --. The operators ++ adds 1 to its operand, and – subtracts one.
In other words,
a = a + 1;
is the same as
++a ; or a++;
And
a = a – 1
is the same as
--a ; or a --;

However, both the increment and decrements come in to two varieties: they may either precede of=r fallow the operand. The prefix version comes before the operand (as in ++ a or -- a) and the post-fix version comes after the operand (as in a++ or a--). The two version have the same effect upon the operand, but they differ when they take place in an expression.

How to find absolute number of a given number in c# programming

In c# programming we easily find absolute number of a given number. We can use Abs function, which is include in Math class. Lets take an simple example

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            sbyte sortbyte1 = -12, sortbyte2 = 12;
            short shnumber1 = -13, shnumber2 = 13;
            int integer1 = -14, integer2 = 14;
            long long1 = -15, long2 = 15;
            float float1 = -16.0f, float2 = 16.0f;
            double double1 = -17.1, double2 = 17.1;
            Decimal decimal1 = -18.0m, decimal2 = 18.0m;

            Console.WriteLine();
            Console.WriteLine("SortByte:   1) {0,-2} {1,-2}", Math.Abs(sortbyte1), Math.Abs(sortbyte2));

            Console.WriteLine("Integer 16 bit:   1) {0,-2} 2) {1,-2}", Math.Abs(shnumber1), Math.Abs(shnumber2));

            Console.WriteLine("Integer 32 bit:   1) {0,-2} 2) {1,-2}", Math.Abs(integer1), Math.Abs(integer2));

            Console.WriteLine("Integer 64 bit:   1) {0,-2} 2) {1,-2}", Math.Abs(long1), Math.Abs(long2));

            Console.WriteLine("Single:  1) {0,-2} 2) {1,-2}", Math.Abs(float1), Math.Abs(float2));

            Console.WriteLine("Double:  1) {0,-2} 2) {1,-2}", Math.Abs(double1), Math.Abs(double2));

            Console.WriteLine("Decimal: 1) {0,-2} 2) {1,-2}", Math.Abs(decimal1), Math.Abs(decimal2));

            Console.ReadKey();

        }
    }
}
Here Math class provide Abs static method for returning absolute value of a given number. Abs function is overloaded by some data types like double, decimal, Int16, Int32 and SByte.
Code generate the following output
How to find absolute number of a given number in c# programming

Memory representation of Linked List Data Structures in C Language

                                 Memory representation of Linked List

             In memory the linked list is stored in scattered cells (locations).The memory for each node is allocated dynamically means as and when required. So the Linked List can increase as per the user wish and the size is not fixed, it can vary.

               Suppose first node of linked list is allocated with an address 1008. Its graphical representation looks like the figure shown below:


      Suppose next node is allocated at an address 506, so the list becomes,



  Suppose next node is allocated with an address with an address 10,s the list become,


The other way to represent the linked list is as shown below:




 In the above representation the data stored in the linked list is “INDIA”, the information part of each node contains one character. The external pointer root points to first node’s address 1005. The link part of the node containing information I contains 1007, the address of next node. The last node of the list contains an address 0, the invalid address or NULL address.

Example:

The data stored in the linked list is: “The Is Another:
 Example Of Linked List”

Q. What is stored in the linked list shown below?



Q. What is stored in the Linked List shown below?



© Copyright 2013 Computer Programming | All Right Reserved