Sunday, February 27, 2011
0
C Program Language (Ascending or Descending)
The problem is to create a program will ask the user a number and counts that the user wished to have and on what order will it count. And in this problem I used for repetition statement in getting the correct program. And here is the output of the program for ascending.
The conditional codes for this output is here.
if(c=='a'){
printf("The numbers in ascending order %d ",n);
for(cnt=1;cnt<=x;cnt++){
printf("%d ",n+cnt);
}
And another output below will be shown for the descending order. See the difference from the first shown below. The same initial value and counts but different output.
Observe the condition.
if(c=='d'){ printf("The numbers in descending order "); for(cnt=x;cnt>=1;cnt--){ printf("%d ",n-cnt); } printf("%d ",n); }
The codes will be shown below is a complete one. At this time it's on your time on how interpret and understand the codes.
#include<stdio.h> #include<conio.h> main() { int n,x,cnt,n2; char c; printf("Enter initial value: "); scanf("%d",&n); printf("How many counts? "); scanf("%d",&x); printf("Order (a - ascending or d - descending): "); scanf("%s",&c); if(c=='a'){ printf("The numbers in ascending order %d ",n); for(cnt=1;cnt<=x;cnt++){ printf("%d ",n+cnt); } } if(c=='d'){ printf("The numbers in descending order "); for(cnt=x;cnt>=1;cnt--){ printf("%d ",n-cnt); } printf("%d ",n); } getch(); }
Subscribe to:
Post Comments (Atom)
0 Responses to “C Program Language (Ascending or Descending)”
Post a Comment