0% found this document useful (0 votes)
61 views

Assignment 5 Based On Loop Control Structure

Uploaded by

HarishPatil
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)
61 views

Assignment 5 Based On Loop Control Structure

Uploaded by

HarishPatil
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/ 9

Assignments based on Loop Control Structure

1) Write a program to calculate sum of digits of a given input


number
#include <stdio.h>
#include<conio.h>
int main() {
int num, sum = 0, digit;
Clrscr();
printf("Enter a number: ");
scanf("%d", &num);

while (num != 0) {
digit = num % 10;
sum =sum+ digit;
num = num/10;
}

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

Getch() ;
}
2) Write a program to accept two numbers as a range and
display sum of all numbers between that
range.
#include <stdio.h>
#include<conio.h>
int main() {
int start, end, sum = 0;
Clrscr();
printf("Enter the starting number: ");
scanf("%d", &start);

printf("Enter the ending number: ");


scanf("%d", &end);

for (int i = start; i <= end; i++) {


sum =sum+ i;
}

printf("Sum of numbers between %d and %d is: %d\n", start,


end, sum);

Getch();
}
3) Write program to check whether a input number is Armstrong
number or not.
#include <stdio.h>
#include<conio.h>
#include <math.h>

int main() {
int num, originalNum, remainder, arm = 0, n = 0;
Clrscr();
printf("Enter an integer: ");
scanf("%d", &num);

originalNum = num;

// Count the number of digits

originalNum = num;

while (originalNum != 0) {
remainder = originalNum % 10;
arm = arm+(remainder*reminder*reminder);
originalNum =originalNum / 10;
}

// Check if the number is an Armstrong number


if (arm == num)
printf("%d is an Armstrong number.\n", num);
else
printf("%d is not an Armstrong number.\n", num);
Getch();
}
4) Write a program to accept a binary number and convert it into
decimal number
#include <stdio.h>
#include<conio.h>
#include <math.h>

int main() {
int binaryNum, decimalNum = 0, power = 0;
Clrscr();
printf("Enter a binary number: ");
scanf("%d", &binaryNum);

while (binaryNum != 0) {
decimalNum =decimalNum + (binaryNum % 10) * pow(2,
power++);
binaryNum = binaryNum/ 10;
}

printf("Decimal equivalent: %d\n", decimalNum);

Getch();;
}
5) Write a program to check whether a input number is perfect
number of not.
#include <stdio.h>
#include<conio.h>
int main() {
int num, sum = 0;

printf("Enter a number: ");


scanf("%d", &num);

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


if (num % i == 0) {
sum = sum+i;
}
}

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

Getch();
}
6) Write a program to calculate x y
#include <stdio.h>

int main() {
int x, y, result = 1;

printf("Enter the base number: ");


scanf("%d", &x);

printf("Enter the exponent: ");


scanf("%d", &y);

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


result=result* x;
}

printf("%d raised to the power of %d is %d\n", x, y, result);

Getch();
}

7) Write a program to check whether a input number is


palindrome or not.
#include <stdio.h>
#include<conio.h>
int main() {
int num, reversedNum = 0, originalNum, remainder;
Clrscr();
printf("Enter a number: ");
scanf("%d", &num);

originalNum = num;

while (num != 0) {
remainder = num % 10;
reversedNum = reversedNum * 10 + remainder;
num = num /10;
}

if (originalNum == reversedNum)
printf("%d is a palindrome number.\n", originalNum);
else
printf("%d is not a palindrome number.\n", originalNum);

Getch();
}

8) Write a program to display multiplication of two input


numbers without using * operator.
#include <stdio.h>
#include<conio.h>
int main() {
int num1, num2, product = 0;
Clrscr();
printf("Enter the first number: ");
scanf("%d", &num1);

printf("Enter the second number: ");


scanf("%d", &num2);

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


product = product+ num1;
}

printf("Product of %d and %d is %d\n", num1, num2,


product);

Getch();;
}
9) Write a program to calculate sum of first and last digit of a
number
#include <stdio.h>
#include<conio.h>
int main() {
int num, firstDigit, lastDigit, sum;
Clrscr();
printf("Enter a number: ");
scanf("%d", &num);

// Find the last digit


lastDigit = num % 10;

// Find the first digit


while (num >= 10) {
num = num/10;
}
firstDigit = num;

// Calculate the sum


sum = firstDigit + lastDigit;

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

Getch();
}

10) Write a program to accept a number and count number of


even, odd, zero digits within that number.
#include <stdio.h>
#include<conio.h>
int main() {
int num, digit, evenCount = 0, oddCount = 0, zeroCount = 0;
Clrscr();
printf("Enter a number: ");
scanf("%d", &num);

while (num != 0) {
digit = num % 10;

if (digit == 0) {
zeroCount++;
} else if (digit % 2 == 0) {
evenCount++;
} else {
oddCount++;
}

num /= 10;
}

printf("Even digits: %d\n", evenCount);


printf("Odd digits: %d\n", oddCount);
printf("Zero digits: %d\n", zeroCount);

Getch();
}
11) Write a program, which accepts a number n and displays
each digit in words. Example: 6702 Output =
Six-Seven-Zero-Two. (Hint: Reverse the number and use a
switch statement)
#include <stdio.h>
#include<conio.h>
int main() {
int num, reversedNum = 0, remainder;
Clrscr();
printf("Enter a number: ");
scanf("%d", &num);

// Reverse the number


while (num != 0) {
remainder = num % 10;
reversedNum = reversedNum * 10 + remainder;
num = num /10;
}

// Display each digit in words


while (reversedNum != 0) {
remainder = reversedNum % 10;

switch (remainder) {
case 0:
printf("Zero-");
break;
case 1:
printf("One-");
break;
case 2:
printf("Two-");
break;
case 3:
printf("Three-");
break;
case 4:
printf("Four-");
break;
case 5:
printf("Five-");
break;
case 6:
printf("Six-");
break;
case 7:
printf("Seven-");
break;
case 8:
printf("Eight-");
break;
case 9:
printf("Nine-");
break;
}

reversedNum =reversedNum / 10;


}

printf("\n");

Getch();
}

You might also like