You are on page 1of 6

Faculty of Engineering & Technology

303105104 - Computational Thinking for Structured Design-1

6)Write a C Program to find sum of individual digits of a positive number

#include <stdio.h>

int main() { int num, digit,

sum_of_digits = 0; printf("Enter

an integer: ");

scanf("%d", &num);

while (num > 0) { //

Get the last digit

digit = num % 10;

// Add the digit to the sum sum_of_digits

+= digit;

// Remove the last digit from the number

num = num / 10;

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

return 0;

Enrollment No: Page No:


Faculty of Engineering & Technology
303105104 - Computational Thinking for Structured Design-1
7)A Fibonacci sequence is defined as follows : the first and second terms in
sequences are 0 and Subsequent terms are

#include <stdio.h>

int main() { int n, first = 0, second =

1, next, i = 2;

printf("Enter the number of terms: ");

scanf("%d", &n);

printf("Fibonacci Series: %d, %d, ", first, second);

while (i < n) { next

= first + second;

printf("%d, ", next);

first = second;

second = next;

i++;

printf("\n");

return 0;

Enrollment No: Page No:


Faculty of Engineering & Technology
303105104 - Computational Thinking for Structured Design-1
8)To find factorial of given integer

#include <stdio.h>

int main() {

int n;

unsigned long long factorial = 1; // Use "unsigned long long" to handle larger factorials

printf("Enter a non-negative integer: ");

scanf("%d", &n);

if (n < 0) {

printf("Factorial is not defined for negative numbers.\n");

} else { for (int i = 1; i

<= n; i++) { factorial

*= i;

printf("Factorial of %d = %llu\n", n, factorial);

return 0;

Enrollment No: Page No:


Faculty of Engineering & Technology
303105104 - Computational Thinking for Structured Design-1
9)LCM of Two numbers

#include <stdio.h>

int main() { int num1,

num2, max;

printf("Enter two numbers: ");

scanf("%d %d", &num1, &num2);

if (num1 < 0 || num2 < 0) { printf("Please

enter non-negative numbers.\n");

} else {

// Find the maximum of the two numbers

max = (num1 > num2) ? num1 : num2;

while (1) {

if (max % num1 == 0 && max % num2 == 0) {

printf("LCM of %d and %d is %d\n", num1, num2, max);

break;

max++;

return 0;

Enrollment No: Page No:


Faculty of Engineering & Technology
303105104 - Computational Thinking for Structured Design-1
10)Prime Number
#include <stdio.h>

int main() {
int num;
int isPrime = 1; // Assume the number is prime by default

printf("Enter a number: ");


scanf("%d", &num);

if (num <= 1) {
isPrime = 0; // Numbers less than or equal to 1 are not prime
}
else {
for (int i = 2; i * i <= num; i++) {
if (num % i == 0) {
isPrime = 0;
break;
}
}
}

if (isPrime) {
printf("%d is a prime number.\n", num);
} else {
printf("%d is not a prime number.\n", num);
}

return 0;
}

Enrollment No: Page No:


Faculty of Engineering & Technology
303105104 - Computational Thinking for Structured Design-1
11)GCD Number
#include <stdio.h>

int main() {
int num1, num2;

printf("Enter two numbers: ");


scanf("%d %d", &num1, &num2);

while (num1 != num2) {


if (num1 > num2) {
num1 -= num2;
} else {
num2 -= num1;
}
}

printf("GCD: %d\n", num1);

return 0;
}

Enrollment No: Page No:

You might also like