0% found this document useful (0 votes)
26 views5 pages

Examples of Cprograms

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views5 pages

Examples of Cprograms

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

1. Write a C program to compute the sum of the first 10 natural numbers.

#include <stdio.h>
int main ()
{
int j, sum = 0;
printf ("The first 10 natural number is: ");
for (j = 1; j <= 10; j++)
{
sum = sum + j;
}
printf("\n The Sum is : %d\n", sum);
}
Input:
The first 10 natural number is :
1 2 3 4 5 6 7 8 9 10
Output:
The Sum is : 55

2. Write a program in C to display n terms of natural numbers and their sum.


#include <stdio.h>
int main() {
int i, n, sum = 0;
printf("Input Value of terms : ");
scanf("%d", &n);
printf("\n The first %d natural numbers are:\n", n);
for (i = 1; i <= n; i++) {
printf("%d ", i);
sum += i;
}
printf("\n The Sum of natural numbers upto %d terms : %d \n", n, sum);
return 0;
}
Input:
Input Value of terms : 7
The first 7 natural number is :
1234567
Output:
The Sum of Natural Number upto 7 terms : 28

3. Write a C program to calculate the factorial of a given number.


#include <stdio.h>
int main()
{
int i, f = 1, num;
printf("Input the number : ");
scanf("%d", &num);
for(i = 1; i <= num; i++)
prntf("The Factorial of %d is: %d\n", num, f);
return 0;
}
Input:
Input the number : 5
Output:
The Factorial of 5 is: 120 [Note: 5! = 5 X 4 X 3 X 2 X 1=120]

4. Write a C program to display a pattern like a right angle triangle with a number.

The pattern like:


1
12
123
1234
#include <stdio.h>
int main() {
int i, j, rows;
printf("Input number of rows : ");
scanf("%d", &rows);

for (i = 1; i <= rows; i++) {


for (j = 1; j <= i; j++)
printf("%d", j);
printf("\n");
}
return 0;
}
Input:
Input number of rows : 4
Output:
1
12
123
1234
5. Write a program in C to make such a pattern like a right angle triangle with a number which will
repeat a number in a row.

The pattern like :

1
22
333
4444
#include <stdio.h>
int main() {
int i, j, rows;
printf("Input number of rows : ");
scanf("%d", &rows);
for (i = 1; i <= rows; i++) {
for (j = 1; j <= i; j++)
printf("%d", i);
printf("\n");
}
return 0;
}

Input:
Input number of rows : 10
Output:
1
22
333
4444
6. Write a C program to make such a pattern like a right angle triangle with an asterisk.
*
**
***
****
*****
#include <stdio.h>
int main()
{
int i, j, rows;
printf("Input number of rows : ");
scanf("%d", &rows);

for (i = 1; i <= rows; i++)


{
Print(“\n”):
for (j = 1; j <= i; j++)
printf(" * ");
}
return 0;
}

Input:
Input number of rows : 5
Output:
*
**
***
****
*****

7. Write a C program to display the pattern as a pyramid using asterisks, with each row containing an
odd number of asterisks.

The pattern is as below:


*
***
*****
#include <stdio.h>
int main() {
int i, j, n;
printf("Input number of rows for this pattern :");
scanf("%d", &n);
for (i = 0; i < n; i++)
{
for (j = 1; j <= n - i; j++)
printf(" ");
for (j = 1; j <= 2 * i - 1; j++)
printf("*");
printf("\n");
}
return 0;
}
Input:
Input number of rows for this pattern :3
Output:
*
***
*****

8. Write a program in C to display the n terms of a harmonic series and their sum.
1 1 1 1
1 + + + + ...+
2 3 4 𝑛

#include <stdio.h>
int main()
{
int i, n;
float s = 0.0;
printf("Input the number of terms : ");
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
s =s+ 1 / i;
}

printf("\n Sum of Series upto %d terms : %f \n", n, s);


return 0;
}
Input:
Input the number of terms: 5
Output :
Sum of Series upto 5 terms : 2.28

9. Write a program in C to display the n terms of a harmonic series and their sum.
1 1 1 1
+ + + . . .+
2 3 4 𝑛
#include <stdio.h>
#include <math.h>
int main()
{
int i, n;
float s = 0.0;
printf("Input the number of terms : ");
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
s =s+1 / pow(i,2);
}

printf("\n Sum of Series upto %d terms : %f \n", n, s);


return 0;
}
Input:
Input the number of terms: 5
Output:
Sum of Series upto 5 terms : 1.46

You might also like