if 3 then
***
* *
***
if 5 then
*****
* *
*****
* *
*****
in even rows the only 1st and last star shoud be prints.
This post has been edited by muhammadlodhi: 14 April 2010 - 12:25 PM




Posted 14 April 2010 - 12:19 PM
This post has been edited by muhammadlodhi: 14 April 2010 - 12:25 PM
Posted 14 April 2010 - 12:26 PM
sarmanu, on 14 April 2010 - 11:21 AM, said:
int main(){
int num,i,j,k,l;
printf("press any odd num");
scanf("%d",&num);
if(num%2==0){
printf("use odd number thanks");
}
for(i=1;i<=num;i++){
for(j=i;j<=i;j++)
if(j==1&&j==num){
printf("*");
}
else
printf(" ");
getch():
return 0;
}
}
This post has been edited by JackOfAllTrades: 14 April 2010 - 12:41 PM
Reason for edit:: Added code tags
Posted 14 April 2010 - 12:36 PM
#include <stdio.h>
int main()
{
int num, i;
printf("press any odd num");
scanf("%d", &num);
if (num % 2 == 0)
printf("use odd number thanks");
// We need an else here, since you want this block to be executed
// only when an odd number was entered:
else
{
// This loop tracks the number of rows:
for (i = 1; i <= num; i++)
{
// Let's analyze the pattern. Take an example, let's say num is 3.
// This needs to be printed:
// ***
// * *
// ***
// So, we see that on odd positions (rows), we need to print the amount
// of * equal with 3 (num). So, handle this part here.
if (i % 2 == 1) // If position is odd
{
// Print "*" for "num" times:
for (int i = 1; i <= num; i++)
printf("*");
printf("\n"); // Print a newline for good looking output
}
else // In case we are here, the position is even
{
// Print the first *
printf("*");
// Loop from 2 (because the first character was already printed) to
// num - 1 to print the needed amount of spaces:
for (int i = 2; i <= num - 1; i++)
printf(" ");
printf("*\n"); // Printed spaces, let's output the *
}
}
}
return 0;
}
Posted 14 April 2010 - 12:57 PM
sarmanu, on 14 April 2010 - 11:36 AM, said:
#include <stdio.h>
int main()
{
int num, i;
printf("press any odd num");
scanf("%d", &num);
if (num % 2 == 0)
printf("use odd number thanks");
// We need an else here, since you want this block to be executed
// only when an odd number was entered:
else
{
// This loop tracks the number of rows:
for (i = 1; i <= num; i++)
{
// Let's analyze the pattern. Take an example, let's say num is 3.
// This needs to be printed:
// ***
// * *
// ***
// So, we see that on odd positions (rows), we need to print the amount
// of * equal with 3 (num). So, handle this part here.
if (i % 2 == 1) // If position is odd
{
// Print "*" for "num" times:
for (int i = 1; i <= num; i++)
printf("*");
printf("\n"); // Print a newline for good looking output
}
else // In case we are here, the position is even
{
// Print the first *
printf("*");
// Loop from 2 (because the first character was already printed) to
// num - 1 to print the needed amount of spaces:
for (int i = 2; i <= num - 1; i++)
printf(" ");
printf("*\n"); // Printed spaces, let's output the *
}
}
}
return 0;
}
Posted 14 April 2010 - 01:04 PM
sarmanu, on 14 April 2010 - 12:00 PM, said:
***** * * ***** * * *****
*** * * ***
Posted 14 April 2010 - 01:20 PM
muhammadlodhi, on 14 April 2010 - 12:04 PM, said:
sarmanu, on 14 April 2010 - 12:00 PM, said:
***** * * ***** * * *****
*** * * ***
Posted 14 April 2010 - 01:32 PM
#include <stdio.h>
int main()
{
int num, i;
printf("press any odd num");
scanf("%d", &num);
if (num % 2 == 0)
printf("use odd number thanks");
else
{
for (i = 1; i <= num; i++)
{
if (i % 2 == 1) // If position is odd
{
for (int i = 1; i <= num; i++)
printf("*");
printf("\n"); // Print a newline for good looking output
}
else
{
printf("*");
for (int i = 2; i <= num - 1;
printf(" ");
printf("*\n");
}
}
}
return 0;
}
Posted 14 April 2010 - 01:37 PM
Posted 14 April 2010 - 03:32 PM
muhammadlodhi, on 14 April 2010 - 12:32 PM, said:
#include <stdio.h>
int main()
{
int num, i;
printf("press any odd num");
scanf("%d", &num);
if (num % 2 == 0)
printf("use odd number thanks");
else
{
for (i = 1; i <= num; i++)
{
if (i % 2 == 1) // If position is odd
{
for (int i = 1; i <= num; i++)
printf("*");
printf("\n"); // Print a newline for good looking output
}
else
{
printf("*");
for (int i = 2; i <= num - 1;
printf(" ");
printf("*\n");
}
}
}
return 0;
}
int main() {
int num,i, j;
num = 2;
while(num % 2 == 0) {
//printf("\nenter any odd num");
scanf("%d",&num);
getchar(); //not essential, but good.
}
//c'mon, this is C, we count "zero, one, two...", unless there's a
//good reason for starting elsewhere, start at zero.
for(i=0;i<num;i++){ //for every row of *'s
printf("\n");
if(i % 2 == 0) { //if the row is even
for(j=0;j<num;j++) //print num stars
printf("*");
}
else { //row is odd C odd ;)/>
for(j=0;j<num;j++){printf("*");
if(j ==0 || j == (num-2)) {
printf("*");
}
printf(" ");
}
}
getchar();
return 0;
} }
int main() {
int num,i, j;
num = 2;
while(num % 2 == 0) {
//printf("\nenter any odd num");
scanf("%d",&num);
getchar(); //not essential, but good.
}
//c'mon, this is C, we count "zero, one, two...", unless there's a
//good reason for starting elsewhere, start at zero.
for(i=0;i<num;i++){ //for every row of *'s
printf("\n");
if(i % 2 == 0) { //if the row is even
for(j=0;j<num;j++) //print num stars
printf("*");
}
else { //row is odd C odd ;)/>
for(j=0;j<num;j++){printf("*");
if(j ==0 || j == (num-2)) {
printf("*");
}
printf(" ");
}
}
getchar();
return 0;
} }
Posted 14 April 2010 - 03:41 PM
#include <stdio.h>
int main()
{
int num, i;
printf("press any odd num");
scanf("%d", &num);
if (num % 2 == 0)
printf("use odd number thanks");
else
{
for (i = 1; i <= num; i++)
{
if (i % 2 == 1) // If position is odd
{
for (int i = 1; i <= num; i++)
printf("*");
printf("\n"); // Print a newline for good looking output
}
else
{
printf("*");
for (int i = 2; i <= num - 1; i++)
printf(" ");
printf("*\n");
}
}
}
return 0;
}
for(j=0;j<num;j++){printf("*"); //why do you print it out here?
if(j ==0 || j == (num-2)) {
printf("*");
}
printf(" ");
}
Posted 15 April 2010 - 05:44 AM
erik.price, on 14 April 2010 - 02:41 PM, said:
#include <stdio.h>
int main()
{
int num, i;
printf("press any odd num");
scanf("%d", &num);
if (num % 2 == 0)
printf("use odd number thanks");
else
{
for (i = 1; i <= num; i++)
{
if (i % 2 == 1) // If position is odd
{
for (int i = 1; i <= num; i++)
printf("*");
printf("\n"); // Print a newline for good looking output
}
else
{
printf("*");
for (int i = 2; i <= num - 1; i++)
printf(" ");
printf("*\n");
}
}
}
return 0;
}
for(j=0;j<num;j++){printf("*"); //why do you print it out here?
if(j ==0 || j == (num-2)) {
printf("*");
}
printf(" ");
}
