0% found this document useful (0 votes)
12 views10 pages

3 RD

Uploaded by

shyamanth1611
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)
12 views10 pages

3 RD

Uploaded by

shyamanth1611
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/ 10

24I260 SHYAMANTH K B

EX.NO : 3
21/10/2024
Control statements – Looping
Aim:
To implement the solutions of the problem using C language
1.Find sum of all even numbers and sum of odd numbers between 1 and n.
Source code:
#include <stdio.h>
int main(){
int n, sumEven = 0, sumOdd = 0;
printf("Enter a number:");
scanf("%d",&n);
for(int i = 1; i <= n; i++)
{
if(i % 2 == 0)
{
sumEven += i;
}
else
{
sumOdd += i;
}
}
printf("Sum of even numbers: %d\n", sumEven);
printf("Sum of odd numbers: %d\n", sumOdd);
return 0;
}
Output:

1
24I260 SHYAMANTH K B

2. Print multiplication table of given number.


Source code:
#include <stdio.h>
int main()
{
int num,i;
printf("Enter a number: ");
scanf("%d", &num);
printf("Multiplication Table of %d\n" ,num);
for(i = 1; i <= 10; i++)
{
printf("\n%d x %d = %d\n",num, i, num * i);
}
return 0;
}

Output:

2
24I260 SHYAMANTH K B

3. Find first and last digit of a number and do the following operations:
● Sum of first and last digit
● Swapping of first and last digit
Source code:
#include <stdio.h>
int main() {
int num, first, last, temp, divisor = 1,swappedNum;
printf("Enter a number: ");
scanf("%d", &num);
last = num % 10;
temp = num;
while(temp >= 10)
{
temp /= 10;
divisor *= 10;
}
first = temp;
printf("First digit: %d\n", first);
printf("Last digit: %d\n", last);
printf("Sum of first and last digit: %d\n", first + last);
swappedNum = last * divisor + (num % divisor - last + first);
printf("Number after swapping first and last digit: %d\n", swappedNum);
return 0;
}
Output:

3
24I260 SHYAMANTH K B

4. Input a number from user and calculate product of its digits.


Source code:
#include <stdio.h>
int main()
{
int num, product = 1, digit;
printf("Enter a number: ");
scanf("%d", &num);
while(num > 0)
{
digit = num % 10;
product = product*digit;
num = num/10;
}
printf("Product of digits: %d\n", product);
return 0;
}
Output:

5. Input a number from user and check number is palindrome or not.


Source code:
#include <stdio.h>
int main() {
int num, reverse = 0, original, rem;
printf("Enter a number: ");
scanf("%d", &num);
original = num;
while(num> 0) {
rem = num % 10;
reverse = reverse * 10 + rem;
num /= 10;
}

4
24I260 SHYAMANTH K B

if(original == reverse) {
printf("The number is a palindrome.\n");
}
else
{
printf("The number is not a palindrome.\n");
}
return 0;
}
Output:

6. Read a number from user and print it into words.

Source code:

#include <stdio.h>
int main()
{
int num, reverse = 0, lastDigit;
printf("Enter a number: ");
scanf("%d", &num);
while(num > 0)
{
lastDigit = num % 10;
reverse = reverse * 10 + lastDigit;
num /= 10;
}
while(reverse > 0) {
lastDigit = reverse%10;
switch(lastDigit) {

5
24I260 SHYAMANTH K B

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;
}

6
24I260 SHYAMANTH K B

reverse/=10;}
return 0;
}
Output:

7. Find HCF(GCD) and LCM of the given two numbers.


Source code:
#include <stdio.h>
int main() {
int n1,n2,i,hcf,lcm;
printf("Enter two numbers: ");
scanf("%d %d",&n1, &n2);
for(i=1;i<=n1&&i<=n2;++i){
if(n1%i==0&&n2%i==0){
hcf=i;
}}
lcm=(n1*n2)/hcf;
printf("HCF= %d\nLCM= %d",hcf,lcm);
return 0;
}
Output:

7
24I260 SHYAMANTH K B

8. Read number from user and check whether number is Strong number or not.
Source code:
#include <stdio.h>
int main() {
int num,i,original,sum=0,lastdigit,fact;
printf("Enter a number:");
scanf("%d",&num);
original=num;
while(num>0)
{
lastdigit=num%10;
fact=1;
for(i = 1; i <=lastdigit; i++)
{
fact *= i;
}
sum=sum+fact;
num=num/10;
}
if(sum == original) {
printf("The number is a Strong number.\n");
}
else {
printf("The number is not a Strong number.\n");
}
return 0;
}
Output:

8
24I260 SHYAMANTH K B

9. Find the number is perfect number or not.


Source code:
#include <stdio.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+=i;
}
}
if(sum==num) {
printf("The number is a Perfect number.\n");
}
else {
printf("The number is not a Perfect number.\n");
}
return 0;
}

Output:

10. Read a number from user and check whether given number is Armstrong number or
Not.
Source code:
#include <stdio.h>
#include <math.h>
void main() {
int n,digit=0,sum=0,rem,temp;
printf("Enter a number: ");
scanf("%d, &n");

9
24I260 SHYAMANTH K B

temp = n;
while(temp>0){
rem=temp%10;
digit++;
temp=temp/10;
}
while(n>0){
rem=n%10;
sum=sum + pow(rem,digit);
n=n/10;
}

if(sum==n)
{
printf("The number is an Armstrong number.\n");
} else {
printf("The number is not an Armstrong number.\n");
}
}
Output:

Result:
All the solutions are implemented using C language successfully.

10

You might also like