Question:31
Write a C program to take input for a 4 digit number,calculate and print sum of odd 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==1) s=s+d1; n=n/10; d2=n%10; if(d2%2==1) s=s+d2; n=n/10; d3=n%10; if(d3%2==1) s=s+d3; n=n/10; d4=n%10; if(d4%2==1) s=s+d4; printf("Sum of odd 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 odd digits = 4
case 2:
Enter a 4 digit no 1357
Sum of odd digits = 16
case 3:
Enter a 4 digit no 2365
Sum of odd digits = 8
Question:32
C program to take input for day number (1..7) and print the day?
for example:
input output
1 Sun
2 Mon
3 Tue
4 Wed
5 Thru
6 Fri
7 Sat
Sol:
#include <stdio.h> int main() { int d; printf("Enter day no "); scanf("%d",&d); if(d==1) printf("Sun"); else if(d==2) printf("Mon"); else if(d==3) printf("Tue"); else if(d==4) printf("Wed"); else if(d==5) printf("Thru"); else if(d==6) printf("Fri"); else if(d==7) printf("Sat"); else printf("Invalid day number"); return 0; }
Output:
case 1:
Enter day no 5
Thru
case 2:
Enter day no 1
Sun
case 3:
Enter day no 15
Invalid day number
Question:33
Write a C program to take input for month number (1..12) and print the Month?
for example:
input output
1 Jan
2 Feb
3 Mar
and so on
Sol:
#include <stdio.h> int main() { int m; printf("Enter month no "); scanf("%d",&m); if(m==1) printf("Jan"); else if(m==2) printf("Feb"); else if(m==3) printf("Mar"); else if(m==4) printf("Apr"); else if(m==5) printf("May"); else if(m==6) printf("Jun"); else if(m==7) printf("Jul"); else if(m==8) printf("Aug"); else if(m==9) printf("Sep"); else if(m==10) printf("Oct"); else if(m==11) printf("Nov"); else if(m==12) printf("Dec"); else printf("Invalid month number"); return 0; }
Output:
case 1:
Enter month no 2
Feb
case 2:
Enter month no 1
Jan
case 3:
Enter month no 10
Oct
case 4:
Enter month no 15
Invalid month number