Friday, September 9, 2011

Find Binary Number Of Any Given Decimal Number


/*Find Binary Number Of Any Given Decimal Number (In Reverse Order) */


#include <stdio.h>
int main(void)
{
    int num,j,k = 0;
    printf("Enter a number whose binary you want to know :");
    scanf("%d",&num);
    j = num ;

    while ( j > 0)
    {
        k = j % 2;
        printf("%d ",k );
        j = j / 2 ;
        continue;
    }

    printf("\n");
    return 0;

}

Euclid's Geometric Position

/*According to Euclid's Geometric Position find Integer Factorization/Prime Factorization of Given Number */


#include <stdio.h>

int main(void)
{
    int i,num,j;
    printf("Enter a number whose Prime factors you want to find :");
    scanf("%d",&num);
    j = num ;    //Assigning value of num to j

    for ( i = 2 ; i <= j-1 ;i++)
    {
         while( num % i == 0)
         {
             printf("\n%d ",i );
             num = num / i ;
             continue;
         }
       
    }
 
   printf("\n");
   return 0;

 }


Monday, September 5, 2011

According to the Gregorian calendar,it was Monday on the date 01/01/1900.If any year is input through the keyboard write a program to find out what is the day on 1st January of this year



Solution code:-
=========


Easy One (Irrespective of difference between year input from year 1901) : 

/*According to the Gregorian calender, it was Monday on the date 01/01/01. If any year is input
 * through the keyboard write a program to find out what is the day on 1st January of this year?*/



#include <stdio.h>
int main(void)
{
    int yr,diff,lpyrdays,normaldays,res;
    printf("\nEnter a year whose day of 1st Jan you want to know : ");
    scanf("%d",&yr);
   
    yr = (yr - 1) ; //removing 1 year as only 1 day we are calculating of current yr
    lpyrdays =  (yr/4)  + (yr / 400) - (yr / 100 ); //Calculating leap days in that particular year
    normaldays = (yr* 365 )+ 1 + lpyrdays ; //Calculating normal days in that year & adding 1st jan's 1 day in
    res = normaldays % 7;
   
   
    if(res==0)
    printf("\nSunday");
    if(res==1)
    printf("Monday");
    if(res==2)
    printf("Tuesday");
    if(res==3)
    printf("Wednesday");
    if(res==4)
    printf("Thursday");
    if(res==5)
    printf("Friday");
    if(res==6)
    printf("Saturday");
   
    return 0;
   
   
   
   
}

Tough One (Solution Code):


#include <stdio.h>

int main(void)          //main function.. starting of c code
{
    int year,differ,lp_year,day_type;
    long int days;
  
   
    printf("Please enter the year: ");
    scanf("%d",&year);
    year=year-1; //we will find days before given year so
    differ=year-1900;


   /*as leap year is not divisible by 100.so,create 2 condition
   one difference less than 100 and greater than 100*/


   if(differ<100)
   {
   lp_year=differ/4; //caln of total no. of leap year
   days=(366*lp_year)+((differ-lp_year)*365+365+1);//see Note1
   day_type=days%7; //caln of day type sun, mon......
   }
  
   if(differ>=100)
   {
   lp_year=(differ/4)-(differ/100)+1+((year-2000)/400);//see Note2           
   days=(366*lp_year)+((differ-lp_year)*365+365+1);//see Note3
   day_type=days%7;
   }


if(day_type==0)
printf("\nSunday");
if(day_type==1)
printf("Monday");
if(day_type==2)
printf("Tuesday");
if(day_type==3)
printf("Wednesday");
if(day_type==4)
printf("Thursday");
if(day_type==5)
printf("Friday");
if(day_type==6)
printf("Saturday");
    
    
  
   
    return 0;  //int main() is function so value must be return.
               //u will read in function chapter
}



/*Note1:
-leap year has 366 day so lp_year*366
-remaining year has 365 day so (differ-lp_year)*365
-add 365 because we reduce 1 year
-add 1 to make jan 1 on which we find day type
     
Note2:
-(leap year come in every 4 year so) (differ/4) for leap year
-(leap year isn't divisible by 100 so we subtract (differ/100)
  from counting as leap year
-(leap year will be if divisible by 400 so ((year-2000)/400)
  to count that year as leap year
- we calculate from 2000 so we add 1

Note3:
-leap year has 366 day so lp_year*366
-remaining year has 365 day so (differ-lp_year)*365
-add 365 because we reduce 1 year
-add 1 to make jan 1 on which we find day type */

Wednesday, August 24, 2011

Validate if given number is Palindrome or not

#include <stdio.h>
int main(void)
{
int i,num,rev= 0,j = 0;
printf("Enter a number for Palindrome check :");
scanf("%d",&i);
num = i;

while ( i != 0 )
{
rev = i % 10;
j = rev + j * 10 ;
i = i / 10 ;
}

if ( j == num)
printf("\n %d is palindrome",num);
else
printf("\n %d is not Palindrome\n",num);
return 0;

}


Sunday, May 8, 2011

Find Prime Factorization of given number

/*Find Prime Factorization Of Any Given Number */

#include <stdio.h>
int main(void)
{
int i,num,j,k;
printf("Enter an integer whose Prime factors you want :");
scanf("%d",&num);
k = num ;
for(i = 2 ; i <= num ; i++)
{
for (j = 2 ; j <= i - 1 ; j++)
{
if ( i % j == 0)
break;
}
if (i == j)
{
while ( k % i == 0)
{
printf("%d ",i);
k = k / i ;
continue;

}


}

}
return 0;
}

Sunday, March 21, 2010

Bifurcate entered sentence into uppercase,lowercase,number,special characters & count at the end

/*End user enters sentence/s and you need to bifurcate each entered character whether its number,uppercase character,lower case character ,special character,whitespace character,tab/new line and then reprint the same sentence/s on new line*/

#include <stdio.h>
int main(void)
{
int i;
int big = 0,num = 0,sp = 0,small = 0,space = 0;
while ( (i = getchar()) != EOF )
{
if (( i >= 'a') && ( i <= 'z'))
small++;
else if (( i >= 'A') && ( i <= 'Z'))
big++;
else if (( i >= '0') && (i <= '9'))
num++;
else if ((( i >= 0) && (i <= 31)) || (( i >= 33) && ( i <= 47)) ||((i >= 58) && (i <= 64)) || (( i >= 91) && ( i <= 96)) || (( i >= 123) && ( i <= 127))) //special characters
sp++;
else if (( i == 32) || ( i == 9) || ( i = 10) || ( i = 13)) // for space,tab,newline
space++;
putchar(i);
}
printf("%d lower characters are entered \n",small);
printf("%d Uppercase characters are entered \n",big);
printf("%d White space characters are entered \n",space);
printf("%d Numerical characters are entered \n",num);
printf("%d Special characters are entered \n",sp);
return 0;
}

Monday, February 22, 2010

Freenode IRC - Connect via Openssl with SASL authentication


Migration of freenode to new server from hyperion-ircd to ircd-seven happened in January end,2010.With which one can connect to IRC freenode via OpenSSL encryption between client & server .Using a script in Irssi one can get authentication via SASL.Freenode's standard port is 6667 but listen's for SSL connections on ports 7000 and 7070.So here are steps of how to get SSL and SASL setup for Irssi:

1)Perl libraries required: Before hand to run Irssi script pre-requisite perl lib are Blowfish, DH and BIGNUM

2)For Debian/Ubuntu ( install irssi,perl lib pre-requisites & Openssl) :

sudo apt-get install irssi openssl libcrypt-openssl-bignum-perl libcrypt-dh-perl libcrypt-blowfish-perl

3)At terminal:

cd ~/.irssi/scripts
sudo mkdir autorun ## only if you do not have this directory already
sudo wget http://www.freenode.net/sasl/cap_sasl.pl ##location of cap_sasl.pl is ~/.irssi/scripts/autorun/
cd autorun
sudo ln -s ../cap_sasl.pl cap_sasl.pl

4)Start up irssi without connecting to anything :

irssi -!

5)Once in Irssi,at Status, setup your username and password for SASL:
/server add -auto -ssl -network freenode irc.freenode.net 7000
/server add -auto -ssl -ssl_cacert /etc/ssl/certs/GandiStandardSSLCA.pem -network freenode irc.freenode.net 7000(##Incase if you have any certification issues pass these command)
/sasl set freenode your_nick your_password DH-BLOWFISH
/sasl save
/save

6)Quit Irssi

7)Edit config file of irssi :

sudo vi ~/.irssi/config (## search for section servers and see if below entries matches & update as below)

address = "chat.us.freenode.net";
chatnet = "freenode";
port = "7000";
use_ssl = "yes";
ssl_verify = "yes";
ssl_capath = "/etc/ssl/certs";
autoconnect = "yes";

8)Now simply fire up irssi-windows ( for window's navigation btw channels).You may get something like these :

17:36 -!- Irssi: Looking up chat.us.freenode.net
17:36 -!- Irssi: SASL: auth loaded from /home/binnishah/.irssi/sasl.auth
17:36 -!- Irssi: Connecting to chat.us.freenode.net [140.211.166.4] port 7000
17:36 -!- Irssi: Connection to chat.us.freenode.net established
17:36 !niven.freenode.net *** Looking up your hostname...
17:36 !niven.freenode.net *** Checking Ident
17:36 -!- Irssi: CLICAP: supported by server: identify-msg multi-prefix sasl
17:36 -!- Irssi: CLICAP: requesting: multi-prefix sasl
17:36 -!- Irssi: CLICAP: now enabled: multi-prefix sasl
17:36 -!- binnishah!binnishah@unaffiliated/abms1116 abms1116 You are now logged in as abms1116.
17:36 -!- Irssi: SASL authentication successful
17:36 -!- Welcome to the freenode Internet Relay Chat Network binnishah
17:36 -!- Your host is niven.freenode.net[140.211.166.4/7000], running version ircd-seven-1.0.1
17:36 -!- This server was created Sat Jan 30 2010 at 21:09:36 UTC
17:36 -!- niven.freenode.net ircd-seven-1.0.1 DOQRSZaghilopswz CFILMPQbcefgijklmnopqrstvz bkloveqjfI
17:36 -!- CHANTYPES=# EXCEPTS INVEX CHANMODES=eIbq,k,flj,CFLMPQScgimnprstz CHANLIMIT=#:120 PREFIX=(ov)@+ MAXLIST=bqeI:100 MODES=4 NETWORK=freenode KNOCK STATUSMSG=@+ CALLERID=g are supported by this server
17:36 -!- SAFELIST ELIST=U CASEMAPPING=rfc1459 CHARSET=ascii NICKLEN=16 CHANNELLEN=50 TOPICLEN=390 ETRACE CPRIVMSG CNOTICE DEAF=D MONITOR=100 are supported by this server
17:36 -!- FNC TARGMAX=NAMES:1,LIST:1,KICK:1,WHOIS:1,PRIVMSG:4,NOTICE:4,ACCEPT:,MONITOR: EXTBAN=$,arx WHOX CLIENTVER=3.0 are supported by this server
17:36 -!- There are 899 users and 52716 invisible on 23 servers
17:36 -!- 37 IRC Operators online
17:36 -!- 58 unknown connection(s)
17:36 -!- 27761 channels formed
17:36 -!- I have 2559 clients and 1 servers
17:36 -!- 2559 4459 Current local users 2559, max 4459
17:36 -!- 53615 60092 Current global users 53615, max 60092
17:36 -!- Highest connection count: 4460 (4459 clients) (271988 connections received)
17:36 -!- - niven.freenode.net Message of the Day



*Xchat configuration to SSL :

From Menu select--> Network list -->Networks -->Select freenode --> Edit-->Servers for freenode -->Add "irc.freenode.net/7070" -->set nickserv password
On same window you may find --> Connecting -->Click on "Use SSL for all servers on these Network"

On connection via SSL with port 7070 on Xchat ,you may find it like these:

* * Certification info:
* Subject:
* OU=Domain Control Validated
* OU=Gandi Standard Wildcard SSL
* CN=*.freenode.net
* Issuer:
* C=FR
* O=GANDI SAS
* CN=Gandi Standard SSL CA
* Public key algorithm: rsaEncryption (2048 bits)
* Sign algorithm sha1WithRSAEncryption
* Valid since Jan 13 00:00:00 2010 GMT to Jan 13 23:59:59 2011 GMT
* * Cipher info:
* Version: TLSv1/SSLv3, cipher DHE-RSA-AES256-SHA (256 bits)
* Connected. Now logging in...
* *** Looking up your hostname...
* *** Checking Ident
* Welcome to the freenode Internet Relay Chat Network abms1116
* Your host is barjavel.freenode.net[78.40.125.4/7070], running version ircd-seven-1.0.0


Public Key : 12695C6D
Fingerprint : 0131 E273 B314 E8C8 2599 E32D 99AA 9362 1269 5C6D