CPL ASSIGNMENT TASK 2
CPL ASSIGNMENT TASK 2
#include <stdio.h>
int main()
int i=1;
while (i<11)
printf("%d\n", i);
i++;
return 0;
OUTPUT:- 1
10
2. Create a program that calculates the sum of the first N positive integers using a while loop.
#include <stdio.h>
int main()
int N, i = 1;
int sum = 0;
scanf("%d", &N);
while (i <= N)
sum += i;
i++;
return 0;
3. Write a program that reads a number from the user and prints its multiplication table using a while
loop.
#include <stdio.h>
int main()
int number, i = 1;
printf("Enter a number: ");
scanf("%d", &number);
i++;
return 0;
3x1=3
3x2=6
3x3=9
3 x 4 = 12
3 x 5 = 15
3 x 6 = 18
3 x 7 = 21
3 x 8 = 24
3 x 9 = 27
3 x 10 = 30
4. Develop a program that reads a number and prints all even numbers up to that number using a
while loop.
#include <stdio.h>
int main()
int number, i = 2;
scanf("%d", &number);
printf("%d\n", i);
i += 2;
return 0;
10
#include <stdio.h>
int main()
{
int number, i = 1;
scanf("%d", &number);
if (number < 0) {
} else {
factorial *= i;
i++;
return 0;
6. Write a program that calculates the sum of digits of a given number using a while loop.
#include <stdio.h>
int main()
scanf("%d", &number);
number /= 10;
return 0;
7. Write a program that generates the Fibonacci series up to a given number using a while loop.
#include <stdio.h>
int main()
scanf("%d", &limit);
next = a + b;
a = b;
b = next;
printf("\n");
return 0;
0112358
8. Write a program to count the number of digits in a given integer using a while loop.
#include <stdio.h>
int main()
scanf("%d", &number);
if (number == 0) {
count = 1;
} else {
number /= 10;
count++;
return 0;
Number of digits: 5
9. Write a program that calculates the multiplication of digits of a given number using a while loop.
#include <stdio.h>
int main()
scanf("%d", &number);
if (number == 0) {
product = 0;
} else {
number /= 10;
return 0;
10. Create a program to find the greatest common divisor (GCD) of two numbers using a while loop.
#include <stdio.h>
int main()
int a, b;
if (b > a) {
int temp = a;
a = b;
b = temp;
}
while (b != 0)
int temp = b;
b = a % b;
a = temp;
return 0;
GCD is: 6
11. Develop a program to find the least common multiple (LCM) of two numbers using a while loop.
#include<stdio.h>
int main(){
int n1,n2,max,lcm;
lcm=max;
lcm+=max;
}
printf("The LCM of %d and %d is %d.",n1,n2,lcm);
return 0;