Tuesday, March 31, 2009

Calculate sum of digits of five digit +ve number

/*Calculate sum of digits of 5 digit +ve number input through keyboard.The division operator & modulus can be used */

#include <stdio.h>
int main(void)
{
int num,temp = 0 ,a;
printf("Enter a +ve 5 digit number : ");
scanf("%d",&num);
if (( num <= 99999) && ( num > 0 ))
{
printf("The number you entered is %d ",num);
a = (num / 10000);
temp = a;
a = (num % 10000)/1000;
temp = temp + a;
a = ((num % 10000) % 1000) / 100 ;
temp = temp + a;
a = (((num % 10000) % 1000) % 100) / 10 ;
temp = temp + a;
a = (((num % 10000) % 1000) % 100) % 10;
temp = temp + a;
printf("\nSum of all five digits is %d",temp);
}
else
printf("\nOops not valid figure pl.try again");

return 0;
}

Monday, March 30, 2009

Write an Insurance program with & without && ||

/*A company insures its drivers in the following cases :-

- if the driver is married
- if the driver is unmarried,male & above 30 years of age
-if the driver is unmarried and female & above 25 years of age

In all other cases the driver isnt insured.If marital status ,sex & age of the driver is input ,write a prg to determine whether the driver is to be insured or not */


#include <stdio.h>
int main(void)
{
int age;
char sex,ms; //ms = marital status
printf("Are you married (Y=yes) & (n=no) :");
scanf("%c",&ms);

if ( ms == 'Y')
printf("You are qualified as you are married");
else
{
printf("Enter your sex(M/F)");
scanf(" %c",&sex);
if ( sex == 'M')
{
printf("Enter your age : ");
scanf("%d",&age);
if ( age > 30)
printf("You are qualified for Insurance");
else
printf("Sorry Mr.you are not qualified");
}
else
{
printf("Enter your age : ");
scanf("%d",&age);
if ( age > 25 )
printf("You are qualified for Insurance lady !!!");
else
printf("Sorry gal you are not qualified for Insurance");

}

}
return 0;
}


*Second Method using && ||
============================
#include <stdio.h>
int main(void)
{
int age;
char ms,sex; // ms is marital status
printf("Enter your Age,Sex(M/F),Marital Status (Y/N):");
scanf("%d %c %c",&age,&sex,&ms);

if (( ms == 'Y') || ( ms == 'N') && ( sex == 'M') && ( age > 30) || ( ms == 'N') && ( sex == 'F') && ( age > 25))
printf("You are eligible for Insurance");
else
printf("Sorry no Insurance for you");
return 0;
}




Calculate sum of first and fourth(last) digit of four digit number

/*If a four digit number is input through the keyboard,write a program to obtain the sum of first & fourth(last) digit of the number*/

#include <stdio.h>
int main(void)
{
int num,temp = 0,a ;
printf("Enter a Four digit +ve number : ");
scanf("%d",&num);
if (( num <= 9999) && ( num > 0 ))
{
printf("\nYou entered %d Number ",num);
a = num / 1000;
printf("\n First digit is %d",a);
temp = a ;
a = num % 10;
printf("\n Fourth digit is %d",a );
temp = temp + a;
printf("\nSum of 1st & fourth digit is %d",temp);
}
else
printf("Pl. enter valid 4 digit +ve number");

return 0;
}

Write a program to reverse 5 digit number

/*If a five digit number is input through keyboard,write a program to reverse the number */

Solution Code:-
=========

#include <stdio.h>
int main(void)
{
int num;
printf("Enter a 5 digit +ve number : ");
scanf("%d",&num);
if (( num < 99999 ) && (num > 0 ))
{
printf("The number you entered is %d ",num);
printf("\nAlternate number is %d",num%10);
printf(" %d",((num/10) % 10));
printf(" %d",(((num/10) / 10) % 10));
printf(" %d",((((num/10) / 10) / 10) % 10));
printf(" %d",( num / 10000));
}
else
printf("Pl.enter valid +ve 5 digit number...");

return 0;
}

How to add/increase speed dials in Opera in Linux

Currently I am using Fedora-10 with primitive browser as FF(Firefox) while secondary browser as Opera or Konqueror.Opera is pretty quick & flexible but not as secure as FF or Konqueror can be at times.

Nonetheless Open terminal & at command prompt type :-

1) vi /root/.opera/speeddial.ini
2)Add following lines :- (preferably at bottom)

[size]
Rows=3
Columns=5
3)Press Esc & save it (:w) & quit (q)
4)Restart Opera you will get new 15 speed-dials

Depending on your needs you can manipulate rows & columns with some R&D

Sunday, March 29, 2009

Write a program to add two fractions


/*Write a prg that adds two fractions.Your ouput need not be in terms*/

#include
int main(void)
{
int n1,n2,b1,b2,res1,res2;
printf("First Fraction \n");
printf("Enter the numerator : ");
scanf("%d",&n1);
printf("\nEnter the denominator : ");
scanf("%d",&n2);
printf("Second Fraction \n");
printf("Enter the numerator : ");
scanf("%d",&b1);
printf("\nEnter the denominator : ");
scanf("%d",&b2);
res1 = ((n1*b2) + (b1*n2));
res2 = (n2*b2);
printf("\nResult of two fraction %d/%d + %d/%d = %d/%d",n1,n2,b1,b2,res1,res2);
return 0;
}

Calculate temperature in Celcius from Fahrenheit

/*Calculate temperature in Celsius with input of temp in fahrenheit ,while call function to calculate temp in Cel from fahr' and return value to calling function and display value returned by your functions*/

#include
float temp(float fah); //function temp calculates input fahrenheit to Celsius fah is formal parameter
int main(void)
{
float far; // here far is actual parameter whose value is stored in fah formal parameter ,so far => fah
printf("Enter temperature in Fahrenheit : ");
scanf("%f",&far);
printf("\nTemperature in Celcius is %f",temp(far));
return 0;
}

float temp(float fah)
{
return ( 5 * ( fah - 32))/9;
}

Calculate prices of oranges


/*Problem:- A grocery store is selling oranges with price of
1 orange = 9 cent while if its 1 dozen then one dollar.Now write a function
that prompts end user to enter number of oranges ,accepts input of an integer
value and outputs total price of oranges in dollar & cents both,you can have just one parameter in
function/s you declare */

#include
#define PRICE 9 //actual parameter

int doz(int no); //no is formal parameter
int cents(int no); //same no is formal parameter
int main(void)
{
int n ; //n = no here n is actual parameters which is # of oranges
printf("Enter number of oranges to buy :- ");
scanf("%d",&n);
printf("\nPrice of %d oranges is %d Dollar",n,doz(n));
printf(" and %d cents\n",cents(n));
return 0;
}

int doz(int no)
{

return (( no / 12) * 1);
}

int cents(int no)
{
return ((no % 12) * PRICE);
}


Note:- Problem is from book "C by discovery" - Foster & Foster