0% found this document useful (0 votes)
60 views

1) Program To Find Factorial of A Given Number

The document contains 7 code snippets that demonstrate various programming concepts in C including: calculating factorials, adding digits of a number, printing multiples of 3, printing a triangle pattern of asterisks, generating Pascal's triangle, calculating binomial coefficients, and printing the Fibonacci series up to a given number of terms.

Uploaded by

Neha Raju
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views

1) Program To Find Factorial of A Given Number

The document contains 7 code snippets that demonstrate various programming concepts in C including: calculating factorials, adding digits of a number, printing multiples of 3, printing a triangle pattern of asterisks, generating Pascal's triangle, calculating binomial coefficients, and printing the Fibonacci series up to a given number of terms.

Uploaded by

Neha Raju
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

1) Program to find Factorial of a given Number

#include <stdio.h>
int main()
{
int n, i;
unsigned long long factorial = 1;

printf("Enter an integer: ");


scanf("%d",&n);

// show error if the user enters a negative integer


if (n < 0)
printf("Error! Factorial of a negative number doesn't exist.");

else
{
for(i=1; i<=n; ++i)
{
factorial *= i; // factorial = factorial*i;
}
printf("Factorial of %d = %llu", n, factorial);
}

return 0;
}

------------------------------------------------------------------------------------
2) C program to add digits of a number

#include <stdio.h>

int main()

int n, t, sum = 0, remainder;

printf("Enter an integer\n");

scanf("%d", &n);

t = n;

while (t != 0)

remainder = t % 10;

sum = sum + remainder;

t = t / 10;

printf("Sum of digits of %d = %d\n", n, sum);

return 0;

--------------------------------------------------------------------------------------
3) Write a c program that prints all multiples of 3between 1 and 50

#include<stdio.h>
#include<conio.h>
void main()
{
int a;
printf("The multiples of 3 between 1 and 50 are :");
for(a=1;a<=50;a++)
{
if(a%3==0)
{
printf("\n%d",a);
}
}
getch();
}

---------------------------------------------------------------------------------------

Consider the following triangle pattern


*
**
***
****
*****

4) Write a C program to print the above pattern:

#include <stdio.h>

int main()

int n, c, k;

printf("Enter number of rows\n");

scanf("%d", &n);

for (c = 1; c <= n; c++)

for(k = 1; k <= c; k++)


printf("*");

printf("\n");

return 0;

----------------------------------------------------------------------------------------------------

5) Program to Print Pascal's triangle

1 1

1 2 1

1 3 3 1

1 4 6 4 1

1 5 10 10 5 1

#include <stdio.h>
int main()
{
int rows, coef = 1, space, i, j;
printf("Enter number of rows: ");
scanf("%d",&rows);
for(i=0; i<rows; i++)
{
for(space=1; space <= rows-i; space++)
printf(" ");
for(j=0; j <= i; j++)
{
if (j==0 || i==0)
coef = 1;
else
coef = coef*(i-j+1)/j;

printf("%4d", coef);
}
printf("\n");
}
return 0;
}
6) Program to print Binomial coefficient
#include<stdio.h>
#define MAX 10

int main()
{
int n, a, bi_nom;

printf("Mx ");

for (n=0; n<=10; ++n)


printf("%d ", n);
printf("\n----------------------------------------------------------\n");
n=0;

do
{
a = 0, bi_nom = 1;
printf("%2d", n);
while(a<=n)
{
if(n==0 || a==0)
printf("%4d", bi_nom);
else
{
bi_nom=bi_nom* (n-a+1)/a;
printf("%4d", bi_nom);
}
a=a+1;
}
printf("\n");
n=n+1;
}while(n<=MAX);

printf("----------------------------------------------------------");
}
Sample Output of above program:
Mx 0 1 2 3 4 5 6 7 8 9 10
----------------------------------------------------------
0 1
1 1 1
2 1 2 1
3 1 3 3 1
4 1 4 6 4 1
5 1 5 10 10 5 1
6 1 6 15 20 15 6 1
7 1 7 21 35 35 21 7 1
8 1 8 28 56 70 56 28 8 1
9 1 9 36 84 126 126 84 36 9 1
10 1 10 45 120 210 252 210 120 45 10 1
----------------------------------------------------------

7) Program for Fibonacci Series up to n number of terms

#include <stdio.h>
int main()
{
int i, n, t1 = 0, t2 = 1, nextTerm;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci Series: ");
for (i = 1; i <= n; ++i)
{
printf("%d, ", t1);
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
}
return 0;
}

Sample Output

Enter the number of terms: 10

Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,

You might also like