3 RD
3 RD
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
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
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:
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
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
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