CEF 207 Programming I Tutorial Sheet 2: C Exercises

Telechargé par Alvin
Faculty of Engineering and Technology / Computer Engineering/ School Year 2025-2026
1
CEF 207: PROGRAMMING I
Tutorial sheet 2:
Part 1: solved exercises
Write a C program to input decimal number from user and convert to binary number
system
/**
* C program to convert from Decimal to Binary number system
*/
#include <stdio.h>
int main()
{
long long decimal, tempDecimal, binary;
int rem, place = 1;
binary = 0;
/* Input decimal number from user */
printf("Enter any decimal number: ");
scanf("%lld", &decimal);
tempDecimal = decimal;
/* Decimal to binary conversion */
while(tempDecimal > 0)
{
rem = tempDecimal % 2;
binary = (rem * place) + binary;
tempDecimal /= 2;
place *= 10;
}
printf("Decimal number = %lld\n", decimal);
printf("Binary number = %lld", binary);
Faculty of Engineering and Technology / Computer Engineering/ School Year 2025-2026
2
return 0;
}
Write a C program to input number of days from user and convert it to years, weeks
and days. How to convert days to years, weeks in C programming. Logic to convert
days to years, weeks and days in C program
/**
* C program to convert days in to years, weeks and days
*/
#include <stdio.h>
int main()
{
int days, years, weeks;
/* Input total number of days from user */
printf("Enter days: ");
scanf("%d", &days);
/* Conversion */
years = (days / 365); // Ignoring leap year
weeks = (days % 365) / 7;
days = days - ((years * 365) + (weeks * 7));
/* Print all resultant values */
printf("YEARS: %d\n", years);
printf("WEEKS: %d\n", weeks);
printf("DAYS: %d", days);
return 0;
}
part 2: Guided exercises
Write a C program to count number of digits in an integer
Step by step descriptive logic to count number of digits in given integer using loop.
1. Input a number from user. Store it in some variable say num.
2. Initialize another variable to store total digits say digit = 0.
Faculty of Engineering and Technology / Computer Engineering/ School Year 2025-2026
3
3. If num > 0 then increment count by 1 i.e. count++.
4. Divide num by 10 to remove last digit of the given number i.e. num = num / 10.
5. Repeat step 3 to 4 till num > 0 or num != 0.
C program to find sum of first and last digit of any number
Step by step descriptive logic to find sum of first and last digit using loop.
1. Input a number from user. Store it in some variable say num.
2. To find last digit of given number we modulo divide l the given number by 10. Which is
lastDigit = num % 10.
3. To find first digit we divide the given number by 10 till num is greater than 0.
4. Finally calculate sum of first and last digit i.e. sum = firstDigit + lastDigit
C program to find HCF of two numbers
Step by step descriptive logic to find HCF
1. Input two numbers from user. Store them in some variable say num1 and num2.
2. Declare and initialize a variable to hold hcf i.e. hcf = 1.
3. Find minimum between the given two numbers
4. Store the result in some variable say min = (num1<num2) ? num1 : num2;.
5. Run a loop from 1 to min, increment loop by 1 in each iteration. The loop structure should
look like for(i=1; i<=min; i++).
6. Inside the loop check if i is a factor of two numbers i.e. if i exactly divides the given two
numbers num1 and num2 then set i as HCF i.e. hcf = i.
Write a C program to input number from user and check whether number is Strong
number or not
note : a Strong number is a special number whose sum of factorial of digits is equal to the original
number. For example: 145 is strong number. Since, 1! + 4! + 5! = 145
Step by step descriptive logic to check strong number.
1. Input a number from user to check for strong number. Store this in a variable say
num. Copy it to a temporary variable for calculations purposes, say originalNum =
num.
2. Initialize another variable to store sum of factorial of digits, say sum = 0.
Faculty of Engineering and Technology / Computer Engineering/ School Year 2025-2026
4
3. finf last digit of the given number num. Store the result in a variable say lastDigit = num %
10.
4. find factorial of lastDigit. Store factorial in a variable say fact.
5. Add factorial to sum i.e. sum = sum + fact.
6. Remove last digit from num as it is not needed further.
7. Repeat steps 3 to 6 till num > 0.
8. After loop check condition for strong number. If sum == originalNum, then the given
number is Strong number otherwise not
part 3: consolidation exercises
Write a C program to print Fibonacci series up to n terms using loop
Fibonacci series is a series of numbers where the current number is the sum of previous two terms.
For Example: 0, 1, 1, 2, 3, 5, 8, 13, 21, ... , (n-1th + n-2th)
Write a C program to count frequency of digits in a given numbe
Write a C program to input a number from user and check whether given number is
Armstrong number or not
An Armstrong number is a n-digit number that is equal to the sum of the nth power of its digits. For
example 6 = 61 = 6 ; 371 = 33 + 73 + 13 = 371
Write a C program to input a number and check whether the number is Perfect
number or not
Perfect number is a positive integer which is equal to the sum of its proper positive divisors.
For example: 6 is the first perfect number
Proper divisors of 6 are 1, 2, 3
Sum of its proper divisors = 1 + 2 + 3 = 6.
Hence 6 is a perfect number.
Write a C program to find power of a number using for loop
Write a C program to input two numbers from user and find LCM (Lowest Common
Multiple) using loop
is a smallest positive integer that exactly divides two or more numbers.
Faculty of Engineering and Technology / Computer Engineering/ School Year 2025-2026
5
For Example
W
rite a C program to print pascal triangle up to n rows using loop
Pascal triangle is a triangular number pattern named after famous mathematician Blaise Pascal
For example Pascal triangle with 6 rows.
To find nth term of a pascal triangle we use following formula.
Where n is row number and k is term of that row.
Write a C program to input binary number from user and find twos complement of
the binary number
1 / 6 100%
La catégorie de ce document est-elle correcte?
Merci pour votre participation!

Faire une suggestion

Avez-vous trouvé des erreurs dans l'interface ou les textes ? Ou savez-vous comment améliorer l'interface utilisateur de StudyLib ? N'hésitez pas à envoyer vos suggestions. C'est très important pour nous!