FCP Assignment 5
Anurag Bhunia
Chapter 7
Q7.
#include <stdio.h>
int main(){
double n,sum=0;
printf("This program sums a series of number.\n");
printf("Enter integers (0 to terminate):");
scanf("%lf",&n);
while(n!=0){
sum+=n;
scanf("%lf",&n);
}
printf("The sum is: %lf\n",sum);
}
Q9.
#include <stdio.h>
int main(){
int hr,min;
char ap[3];
printf("Enter the time in 12 hour format:");
scanf("%d:%d %2s",&hr,&min,ap);
if(ap[0]=='a' || ap[0]=='A'){
if(hr==12){
hr=0;
}
}
else if(ap[0]=='p' || ap[0]=='P'){
if(hr!=12){
hr+=12;
}
}
else{
printf("Invalid input");
}
printf("Time in 24 hour format: %02d:%02d",hr,min);
}
Q10.
#include <stdio.h>
int main() {
char a[]={'a','A','e','E','i','I','o','O','u','U'};
char s[1000];
int f=0;
printf("Enter a sentence:");
fgets(s,sizeof(s),stdin);
int n=0,m=sizeof(a)/sizeof(a[0]);
while(s[n]!='\0') {
n++;
}
for (int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(a[j]==s[i]){
f+=1;
break;
}
}
}
printf("The number of vowels in this sentence is %d\n",
f);
}
Q13.
#include <stdio.h>
#include <string.h>
int main(){
char s[1000];
printf("Enter a sentence:");
fgets(s,1000,stdin);
int len=0,words=0,i=0;
while(s[i]!='\0' && s[i]!='\n'){
if(s[i]!=' '){
len++;
if(s[i+1]==' ' || s[i+1]=='\0' || s[i+1]=='\n'){
words++;
}
}
i++;
}
if(words==0){
printf("No words entered");
}
else{
double avg=(double)len/words;
printf("Average word length: %.2f",avg);
}
}
Q14.
#include <stdio.h>
#include <math.h>
int main(){
double x;
printf("Enter the number whose square root you want to
find:");
scanf("%lf",&x);
if(x<0){
printf("Square root of negative number is not
real");
}
double y=x;
double y_new;
while(1){
y_new=0.5*(y+x/y);
if(fabs(y_new-y)<0.00001*y_new){
break;
}
y=y_new;
}
printf("Approximate square root: %.10f",y_new);
}