Computer Concept & Programming In C - Unit III - 12

Q.24. Write a program in C language to generate the given series upto term less than 200.                       1 - 4 + 9 - 16 + 25 - ..........                            (AKTU. 2011 - 12)
Ans.
# include<stdio.h>
# include<conio.h>
# include<math.h>
      void main ( )
       {
double i, sum = 0;
i = 1.0;
while ((i*i) < 200.0)
  {
sum + = ((pow(-1.0, i + 1))*i*i);
i++;
   }
printf(“\n sum = %lf”, sum);
getch( );
 }

Q.25. Draw the flow chart and write a function in C to calculate the sum S of all the following series 
S = 1l + 22 + 33 + 44 + ... + NN (N is a positive integer)                          (AKTU. 2010 - 11)
Ans. C function to calculate the sum S of all the following series
S = 1l + 22 + 33 + 44 + ... + NN
long series_sum (int N)
       {
long sum = 0, p = 1;
inti, j;
for (i = 1; i < = N; i++)
    {
p = 1;
for(j = 1; j < = i; j++)
{
p* = i;
}
sum + = p;
}
      return sum;
}



Q.26. Give the flow chart and algorithm to calculate the number of words in a given sentence.                                                (AKTU. 2010 - 11)
Related Questions -
Q. Write a program in C to accept a sentence of 9 or more words. Due to some error two or more blank spaces have been entered between the words. The program should also remove the extra blank space between the words and also print the longest word in that sentence.                                                                                         (AKTU. 2012 - 13)
Ans.

Number of words in a given sentence.
1. blank ¬ 0
2. size ¬ 30
3. Write “Enter a sentence”
4. read sent [size]
5. for i ¬ 1 to (size - 1)
6.             do if (sent [i] ) and (sent[i + 1] = )
7.                             then blank ¬ blank + 1
8. Write “Total number of words in a sentence”, blank + 1