C Language Tutorial Home

# If Condition Programs Set 6

Question:26
C program to take input for a 3 digit number and print sum of the digits?
Sol:

```#include <stdio.h>
int main()
{
int n,d1,d2,d3,s;
printf("Enter a 3 digit no ");
scanf("%d",&n);
if(n>=100 && n<=999)
{
d1=n%10;
n=n/10;
d2=n%10;
n=n/10;
d3=n%10;
s=d1+d2+d3;
printf("Sum of digits = %d",s);
}
else
printf("Number is not a 3 digit no");
return 0;
}```

Output:

case :1
Enter a 3 digit no 125
Sum of digits = 8

case 2:
Enter a 3 digit no 14
Number is not a 3 digit no

case 3:
Enter a 3 digit no 5
Number is not a 3 digit no

Question:27
C program to take input for a 3 digit number,calculate and print sum 1st and last digits?
Sol:

```#include <stdio.h>
int main()
{
int n,d1,d2,d3,s;
printf("Enter a 3 digit no ");
scanf("%d",&n);
if(n>=100 && n<=999)
{
d1=n%10;
n=n/10;
d2=n%10;
n=n/10;
d3=n%10;
s=d1+d3;
printf("Sum of 1st and last digits = %d",s);
}
else
printf("Number is not a 3 digit no");
return 0;
}```

Output:

case :1
Enter a 3 digit no 253
Sum of 1st and last digits = 5

case 2:
Enter a 3 digit no 968
Sum of 1st and last digits = 17

case 3:
Enter a 3 digit no 25
Number is not a 3 digit no

Question:28
C program to take input for a 4 digit number,calculate and print sum of the digits?
Sol:

```#include <stdio.h>
int main()
{
int n,d1,d2,d3,d4,s;
printf("Enter a 4 digit no ");
scanf("%d",&n);
if(n>=1000 && n<=9999)
{
d1=n%10;
n=n/10;
d2=n%10;
n=n/10;
d3=n%10;
n=n/10;
d4=n%10;
s=d1+d2+d3+d4;
printf("Sum of digits = %d",s);
}
else
printf("Number is not a 4 digit no");
return 0;
}```

Output:

case :1
Enter a 4 digit no 1234
Sum of digits = 10

case 2:
Enter a 4 digit no 1452
Sum of digits = 12

case 3:
Enter a 4 digit no 25
Number is not a 4 digit no

Question:29
C program to take input for a 4 digit number,calculate and print sum 1 and last digits?
Sol:

```#include <stdio.h>
int main()
{
int n,d1,d2,d3,d4,s;
printf("Enter a 4 digit no ");
scanf("%d",&n);
if(n>=1000 && n<=9999)
{
d1=n%10;
n=n/10;
d2=n%10;
n=n/10;
d3=n%10;
n=n/10;
d4=n%10;
s=d1+d4;
printf("Sum of 1st and last digits = %d",s);
}
else
printf("Number is not a 4 digit no");
return 0;
}```

Output:

case :1
Enter a 4 digit no 1526
Sum of 1st and last digits = 7

case 2:
Enter a 4 digit no 9657
Sum of 1st and last digits = 16

case 3:
Enter a 4 digit no 65
Number is not a 4 digit no

Question:30
Write a C program to take input for a 4 digit number,calculate and print sum of even digits?
Sol:

```#include <stdio.h>
int main()
{
int n,d1,d2,d3,d4,s=0;
printf("Enter a 4 digit no ");
scanf("%d",&n);
if(n>=1000 && n<=9999)
{
d1=n%10;
if(d1%2==0)
s=s+d1;
n=n/10;
d2=n%10;
if(d2%2==0)
s=s+d2;
n=n/10;
d3=n%10;
if(d3%2==0)
s=s+d3;
n=n/10;
d4=n%10;
if(d4%2==0)
s=s+d4;
printf("Sum of even digits = %d",s);
}
else
printf("Number is not a 4 digit no");
return 0;
}```

Output:

case :1
Enter a 4 digit no 1245
Sum of even digits = 6

case 2:
Enter a 4 digit no 2486
Sum of even digits = 20

case 3:
Enter a 4 digit no 1358
Sum of even digits = 8

Important Pages