Thursday, December 5, 2013

Sample C# program to find Happy or Sad numbers

Here is a sample C# program to find the given number is Happy or Sad.

What is Happy or Sad Numbers?

Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy numbers, while those that do not end in 1 are Sad numbers.

Example:

7 is a happy number: 7->49->97->130->10->1.
22 is NOT a happy number: 22->8->64->52->29->85->89->145->42->20->4->16->37->58->89
Code:
using System;
using System.Collections.Generic;

namespace HappyNumbers
{
    class Program
    {
        static void Main(string[] args)
        {
            while (true)
            {
                Console.WriteLine();
                Console.WriteLine("Enter a Number : ");
                string num = Console.ReadLine();
                Console.WriteLine();
                HappyOrSad hs = new HappyOrSad();
                if (hs.IsEqulOne(num))
                {
                    Console.WriteLine();
                    Console.WriteLine("Happy :) ");
                    Console.WriteLine();
                    Console.WriteLine("----------------------");
                }
                else
                {
                    Console.WriteLine();
                    Console.WriteLine("Sad :( ");
                    Console.WriteLine();
                    Console.WriteLine("----------------------");
                }
            }
        }
    }

    class HappyOrSad
    {
        public bool IsEqulOne(string numbr)
        {
            bool isOne = false;
            List<int> al = new List<int>();
            while (isOne == false)
            {
                int val = 0;
                char[] numArr = numbr.ToCharArray();
                foreach (char n in numArr)
                {
                    int nVal = Int32.Parse(n.ToString());
                    val += (nVal * nVal);
                }
                if (val == 1)
                {
                    al.Add(val);
                    isOne = true;
                    break;
                }
                else
                {
                    if (al != null)
                    {
                        if (al.Contains(val))
                        {
                            al.Add(val);
                            break;
                        }
                        else
                        {
                            al.Add(val);
                        }
                    }
                }
                numbr = val.ToString();
            }
            foreach (var item in al)
            {
                Console.Write(item + " -> ");
            }
            return isOne;
        }
    }
}

Sunday, December 1, 2013

Get the list of country code and names in c#

Here is a simple Linq code to retrive the list of country code and names
C# code:

public IEnumerable<Country> GetCountries()
{
    var countryList = from r in
                      from ci in CultureInfo.GetCultures(CultureTypes.SpecificCultures)
                      select new RegionInfo(ci.LCID)
                      group r by r.TwoLetterISORegionName into g
                      select new Country
                      {
                          Code = g.Key,
                          Name = g.First().DisplayName
                      };
    return countryList;
}
Also you need to add the below class
class Country
{
        public string Code { get; set; }
        public string Name { get; set; }
}

Monday, June 24, 2013

Salesforce - Pass parameter value from one page to another page

Here i have provided a sample to understand how to pass parameter value from one page to another and retrieve it. I have used two pages named Books and SelectedBooks. Books page has a PageBlockTable it dispalyes only book names from the Book_c custom object. The book names are displayed as links using <apex:CommandLink>. When a user click the link the first page Book is navigate to second page SelectedBook with the parameter value that is passed using <apex:param>. The Second page then retrieves the parameter value and displays the book details in a PageBlockTable
Design code for Books page:

By default the PageBlockTable displays its value as OutputLabel but we need to click the value for navigation purpose. So here i used <apex:commandLink> to convert it as Link. Book name is passed as parameter using <apex:param>

<apex:page controller="BooksController">  
   <apex:form >  
   <apex:sectionHeader subtitle="All Books" title="Books"/>  
      <apex:pageBlock >  
       <apex:pageBlockTable value="{!allbooks}" var="a">  
           <apex:column headervalue="Name">  
                 <apex:commandLink value="{!a.Name}" action="/apex/SelectedBook?id={!a.Name}">  
                 <apex:param name="id" value="{!a.Name}"/>  
                 </apex:commandLink>  
           </apex:column>  
             </apex:pageBlockTable>  
        </apex:pageBlock>  
   </apex:form>  
 </apex:page>  

Apex Code for Books page controller:

Here the getAllBooks fuction retrive the values from the Book__c custom objects and bind it to the PageBlockTable

 public class BooksController
 {  
      public List<Book__c> allbooks;   
      public List<Book__c> getAllBooks()  
      {  
           if(allbooks==null)  
           {  
                allbooks=[SELECT Name,Book_Author__c,Book_ISBN__c from Book__c];  
           }  
      return allbooks;  
      }  
 }  

Apex Code for SelectedBook page controller:

Here in the constructor the parameter value is retrieved using its name. Using that value the getSbook function gets the specific book details and bind it to the PageBlockTable.

 public class SelectedBookController 
 {  
      public String selectedName {get;set;}  
      public Book__c sbook;  
      public SelectedBookController()  
      {  
           sbook=new Book__c();  
           selectedName = ApexPages.currentPage().getParameters().get('id');  
      }  
       
      public Book__c getSbook()  
      {  
           sbook=[Select Name,Book_ISBN__c,Book_Author__c from Book__c where Name=:selectedName];  
           return sbook;  
      } 
 } 

Design code for SelectedBook page:

<apex:page controller="SelectedBookController">  
 <apex:form >  
      <apex:pageBlock >  
           <apex:pageBlockTable value="{!sbook}" var="b">  
                <apex:column value="{!b.Name}"/>  
                <apex:column value="{!b.Book_Author__c }"/>  
               <apex:column value="{!b.Book_ISBN__c}"/>  
             </apex:pageBlockTable>  
      </apex:pageBlock>  
 </apex:form>  
 </apex:page>