Posts

Showing posts from September, 2019

25. Write a program to evaluate the series 1^2+2^2+3^2+……+n^2

Image
#include<stdio.h> #include<conio.h> #include<math.h>  /* Here we are using math.h header file because we are using pow function under                                     this header file. */ void main() { int i,n,sum=0; printf("Enter your Numbers: "); scanf("%d",&n); for(i=1;i<=n;i++) { sum=sum+pow(i,2); } printf("Sum=%d",sum); getch(); } #OUTPUT:

24. Write a program to check whether the given number is prime or not.

Image
#include <stdio.h> #include<conio.h> void main() {     int a,b,val=0;     printf("Enter a positive integer: ");     scanf("%d", &a);     for(b = 2; b <= a/2; ++b)     {         // condition for nonprime number         if(a%b == 0)         {             val = 1;             break;         }     }     if (b == 1)     {       printf("1 is neither a prime nor a composite number.");     }     else     {         if (val == 0)           printf("%d is a prime number.",a);         else           printf("%d is not a prime number.",a);     }     getch(); } #OUTPUT:

23. Read five persons height and weight and count the number of person having height greater than 170 and weight less than 50,

Image
#include<stdio.h> #include<conio.h> void main() { int i,j,str[5][2],count=0; printf("Enter Height and Weight:\n"); for(i=0;i<5;i++) { for(j=0;j<2;j++) { scanf("%d",&str[i][j]); } } for(i=0;i<5;i++) { for(j=0;j<2;j++) { if(str[i][0]>=170 && str[i][j]<=50) { count++; } } } printf("Number of Students= %d",count); getch(); } #OUTPUT: