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;

}


Friday, June 6, 2008

Create a Diamond -Using asterisk ( * ) - Pattern # 2

Create a Diamond out of asterisk whose value is 42 in Ascii code as below :-
         
               
    *
                 ***
               *****
             *******
           *********
             *******
               *****
                 ***
                  *   


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

#include <stdio.h> 

int main(void)
{
int space ,list = 1,i,f,q,push = 9,a,b,c = 4,m,n,o = 1;
/*for creating upper space*/

for ( space = 1; space <= 5; space++ ) { for ( i = 6; i >= list ; i--)
{
printf("%c",32);
}
list = list + 1;
/* for creating upper stars*/

for ( f = 1 ; f <= 5 ; f++ ) { for ( q = 9 ; q >= push ; q-- )
{
printf("%c",42);
}
break;
}
push = push - 2;
printf("\n");
}
/*End of first for loop*/


/*Second diamond half starts with spaces*/

for ( a = 1 ; a <= 5 ; a++ )
{
for ( b = 2; b <= c ; b++ )
{
printf("%c",32);
}
c = c + 1 ;

/*Second start of stars*/

for ( n = 1 ; n <= 4 ; n++ )
{
for ( m = 7 ; m >= o ; m-- )
{
printf("%c",42);

}
break;
}
o = o + 2;
printf("\n");
}
}





Asterisk pattern with spaces - pattern # 1

Create a pattern with asterisk as follows :-

************
  ***********
    **********
      *********
            ******
              *****
                ****
                  ***
                    **
                      *

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

#include <stdio.h>
int main(void)
{
int i,j,a = 1,b,c,d = 12,k,u,h = 1,x,y,z = 13,l,m,n = 6 ;

/*For spaces */

for ( b = 1 ; b <= 4 ; b++ ) { for ( c = 12 ; c >= d ; c-- )
{
printf("%c",32);
}
d = d - 1;
/* prg for stars */
for ( i = 1 ; i <= 4 ; i++) { for ( j = 12 ; j >=a ; j-- )
{
printf("%c",42);
}
break;
}
a = a + 1;
printf("\n");
}
/*Second spaces*/

for ( x = 1 ; x <= 7 ; x++ )
{
for ( y = 7 ; y <= z ; y++ )
{
printf("%c",32);
}
z = z + 1 ;

/*Second stars*/

for ( l = 1 ; l <= 6 ; l++ )
{
for ( m = 1 ; m <= n ; m++ )
{
printf("%c",42);
}
break;
}
n = n - 1 ;
printf("\n");
}
}

Print Armstrong numbers between 1- 1000

Write a program to print out all Armstrong numbers between 1 and 1000.If sum of cubes of each digit of the number is equal to number itself,then the number is said to be "Armstrong no."

For example 153 = ( 1*1*1) + (5*5*5) + (3 *3*3)

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

#include <stdio.h>

int main(void)

{

int i = 0 ;
while ( i <= 999 )
{
int a,b,c,d = 0 ;
a = (i /100);
b = (( i % 100) / 10);
c = (( i % 100) % 10);
d = ((a * a * a) + ( b * b * b) + (c * c * c));
if ( d == i)
printf("%d is Armstrong number\n ",i);
i = i++ ;
}
}

*Second way using for *
===================
#include <stdio.h>

int main(void)
{
int i ,a = 0,b = 0,c = 0 ,d = 0;
for ( i = 1 ; i <= 1000 ;i++ )
{
a = (i / 100);
b = (( i / 10 ) % 10 );
c = ((i % 100 ) % 10 );
d = (( a * a * a ) + (b * b * b) + ( c * c * c));
if ( i == d )
printf(" %d is Armstrong number\n",i );
}
}













Thursday, June 5, 2008

Intro to fn abs ( )

/*Introduction to function abs -absolute value */

# include<>
#include<>

int main(void)


{
int a,b;
printf("Enter a positive & Negative integer :");
scanf("%d %d",&a,&b);

printf("The value of a /b is %d \n ", (abs (a) / abs (b)));

printf("The value of a %% b is %d \n", (abs (a) % abs (b)));
return 0;

}

Print 100 asterisks one at time with 10th multiple

/*Write a program that prints 100 aesterisks ,one at a time.After every tenth aesterisk,your program should print a newline character
Hint:count from 1 to 100.Use the modulus operator to recognize each time the counter reaches a multiple of 10
*/

**********
**********
**********
**********
**********
**********
**********
**********
**********
**********


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


#include

int main(void)
{
int i;
for ( i = 1 ; i <= 100 ; i++)
{
printf("%c",42);
if ( i % 10 == 0 )
printf("\n");
}
}

Create an asterisk odd pattern 1-9 & 9-1

Create a asterisk pattern as belows :-

*
***
*****
*******
*********
*********
*******
*****
***
*

Sol.Code :-
=======

#include

int main(void)
{
int i,f,p,q,last = 1,push = 9 ;
for ( i = 1 ; i <= 5 ; i++ )
{
for ( f = 1 ; f <= last ;f++ )
{
printf("%c",42);
continue;
}
last = (last + 2) ;
printf("\n");
}
for ( p = 1 ; p <= 5 ; p++ )
{
for ( q = 1 ;q <= push ; q++ )
{
printf("%c",42);
continue;
}
push = (push - 2);
printf("\n");
}
}
















Aesterisk pattern of 8-1

Create a pattern of "Aesterisks(*)" as follows :-

* * * * * * * *
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*

Solutions Code :-

/*Creat a pattern of asterik(star's) from 8-1 */


#include
int main(void)
{
int i,f ,l = 8;
for ( i = 8 ; i >= 1 ; i--)
{
for(f = 1 ;f <= l ;f++)
{
printf(" %c",42);
continue;
}
l = l-- ;
printf("\n");

}

}

Average of several integers

/*Write a prog that calculates and prints the avg of several integers.
Assume the last value read with scanf is sentinel 9999.A typical input
sequence might be :

10 8 11 7 9 9999
indicating that the avg of all values preceding 9999 is to be calculated
*/

#include

int main(void)
{
int i,count = 0,temp = 0 ;
char ans ;
int avg;
for ( i = 1 ; i <= 9999 ; )
{
printf("Enter number between 1 & 9999 : ");
scanf("%d",&i);
count = (count + 1) ;
temp = (temp + i);
printf("Do you want to enter another integer for avg count (y/n) : ");
scanf(" %c",&ans);
if ( ans == 'y')
continue;
else
break;
}
avg = (temp/count);
printf("Average is %d",avg);
}