Need Help in Programming .... Its Important !

Status
Not open for further replies.

[rApToR]

Computer Maniac !
Plz help me by giving me the following program code ...
(Plz use only 'C' Programming Language .... Conditional Statements and Iterative Statements)
Program #1 : Write a program to check if the given no. is an Armstrong no. ?
Program #2 : Write a program to print ::
Code:
   *
  * *
 * * *
* * * * 
 * * *
  * *
   *
 
Last edited:

nasweef

Broken In
Try this piece of code. (I havnt run it)

scanf("%d",&rows);
for(i=1;i<=rows;i++)
{
for(j=rows;j>=1;j--)
printf(" ");
for(k=1;k<=i;k++)
{
printf("* ");
}
printf("\n");
}
for(i=rows-1;i>=1;i--)
{
for(j=1;j<=rows;j++)
printf(" ");
for(k=1;k<=i;k++)
{
printf("* ");
}
printf("\n");
}
-----------------------------------------
Posted again:
-----------------------------------------
scanf("%d",&num);
int temp,digits,places = 0,arms=0,a;
temp= num;
do
{
digits = temp%10;
places ++;
temp = (temp-digits)/10;
}while(temp)

temp= num;
for (i=1;i<=places;i++)
{
digits = temp%10;
temp = (temp-digits)/10;
a = 1;
for (j=1;j<=places;j++)
a = a* digits;
arms = arms + a;
}

if (num == arms)
printf("%d is an Armstrong Number");
 
Last edited:
OP
[rApToR]

[rApToR]

Computer Maniac !
nasweef nice attempt ...
but there are a few errors such as ...
In program :: Armstrong No. no result is displayed ...
And in Star program display is like this (e.g. for rows=4)
Code:
*
**
***
****
***
**
*
 

Liverpool_fan

Sami Hyypiä, LFC legend
@rApToR:
Mate, it is not quite a smart idea to "outsource" an entire program for others to do. It is advisable that you also post the effort and in what lines you are thinking. After all it is YOU who has to learn and not us.
Anyway you would be already thinking, I would not help you but I will but only one of the problem i.e. the star one.

Now to get the ouput, first of all you want this, right?

Code:
    *
   * *
  * * *
 * * * *
* * * * *
 * * * *
  * * *
   * *
    *
Cool. Now you have to think how to achive the pattern. Now you already realise you need a loop(of course we can printf these [:D] but that would not be the point).
Now decide the position of which the loop should move.

So Draw it again and mark positions where the asterisks have to be printed.

Code:
		    *                      4  
		   * * 		  3,5		 
		  * * *		  2,4,6		 
		 * * * *                 1,3,5,7      
		* * * * *                0,2,4,6,8
		 * * * *	               1,3,5,7	 
		  * * *		  2,4,6	 
		   * *		  3,5	 
		    *		  4		 
		012345678
See, now you need loops which go from 4 to 0 and 0 to 4. So the output loop would go like that. I ran the output loop from 4 to -4 and set the negative numbers as positive ones by my abs() function.
Inside it the outer loop run first an inner loop which will print the blank spaces as well as run another parallel inner loop which will print the alternate spaces and asterisks.

Now here in my code. I have compiled it in gcc -pedantic -ansi, so I think it should work in any C compiler.
Read the code carefully. It will work pattern of any size. For getting the pattern you want as exact enter 3 as input.

Code:
#include<stdio.h>

int abs(int n)
{
	if(n>=0)
		return n;
	else
		return -n;
}

int main()
{
	int n;
	int i,j,k;
	
	printf("Enter a number: ");
	scanf("%d", &n);
	
	
	for(i=n; i>=-n; i--)
	{
		for(j=0; j<abs(i); j++)
			printf(" ");
		for(k=0; k<2*(n-abs(i))+1; k++)
			{
			if(k%2==0)
				printf("*");
			else
				printf(" ");
			}
		
		printf("\n");
		
	}
	
	return 0;
	
}

Now I hope you understand the code.

Regarding the second problem, I WONT solve it for you. Try to attempt it first and if you can't then post again. :)
 

n2casey

Super Hero - Super Powers
Yes, Liverpool_fan has given a good suggestion. You shud concentrate a little first before asking for help, so that u can learn. If u feel difficulty at any point then ask.
 
OP
[rApToR]

[rApToR]

Computer Maniac !
:p Oops I forgot to give the program i made !
Sorry guys ...
Code:
/*Program to check if no. is Armstrong no. or not*/
#include<stdio.h>
#include<conio.h>
void main()
{
int rem, num, arm, sum=0;
clrscr();
arm=num;
printf("Enter the number to be checked: ");
scanf("%d",&num);
while(num>0)
{
rem=num%10;
sum=sum+(rem*rem*rem);
num=num/10;
}
if(sum==arm)
{
printf("Number is Armstrong Number");
}
else
{
printf("This number is not an Armstrong Number");
}
getch();
}

... This is the program i made two weeks ago but i din't get the right results ... :!:
Plz check for corrections that can make this program give right results.
 

Liverpool_fan

Sami Hyypiä, LFC legend
^ ^ ^
Excellent. :)

You have only made a tiny mistake mate.

In the line
Code:
arm=num;

should come after:
Code:
printf("Enter the number to be checked: ");
scanf("%d",&num);

Not before.
Your program otherwise runs fine for all 3 digit numbers. :)
Keep in mind this program would work ONLY for 3 digit numbers.

BTW an advice:
Kindly indent your code, it will go a long way in improving presentation.
  • Also use int main and then return 0 instead of void main(). Here is why:
    *www.gidnetwork.com/b-66.html
 

Pragadheesh

In the zone
@raptor
you should try your best in doing homework problems plus get the logic or the way to implement and you do the implementation part rather than getting code from someone. these are the basic c programs. once you start solving them by yourself your interest towards the language would increase and improve your coding skills..
 

rohan_mhtr

Most wanted
main()
{
int amm 0 temp 0 n x;
printf( enter thealue of n );
scanf( d &n);
x n;
for(;n>0;)
{
temp n 10;
amm amm+(temp*temp*temp);
n n/10;
}
if(x n)
{
printf( ARMSTRONG NUMBER );
}
else
printf( NOT AMSTRONG NUMBER );

}
 
Status
Not open for further replies.
Top Bottom