Saturday, June 14, 2008

Calculate Upper case,Lower case,alphabets & whitespaces

/*Calculate Upper case,Lower case,alphabets & whitespaces in entered text/sentence/string*/

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

#include <stdio.h>

int main(void)

{
int i,small,upper,white,digit ;
small = 0;
upper = 0;
digit = 0;
white = 0;

printf("Enter a sentence : ");
while ( (i = getchar()) != EOF )
{

if ( (i >= 'a') && ( i <= 'z'))
{
small = small++;
putchar(i);
}

else if (( i >= 'A') && ( i <= 'Z') )
{
upper = upper++ ;
putchar(i);

}

else if (( i >= '0') && ( i <= '9' ))
{
digit = digit++;
putchar(i);
}

else if (( i = ' ') || ( i = '\t') || ( i = '\n'))
{
white = white++;
putchar(i);
}

}
printf("\n%d are number of Small Alphabets\n",small);
printf("%d are number of Upper Alphabets\n",upper);
printf("%d are number of Digits\n",digit);
printf("%d are number of White Spaces\n",white);
}

Friday, June 13, 2008

Print all ASCII values (0-255) characters

/*Write a prog to print all the ascii values and their equivalent char using while loop.Ascii values vary from 0 to 255 */

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


#include <stdio.h>

int main(void)
{
int i = 0 ;
while ( i <= 255 )
{
printf ( " %d represents character %c \n",i,i);
i = i++ ;
}

}

Find all prime numbers between 1-300

/*Write a program to print all prime numbers from 1-300 - Hint use break,continue & nested loops*/

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

#include  <stdio.h>
int main(void)
{
int num,div ;
for (num = 2 ; num <= 300 ; num++ )
{
for ( div = 2 ; div <= num ;div++ )
{
if ( num % div == 0 )
break;
}
if ( div == num )
printf("\n%d",div);
}
}

Find out if number is prime or not

/*Write a Prog. to know if a number is prime or not */

/*Henceforth I am using GCC compiler of Linux ( Fedora Core 9 & RHEL-5 for compilation prev. I used Visual Studio Express 2008 for compilation */

#include < stdio.h >
int main(void)
{
int i,m = 1 ;
printf("Enter a number to know if its prime or composite : ");
scanf("%d",&i);
while ( m <= i)
{
if ((i % m) == 0)
printf("%d is prime factor of %d\n",m,i);
m = m++;
}
return 0;
}

Find smallest of several integers

/*Write a prog. that finds the smallest of several integers.Assume that the first value read specifies the number of values remaining*/

#include <stdio.h>

int main(void)
{
int i ,m ,temp,n1,n2 = 0,n3 = 0 ;
printf("Enter how many numbers you want to enter : ");
scanf("%d",&i);
for ( m = 1 ; m <= i ; m++ )
{
printf("Enter your number :");
scanf("%d",&n1);
n3 = n1 ;
if ( temp < n3)
n2 = temp;
else
n2 = n3;
temp = n2 ;
}
printf("Smallest number of all %d series is %d ",i,n2);
}

Sunday, June 8, 2008

From given cents display dollars,quarters,dime,nickel & pennies

/*Accept cents from end user and display how many dollars,quarters,dime,nickel and pennies
it will make
1 dollar =100 cents,1 quarter = 25 cents,1 dime = 10 cents,1 nickel = 5 cents,1 penny = 1 cent

*/


Soln Code :-
=======

#include <stdio.h>

int main(void)
{
int amt,nick,dime,quarter,dollar,pennies;
printf("Enter cents & I'll tell you how many quarters,dime,nickel,pennies it makes :");
scanf("%d",&amt);
pennies = ((((amt %100)%25)%10)%5);
nick = ((((amt %100)%25)%10)/5) ;
dime = (((amt %100)%25)/10) ;
quarter = ((amt %100)/25) ;
dollar = (amt/100);
printf("It would be %d dollar ,%d quarter ,%d dime,%d nick & %d Pennies",dollar,quarter,dime,nick,pennies);
printf("\n");
return 0;
}

Second Method:-
=========
#include <stdio.h>
int main(void)
{
int amt;
printf("Enter the amount for which change must be created : ");
scanf("%d",&amt);
printf("\n %d Dollar(s)",amt/100);
printf(" %d Quarter(s)",(amt % 100)/25);
printf(" %d dime(s)",((amt % 100) % 25)/10);
printf(" %d nickel(s)",((((amt % 100) % 25) % 10)/5));
printf(" %d pennie(s)",((((amt % 100) % 25) % 10) % 5));
return 0;
}



From given seconds calculate hours,sec,days,min

/*Calculate hours,seconds,days,minutes out of given seconds from end user */

Soln.Code :-
=========

#include <stdio.h>

int main(void)

{
int sec,days,hours,min,seco;
printf("Enter seconds & I'll tell you hours,min's & seconds of it :");
scanf("%d",&sec);
days = sec / 86400 ;
hours = ((sec % 86400)/ 3600 ) ;
min = (((sec % 86400)/60 )- (hours * 60));
seco = (sec - ((hours * 60 * 60) + (min * 60 )));
printf("Days are %d , Hours are %d,Minutes are %d & seconds are %d",days,hours,min,seco);
printf("\n");
return 0;
}


2nd Method :-
=========

#include <stdio.h>
int main(void)
{
int sec;
printf("Enter Seconds : ");
scanf("%d",&sec);
printf("\n%d Seconds is equal to %d Day/s",sec,sec / 86400) ;
printf(" %d Hours ",(sec % 86400)/3600);
printf("%d Minutes ",(sec % 86400)% 3600/60);
printf("%d Seconds \n ",((sec % 86400)% 3600 % 60) );
return 0;
}


Add integers till EOF

/*Write a program to keep on accepting numbers(integers) & add them till EOF(End of file == -1 or ctrl + z ) isnt pressed.On press it returns sum of all integers entered */


Soln.Code :-
========

#include <stdio.h>

int main(void)
{
int x = 0,y = 0;
printf("Enter your integer now\n");
printf("Enter one integer per line & then press Enter\n");
printf(">>>");
while (((scanf("%d",&y))!= EOF))
{
x = x + y;
printf(">>>");
}
printf("\n The sum of X is %d\n",x);
return 0;

}