In: Computer Science
"This C Programming "
Write a program that accepts four (4) lines of input:
• The first line contains a float value in 4.2f format
• The second line contains a char value
• The third line contains a 4-digit int value
• The fourth line contains a char value
and then displays all of the input on a single line
Write a program that accepts a phone number of the form +1(xxx)-xxx-xxxx
where x is a digit, and displays the sum of all digits in the phone number.
First Program:
#include<stdio.h>
int main(){
float a;
char b;
int c;
char d;
printf("Enter the float value in line 1, Character in the next
line, integer in next line and Character in the 4th line:
\n");
// Getting the inputs form different lines
scanf("%f",&a);
scanf(" %c",&b);
scanf("%d",&c);
scanf(" %c",&d);
printf("Output in single line\n");
//Displaying the output in single line
printf("%4.2f %c %d %c",a,b,c,d);
return 0;
}
Output:
Second Program:
#include<stdio.h>
int main(){
char phone[20] ;
int a;
int total = 0;
printf("Enter the phone number in the form +1(xxx)-xxx-xxxx
\n");
//Getting the phone number and save it in the character array
gets(phone);
//for loop is to iterate over the character array to find the
total
for(a = 2; a <= 20; a++){//initial a= 2 to ignore the '+1'
switch(phone[a]){
case '1':
total = total+1;
break;
case '2':
total = total+2;
break;
case '3':
total = total+3;
break;
case '4':
total = total+4;
break;
case '5':
total = total+5;
break;
case '6':
total = total+6;
break;
case '7':
total = total+7;
break;
case '8':
total = total+8;
break;
case '9':
total = total+9;
break;
default:
total= total+0;
break;
}
}
printf("Total of the digits of the phone number is %d",
total);
return 0;
}
Output: