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

Monday, October 26, 2009

How to enable TATA Photon whiz on Ubuntu 9.04/Fedora 11


For all those who are using TATA Photon whiz (Hauwei -EC121 or others) these is small - "How to for Ubuntu 9.04 Jaunty Jackalope & Fedora 11" ,which by default can't Plug n play (atleast for me on Ubuntu :))even with Network Manager Applet -->Mobile broadband --> Country = India --> Provider -->Tata indicom ( Plug2Surf) -->Uname: internet,Password=internet,Ph =#777 ( As its CDMA).

# Ubuntu 9.04 Photon Whiz set up

1)By default Ubuntu 9.04 doesn't have wvdial ,so you can install it via #Synaptic Package Manager -->search for wvdial

---OR--
Insert original Ubuntu Cd

Go to System->Administration->software sources. add cd as a source and close . It will update itself, Then on terminal type,

sudo apt-get install wvdial .

2) Type lsusb command and see what it gives (for me below was result)


binnishah@google:~$ lsusb
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 005 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 004 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 003 Device 002: ID 045e:00a4 Microsoft Corp.
Bus 003 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 002 Device 005: ID 12d1:1411 Huawei Technologies Co., Ltd. ---------> ( Model #,vendor# & product# to trace for modprobe just in case your OS can't catch it plug n play)
Bus 002 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub


3)I have Huawei Mobile connect whose model # is EC121 and vendor = 12d1 & product=1411
next,

modprobe usbserial vendor=0xAAAA product=0xBBBB ,

Where AAAA is 12d1 and BBBB is 1411 in my case.

4)Open terminal and give command ( pl.give these while inserting USB photon drive in lap/Server)

sudo wvdialconf /etc/wvdial.conf ---> (These will detect the modem and install it for internet connection by inserting appropriate modem configuration into /etc/wvdial.conf)

** Pl.dont forget sudo or else you will be trapped into permission issues of /usr/sbin/pppd & chap-secrets & pap-secrets permissions and worse would be wvdial.conf wont get Baud speed rate self detected ,neither exact location of modem would be traced

5) Open wvdial.conf in /etc with :-

sudo vi /etc/wvdial.conf ,it has some entries by default add some new & match it as belows:

[Dialer Defaults]
Init1 = ATZ
Init2 = ATQ0 V1 E1 S0=0 &C1 &D2 +FCLASS=0
Init3 = AT+CRM=1
Stupid Mode = 1
New PPPD = Yes
Modem Type = USB Modem
Phone = #777
ISDN = 0
Password = internet ---> Your ph number of tata/password
Username = internet --> Your uname /#
Modem = /dev/ttyUSB0
Baud = 9600 -->Baud rate would be decided by 4rth command and written automatically into wvdial.conf script

Now save & exit vi,

6)type sudo wvdial

It may give you something like these:--

binnishah@google:~$ sudo wvdial
--> WvDial: Internet dialer version 1.60
--> Cannot get information for serial port.
--> Initializing modem.
--> Sending: ATZ
ATZ
OK
--> Sending: ATQ0 V1 E1 S0=0 &C1 &D2 +FCLASS=0
ATQ0 V1 E1 S0=0 &C1 &D2 +FCLASS=0
OK
--> Modem initialized.
--> Sending: ATDT#777
--> Waiting for carrier.
ATDT#777
CONNECT 153600
--> Carrier detected. Starting PPP immediately.
--> Starting pppd at Sun Oct 18 17:47:48 2009
--> Pid of pppd: 13130
--> Using interface ppp0
--> local IP address 121.245.189.171
--> remote IP address 172.23.119.14
--> primary DNS address 202.54.29.5
--> secondary DNS address 202.54.10.2
^CCaught signal 2: Attempting to exit gracefully...
--> Terminating on signal 15
--> Connect time 5.1 minutes.
--> Disconnecting at Sun Oct 18 17:52:50 2009

7)Command tail -f /var/log/messages --> Just immediately aft.connection gives following:

binnishah@google:~$ tail -f /var/log/messages
Oct 18 17:47:48 google pppd[13130]: Warning - ***
Oct 18 17:47:49 google pppd[13130]: CHAP authentication succeeded
Oct 18 17:47:49 google pppd[13130]: CHAP authentication succeeded
Oct 18 17:47:49 google pppd[13130]: local IP address 121.245.189.171
Oct 18 17:47:49 google pppd[13130]: remote IP address 172.23.119.14
Oct 18 17:47:49 google pppd[13130]: primary DNS address 202.54.29.5
Oct 18 17:47:49 google pppd[13130]: secondary DNS address 202.54.10.2
Oct 18 17:52:50 google pppd[13130]: Terminating on signal 15
Oct 18 17:52:50 google pppd[13130]: Connect time 5.1 minutes.
Oct 18 17:52:50 google pppd[13130]: Sent 158772 bytes, received 741237 bytes.

8)If you dont want to fall in hassles of pwd ( I wouldn't recommend to do so ,though)

sudo vi /etc/sudoers

# User privilege specification
username machine_name = NOPASSWD: /usr/bin/wvdial

So next time when you give wvdial you dont need to type in password


*For Fedora-11 - TATA Photon Whiz :- ==========================

It came to me as surprise that Fed-11 took it in plug n play ie:- just some tweak with Network manager applet and I was sorted out with live n kicking
tata photon whiz USB Mobile broadband

1)Open Network manager applet
2)In that,there's option of --> Mobile Broadband -->Add ( By default you will have Huawei Technologies or your preferable qualcom or whatever) -->Select it -->Forward -->Country - India -->Forward --> Select Tata Indicom(Plug2Surf) if you have photon whiz -->Forward ->Commit changes by pressing apply
3)In Popup of Editing tata Indicom
Add your username & password
and simply press apply
4)Left click Network manager applet and you will have option of Tata Plug2surf ,select it and you are all set

Hope that helps !!!

*Image courtesy

Thursday, October 8, 2009

Determine Grade of steel

/*A certain grade of steel is graded according to the following conditions:
*1)Hardness must be greater than 50
*2)Carbon content must be less than 0.7
*3)Tensile strength must be greater than 5600
*
*The grades are as follows:
*
*Grade is 10 if all three conditions are met
*Grade is 9 if condition 1 & 2 are met
*Grade is 8 if condition 2 & 3 are met
*Grade is 7 if condition 1 & 3 are met
*Grade is 6 if only one condition is met
*Grade is 5 if none of the conditions are met
*
*Write a program which will require the user to give values of hardness,carbon content and tensile

*strength of the steel under consideration and output the grade of the steel*/

# Solution code :

==========

#include <stdio.h>
int main(void)
{
int hard,ts; // hard = hardness ; ts = tensile strength
float cc; //cc -- Carbon content

printf("\nEnter hardness of steel: ");
scanf("%d",&hard);
printf("\nEnter Carbon content of steel : ");
scanf("%f",&cc);
printf("\nEnter Tensile strenght of steel : ");
scanf("%d",&ts);

if ( (hard > 50) && (cc < 0.7) && (ts > 5600))
printf("Steel is of 10th Grade");
else if ( (hard > 50) && (cc < 0.7))
printf("Steel is of 9th Grade");
else if ( (cc < 0.7) && (ts > 5600 ))
printf("Steel is of 8th Grade");
else if (( hard > 50) && (ts > 5600 ))
printf("Steel is of 7th Grade");
else if ( (hard > 50) || (cc < 0.7) || (ts > 5600))
printf("Steel is of 6th Grade");
else
printf("Steel is of 5th Grade");
return 0;
}

Determine if eligible for premium & Policy

/*An Insurance company follows rules to calculate premium

1)If a person's health is excellent and the person is btw. 25 & 35 years of age and lives in city and is a male then the premium is Rs 4 per thousand and his policy amount cannot exceed Rs 2 lakhs.

2)If a person satisfies all the above conditions except that the sex is female then the premium is Rs 3 per thousand and her policy amount cannot exceed Rs 1 lakh.

3)If a person's health is poor and the person is btw. 25-35 and lives in village and is male ,the premium is Rs 6 per thousand and his policy cannot exceed Rs 10,000

4)In all other cases the person is not insured

Write a program to output whether the person should be insured or not ,his/her premium rate and maximum amount for which he/she can be insured.*/

#Solution
=======

#include <stdio.h>
int main(void)
{
int age,pre,policy_amt;
char sex,live,health;
printf("Enter your age : ");
scanf("%d",&age);
if ( age <= 35 && age >= 25)
{
printf("\nPl.enter your health status ( h/p- h(healthy),p(poor health): ");
scanf(" %c",&health);
printf("\nDo you live in city/Village(c=city)(v=village): ");
scanf(" %c",&live);
printf("\nAre you Male/Female(m=male,f=female : ");
scanf(" %c",&sex);

if ( health == 'h')
{
if ( sex == 'm'&& live == 'c')
printf("\nYou are opt for Rs 4/- Preminum over 1000 Rs & Policy not greater than 2 lakh");
else if ( sex == 'f'&& live == 'c')
printf("\nYou are opt for Rs 3/- Preminum over 1000 Rs & Policy not greater than 1 lakh");
}
else if ( live == 'v' && sex == 'm')
printf("\nYou are opt for Rs 6/- Preminum over 1000 Rs & Policy not greater than 10,000 Rs/-");

if ( sex == 'm' && live == 'c' && health == 'p')
printf("\nSorry no premium or Policy for you");
if ( sex == 'm' && live == 'v' && health == 'h')
printf("\nSorry no premium or Policy for you");
if ( sex == 'f' && live == 'v' && ((health == 'h') || (health == 'p' )))
printf("\nSorry no premium or Policy for you");
if ( sex == 'f' && live == 'c' && health == 'p')
printf("\nSorry no premium or Policy for you");
}

else
printf("\nYou are not eligible for Premium or Policy");

return 0;
}