Learning
C programming language is mandatory for today’s students [as one programming
language], during their academic career. The C language has many mechanisms to
deal with decision making, repetitive tasks, such as if, if…else, for, while
etc. Here are some of such C codes which may asked in theory/practical exams.
C is
a typed language, so no need of code explanation as everything will be cleared
by itself by reading the code. If you know C, you know this code. It’s that
simple!
The
Code::Blocks IDE 20.03+MinGW and Cygwin tool are used to test the code.
Code4a |
Write A Program to ask user for base
value and then display a factorial number using while loop
|
|
|
//WAP#4a:WAP to ask user
for base value & // then display a
factorial number using // while loop /* #include <stdio.h> #include <stdlib.h>
int main() { int num,counter, fact;//declare variables counter = 1; fact = 1;
printf("Enter base value [integer]:
"); scanf("%d",&num); //get
base value
while (counter <= num) { fact = fact * counter; counter = counter + 1; printf("%d\t",fact); } printf("\n Factorial of %d is =
%d\n",num,fact); return 0; } //end of program
//------------------------- //output WAP#4a:
/* Enter base value
[integer]: 6 1 2
6 24 120
720 Factorial of 6 is = 720 */ |
|
|
|
|
Code4b |
Write A Program to ask user for base
value and then display a factorial number using do…while loop
|
|
|
//WAP#4b:WAP to ask user
for base value & // then display a
factorial number using // do...while loop /* #include <stdio.h> #include <stdlib.h>
int main() { int num,counter, fact;//declare variables counter = 1; fact = 1;
printf("Enter base value [integer]:
"); scanf("%d",&num); //get
base value
do { fact = fact * counter; counter = counter + 1; printf("%d\t",fact); } while (counter <= num); printf("\n Factorial of %d is =
%d\n",num,fact); return 0; } //end of program //------------------------- //output WAP#4b:
/* Enter base value
[integer]: 6 1 2
6 24 120
720 Factorial of 6 is = 720 */ |
|
|
|
|
Code4c |
Write A Program to ask user for base
value and then display a factorial number using for loop
|
|
|
//WAP#4c:WAP to ask user
for base value & // then display a
factorial number using // for loop /* #include <stdio.h> #include <stdlib.h>
int main() { int num, fact;//declare variables fact = 1;
printf("Enter base value [integer]:
"); scanf("%d",&num); //get
base value
for(int i=1; i<=num; i++) { fact = fact * i; printf("%d\t",fact); } printf("\n Factorial of %d is =
%d\n",num,fact); return 0; } //end of program
//------------------------- //output WAP#4c:
/* Enter base value
[integer]: 6 1 2
6 24 120
720 Factorial of 6 is = 720 */ |
|
|
|
|
Code5 |
Write A Program to display first five
numbers with their cubes using do…while loop
|
|
|
//WAP#5:WAP to display
first 5 numbers //with their cubes using
do...while loop /* #include <stdio.h> #include <stdlib.h>
int main() { int i;//declare index i = 1;
do { printf("%d\t %d \n",i,
i*i*i); i = i + 1; }while (i <= 5);
return 0; } //end of program
//--------------------------- //output WAP#5:
/* 1 1 2 8 3 27 4 64 5 125 */ |
|
|
|
|
Code6 |
Write A Program to display first 10 odd
numbers using do…while loop
|
|
|
//WAP#6:WAP to display
first 10 odd numbers //using do...while loop /* #include <stdio.h> #include <stdlib.h>
int main() { int i;//declare index i = 1;
do { if((i%2) != 0) printf("%d\n",i); i = i + 1; } while (i <= 20);
return 0; } //end of program */ //output WAP#6:
/* 1 3 5 7 9 11 13 15 17 19 */ |
|
|
|
|
For such codes click CCode label or check next day post on
right-side post history bar.
---till next post, take care and bye-bye.
No comments:
Post a Comment