0% found this document useful (0 votes)
11 views19 pages

CPL Assignment Task 4

Uploaded by

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

CPL Assignment Task 4

Uploaded by

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

CPL ASSIGNMENT TASK 4

1. Print the factorial of a given number using a while loop.

#include<stdio.h>

#include<conio.h>

void main()

int n,i=1,f=1;

clrscr();

printf("\n Enter The Number:");

scanf("%d",&n);

//LOOP TO CALCULATE FACTORIAL OF A NUMBER

while(i<=n)

f=f*i;

i++;

printf("\n The Factorial of %d is %d",n,f);

getch();

OUTPUT:-Enter the number:5

Factorial of 5 is:120
2. Find the product of all numbers between 1 and 10 using a for loop.

#include <iostream>

using namespace std;

int main() {

int product = 1;

for (int i = 1; i <= 10; i++) {

product *= i;

cout << product;

return 0;

OUTPUT:- 3628800

3. Print the first 10 Fibonacci numbers using a while loop.

#include <iostream>

using namespace std;

int main() {

int a = 0, b = 1, count = 0;

while (count < 10) {

cout << a << " ";

int next = a + b;

a = b;

b = next;

count++;

return 0;
}

OUTPUT:- 0, 1, 1, 2, 3, 5, 8, 13, 21, 34

4. Print the reverse of a given number using a for loop.

#include <iostream>

using namespace std;

int main() {

int num, reverse = 0;

cout << "Enter a number: ";

cin >> num;

for (; num != 0; num /= 10) {

reverse = reverse * 10 + num % 10;

cout << "Reversed number: " << reverse;

return 0;

OUTPUT:-Enter a number:12345

Reversed number:54321

5. Print the first 10 odd numbers using a for loop.

#include <iostream>

using namespace std;

int main() {

for (int i = 1, count = 0; count < 10; i += 2) {

cout << i << " ";


count++;

return 0;

OUTPUT:-1,3,5,7,9

6. Calculate the sum of numbers from 1 to 50 using a for loop.

#include <stdio.h>

int main() {

int sum = 0;

for (int i = 1; i <= 50; i++) {

sum += i;

printf("Sum: %d\n", sum);

return 0;

OUTPUT:- Sum: 1275

7. Print the multiplication table of 7 using a for loop.

#include <stdio.h>

int main() {

for (int i = 1; i <= 10; i++) {

printf("7 x %d = %d\n", i, 7 * i);

}
return 0;

OUTPUT:- 7 x 1 = 7

7 x 2 = 14

7 x 3 = 21

7 x 4 = 28

7 x 5 = 35

7 x 6 = 42

7 x 7 = 49

7 x 8 = 56

7 x 9 = 63

7 x 10 = 70

8. Write a program to reverse a given number using a for loop.

#include <stdio.h>

int main() {

int num, reversed = 0;

printf("Enter a number: ");

scanf("%d", &num);

for (; num != 0;) {

reversed = reversed * 10 + num % 10;

num /= 10;

printf("Reversed Number: %d\n", reversed);

return 0;

}
OUTPUT:- Enter a number: 12345

Reversed Number: 54321

9. Print the first 15 terms of the Fibonacci series using a for loop

#include <stdio.h>

int main() {

int n1 = 0, n2 = 1, n3;

printf("Fibonacci Series: ");

for (int i = 0; i < 15; i++) {

printf("%d ", n1);

n3 = n1 + n2;

n1 = n2;

n2 = n3;

printf("\n");

return 0;

OUTPUT:- Fibonacci Series: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377

10.Print all natural numbers between 1 and 100 that are divisible by 5 using a for loop.

#include <stdio.h>

int main() {

printf("Numbers between 1 and 100 divisible by 5: ");

for (int i = 1; i <= 100; i++) {


if (i % 5 == 0) {

printf("%d ", i);

printf("\n");

return 0;

OUTPUT:- Numbers between 1 and 100 divisible by 5: 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80


85 90 95 100

11.Write a program to print the ASCII values of characters from 'A' to 'Z' using a for

#include <stdio.h>

int main() {

printf("ASCII values of characters from 'A' to 'Z':\n");

for (char ch = 'A'; ch <= 'Z'; ch++) {

printf("%c: %d\n", ch, ch);

return 0;

OUTPUT:- ASCII values of characters from 'A' to 'Z':

A: 65

B: 66

C: 67

D: 68

E: 69

F: 70

G: 71
H: 72

I: 73

J: 74

K: 75

L: 76

M: 77

N: 78

O: 79

P: 80

Q: 81

R: 82

S: 83

T: 84

U: 85

V: 86

W: 87

X: 88

Y: 89

Z: 90

12.Write a program to count the number of digits in a number using a for loop.

#include <stdio.h>

int main() {

int num, count = 0;

printf("Enter a number: ");

scanf("%d", &num);

for (; num != 0;) {


num /= 10;

count++;

printf("Number of digits: %d\n", count);

return 0;

OUTPUT:- Enter a number: 123456

Number of digits: 6

13.Print the sum of the squares of the first 20 natural numbers using a for loop.

#include <stdio.h>

int main() {

int sum = 0;

for (int i = 1; i <= 20; i++) {

sum += i * i;

printf("Sum of squares: %d\n", sum);

return 0;

OUTPUT:- Sum of squares: 2870

14.Print all numbers between 1 and 100 that are divisible by both 3 and 7 using a for

#include <stdio.h>
int main() {

printf("Numbers between 1 and 100 divisible by both 3 and 7: ");

for (int i = 1; i <= 100; i++) {

if (i % 3 == 0 && i % 7 == 0) {

printf("%d ", i);

printf("\n");

return 0;

OUTPUT:- Numbers between 1 and 100 divisible by both 3 and 7: 21 42 63 84

15.Check whether a given number is an Armstrong number using a for loop.

#include <stdio.h>

#include <math.h>

int main() {

int num, originalNum, remainder, n = 0;

printf("Enter an integer: ");

scanf("%d", &num);

originalNum = num;

for (; originalNum != 0;) {

originalNum /= 10;

n++;

}
originalNum = num;

int sum = 0;

for (; originalNum != 0;) {

remainder = originalNum % 10;

sum += pow(remainder, n);

originalNum /= 10;

if (sum == num)

printf("%d is an Armstrong number.\n", num);

else

printf("%d is not an Armstrong number.\n", num);

return 0;

OUTPUT:- Enter an integer: 153

153 is an Armstrong number.

16.Calculate the sum of the digits of a given number using a for loop.

#include <stdio.h>

int main() {

int num, sum = 0;

printf("Enter a number: ");

scanf("%d", &num);
for (; num != 0;) {

sum += num % 10;

num /= 10;

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

return 0;

OUTPUT:- Enter a number: 12345

Sum of digits: 15

17.Print all natural numbers between 1 and 100 that are divisible by 5 using a for loop.

#include <stdio.h>

int main() {

printf("Natural numbers between 1 and 100 divisible by 5: ");

for (int i = 1; i <= 100; i++) {

if (i % 5 == 0) {

printf("%d ", i);

printf("\n");

return 0;

OUTPUT:- Natural numbers between 1 and 100 divisible by 5: 5 10 15 20 25 30 35 40 45 50 55 60 65


70 75 80 85 90 95 100
18.Write a program to print the ASCII values of characters from 'A' to 'Z' using a for loop

#include <stdio.h>

int main() {

printf("ASCII values of characters from 'A' to 'Z':\n");

for (char ch = 'A'; ch <= 'Z'; ch++) {

printf("%c: %d\n", ch, ch);

return 0;

OUTPUT:- ASCII values of characters from 'A' to 'Z':

A: 65

B: 66

C: 67

D: 68

E: 69

F: 70

G: 71

H: 72

I: 73

J: 74

K: 75

L: 76

M: 77

N: 78

O: 79

P: 80

Q: 81

R: 82

S: 83
T: 84

U: 85

V: 86

W: 87

X: 88

Y: 89

Z: 90

19.Print below patterns:

12

123

1234

*****

****

***

**

121

12321

1234321

12321

121

**

***
****

*****

44

333

2222

11111

AB

ABC

ABCD

BB

CCC

DDDD

11

121

1331

1. #include <stdio.h>

int main() {

for (int i = 1; i <= 4; i++) {

for (int j = 1; j <= i; j++) {

printf("%d ", j);

}
printf("\n");

printf("*****\n");

for (int i = 5; i >= 1; i--) {

for (int j = 1; j <= i; j++) {

printf("*");

printf("\n");

return 0;

2. #include <stdio.h>

int main() {

for (int i = 1; i <= 4; i++) {

for (int j = 1; j <= i; j++) {

printf("%d", j);

for (int j = i - 1; j >= 1; j--) {

printf("%d", j);

printf("\n");

for (int i = 3; i >= 1; i--) {

for (int j = 1; j <= i; j++) {

printf("%d", j);

for (int j = i - 1; j >= 1; j--) {

printf("%d", j);

}
printf("\n");

return 0;

3. #include <stdio.h>

int main() {

for (int i = 1; i <= 5; i++) {

for (int j = 1; j <= i; j++) {

printf("*");

printf("\n");

return 0;

4. #include <stdio.h>

int main() {

for (int i = 1; i <= 5; i++) {

for (int j = 1; j <= i; j++) {

printf("*");

printf("\n");

return 0;

5. #include <stdio.h>
int main() {

for (int i = 5; i >= 1; i--) {

for (int j = 1; j <= 6 - i; j++) {

printf("%d ", i);

printf("\n");

return 0;

6. #include <stdio.h>

int main() {

for (char ch = 'A'; ch <= 'D'; ch++) {

for (char j = 'A'; j <= ch; j++) {

printf("%c ", j);

printf("\n");

return 0;

7. #include <stdio.h>

int main() {

for (char ch = 'A'; ch <= 'D'; ch++) {

for (int j = 1; j <= ch - 'A' + 1; j++) {

printf("%c ", ch);

printf("\n");

}
return 0;

8. #include <stdio.h>

int main() {

int rows = 4;

for (int i = 0; i < rows; i++) {

int coefficient = 1;

for (int j = 0; j < rows - i - 1; j++) {

printf(" ");

for (int j = 0; j <= i; j++) {

printf("%d ", coefficient);

coefficient = coefficient * (i - j) / (j + 1);

printf("\n");

return 0;

You might also like