01. Write a program to generate and print a table of the first 10 factorials.
Answer
#include <stdio.h>
int main()
{
int y,fact;
fact=1;
printf("The first 10 factorials\n");
for (y=0;y<10;y++){
fact=fact*(y+1);
printf("%i\n",fact);}
return 0;
}
2. The factorial of an integer n, written n!, is the product of the consecutive integers 1 through n. For example,
5 factorial is calculated as
5! = 5 x 4 x 3 x 2 x 1 = 120
Answer
#include <stdio.h>
int main()
{
int n,fact;
fact=1;
printf("Enter the number:");
scanf("%i",&n);
printf("\n");
for (n;n!=0;n=n-1)
fact=fact*n;
printf("%i",fact);
return 0;
}
3. Print the following shapes using loop construct of C programming
*
* *
* * *
* * * *
* * * * *
* * * * * *
Answer
#include <stdio.h>
int main()
{
int x,y;
for (x=1;x<=6;x++)
{
for (y=1;y<=x;y++){
printf("*");
}
printf("\n");
}
return 0;
}
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Answer
#include <stdio.h>
int main()
{
int x,y;
for (x=1;x<=5;x++)
{
for (y=1;y<=x;y++){
printf("%i",y);
}
printf("\n");
}
return 0;
}
* * * * * * *
* * * * * * *
* * * * * * *
* * * * * * *
* * * * * * *
Answer
#include <stdio.h>
int main()
{
int x,y;
for (x=1;x<=5;x++)
{
for (y=1;y<=7;y++){
printf("*");
}
printf("\n");
}
return 0;
}
* * * * * *
* * * * *
* * * *
* * *
* *
*
Answer
#include <stdio.h>
int main()
{
int x,y;
for (x=6;x>=1;x--)
{
for (y=1;y<=x;y++){
printf("*");
}
printf("\n");
}
return 0;
}
* * * * * *
* * * * *
* * * *
* * *
* *
*
Answer
#include <stdio.h>
int main() {
int rows, i, j;
rows=6;
for (i = 1; i <= rows; i++) {
for (j = 1; j < i; j++) {
printf(" ");
}
for (j = i; j <= rows; j++) {
printf("* ");
}
printf("\n");
}
return 0;
}
4. A triangular number can also be generated by the formula
triangularNumber = n (n + 1) / 2
for any integer value of n. For example, the 10th triangular number, 55, can be generated by substituting 10 as the value for n in the preceding formula. Write a program that generates a table of triangular numbers using the preceding formula.
Answer
#include <stdio.h>
int main()
{
int triangular_number,n,x;
triangular_number;
printf("Enter the number:");
scanf("%i",&n);
printf("\n");
printf("The first %i triangular numbers",n);
printf("\n");
for(x=1;x!=(n+1);x++){
triangular_number=x*(x+1)/2;
printf(" %3i\n",triangular_number);
}
return 0;
7. Find the minimum and maximum of sequence of 10 numbers.
Answer
#include <stdio.h>
int main() {
int numbers[10];
int minimum, maximum;
int i;
printf("Enter 10 numbers:\n");
for (i = 0; i < 10; i++) {
scanf("%d", &numbers[i]);
}
minimum = maximum = numbers[0];
for (i = 1; i < 10; i++) {
if (numbers[i] < minimum) {
minimum = numbers[i];
}
if (numbers[i] > maximum) {
maximum = numbers[i];
}
}
printf("Minimum number : %d\nMaximum number : %d\n", minimum, maximum);
return 0;
}
8. Write a program to generate and display a table of n and n *n , for integer values of n ranging from 1 to 10. Be certain to print appropriate column headings.
Answer
#include <stdio.h>
int main() {
int i;
printf("n\t\tn*n\n");
for(i=1;i<=10;i++)
printf("%d\t\t%d\n",i,i*i);
printf("\n");
return 0;
}
9. Display the n terms of harmonic series and their sum.
1 + 1/2 + 1/3 + 1/4 + 1/5 ... 1/n terms
Test Data : Input the number of terms : 5
Expected Output : 1/1 + 1/2 + 1/3 + 1/4 + 1/5 +
Sum of Series upto 5 terms : 2.283334
Answer
#include <stdio.h>
int main() {
int i,n;
float sum = 0;
printf("Input the number of terms: ");
scanf("%d", &n);
printf("1/1");
for (i = 2; i <= n; i++) {
printf(" + 1/%d", i);
sum += 1.0 / i;
}
printf("\n");
printf("Sum of Series upto %d terms: %f\n", n, sum + 1);
return 0;
}
0 Comments