B.
Tech Sem 1 CPL TASK 2
1. Write a program to print the first 10 natural numbers using a while loop.
#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;
printf("Enter a positive integer N: ");
scanf("%d", &N);
while (i <= N)
sum += i;
i++;
printf("The sum of the first %d positive integers is: %d\n", N, sum);
return 0;
OUTPUT:- Enter a positive integer N: 5
The sum of the first 5 positive integers is: 15
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);
printf("Multiplication table for %d:\n", number);
while (i <= 10)
printf("%d x %d = %d\n", number, i, number * i);
i++;
return 0;
OUTPUT:- Enter a number: 3
Multiplication table for 3:
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;
printf("Enter a number: ");
scanf("%d", &number);
printf("Even numbers up to %d:\n", number);
while (i <= number)
printf("%d\n", i);
i += 2;
return 0;
OUTPUT:- Enter a number: 10
Even numbers up to 10:
10
5. Write a program to find the factorial of a number using a while loop.
#include <stdio.h>
int main()
{
int number, i = 1;
unsigned long long factorial = 1;
printf("Enter a positive integer: ");
scanf("%d", &number);
if (number < 0) {
printf("Factorial is not defined for negative numbers.\n");
} else {
while (i <= number)
factorial *= i;
i++;
printf("Factorial of %d is: %llu\n", number, factorial);
return 0;
OUTPUT:- Enter a positive integer: 5
Factorial of 5 is: 120
6. Write a program that calculates the sum of digits of a given number using a while loop.
#include <stdio.h>
int main()
int number, sum = 0;
printf("Enter a number: ");
scanf("%d", &number);
number = (number < 0) ? -number : number;
while (number > 0)
sum += number % 10;
number /= 10;
printf("Sum of the digits is: %d\n", sum);
return 0;
OUTPUT:- Enter a number: 1234
Sum of the digits is: 10
7. Write a program that generates the Fibonacci series up to a given number using a while loop.
#include <stdio.h>
int main()
int limit, a = 0, b = 1, next;
printf("Enter the limit for the Fibonacci series: ");
scanf("%d", &limit);
printf("Fibonacci series up to %d:\n", limit);
while (a <= limit)
printf("%d ", a);
next = a + b;
a = b;
b = next;
printf("\n");
return 0;
OUTPUT:- Enter the limit for the Fibonacci series: 10
Fibonacci series up to 10:
0112358
8. Write a program to count the number of digits in a given integer using a while loop.
#include <stdio.h>
int main()
int number, count = 0;
printf("Enter an integer: ");
scanf("%d", &number);
number = (number < 0) ? -number : number;
if (number == 0) {
count = 1;
} else {
while (number > 0)
number /= 10;
count++;
printf("Number of digits: %d\n", count);
return 0;
OUTPUT:- Enter an integer: -12345
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()
int number, product = 1;
printf("Enter a number: ");
scanf("%d", &number);
number = (number < 0) ? -number : number;
if (number == 0) {
product = 0;
} else {
while (number > 0)
product *= number % 10;
number /= 10;
printf("Multiplication of the digits: %d\n", product);
return 0;
OUTPUT:- Enter a number: 1234
Multiplication of the digits: 24
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;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
// Ensure a is the greater number
if (b > a) {
int temp = a;
a = b;
b = temp;
}
while (b != 0)
int temp = b;
b = a % b;
a = temp;
printf("GCD is: %d\n", a);
return 0;
OUTPUT:- Enter two numbers: 48 18
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;
printf(“Enter two positive integer:”);
scanf("%d %d", &n1, &n2);
max=(n1 > n2) ? n1 : n2;
lcm=max;
while((lcm%n1 != 0) || (lcm % n2 !=0))
lcm+=max;
}
printf("The LCM of %d and %d is %d.",n1,n2,lcm);
return 0;
OUTPUT:- Enter two positive numbers: 12 18
The LCM of 12 and 18 is 36