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);
}