Posts

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:

22. Write a program to calculate average and total of 5 students for 3 subjects (use nested for loops)

Image
#include<stdio.h> #include<conio.h> void main() { int i,j,std[5][3]; float sum[5],avg[5]; printf("Enter the marks of students:\n"); for(i=0;i<5;i++) { for(j=0;j<3;j++) { scanf("%d",&std[i][j]); } } for(i=0;i<5;i++) { for(j=0;j<3;j++) { printf("%d  ",std[i][j]); } printf("\n"); } for(i=0;i<5;i++) { for(j=0;j<3;j++) { sum[i]=sum[i]+std[i][j]; } } for(i=0;i<5;i++) { for(j=0;j<3;j++) { avg[i]=sum[i]/3; } } for(i=0;i<5;i++) { printf("sum[%d]=%f   ",i,sum[i]); printf("avg[%d]=%f\n",i,avg[i]); } getch(); } #OUTPUTS:

21. Write a C program to find the sum and average of different numbers which are accepted by user as many as user wants

Image
#include<stdio.h> #include<conio.h> void main() { int a,c[100],i; float avg,sum=0; printf("How many numbers do you want to enter: "); scanf("%d",&a); printf("Enter the Numbers:\n"); for(i=0;i<a;i++) { scanf("%d",&c[i]); } for(i=0;i<a;i++) { sum=sum+c[i]; } avg=sum/a; printf("Sum is %f\n Avg is %f",sum,avg); } #OUTPUT:

20. Write a program to find out sum of first and last digit of a given number.

Image
#include<stdio.h> #include<conio.h> void main() { int fd,ld,sum,num; printf("Enter your number:"); scanf("%d",&num); if(num>9) { ld=num%10; while(num>9) { num=num/10; } fd=num; sum=fd+ld; printf("Sum of first digit and last digit is %d",sum); } else { printf("You entered one digit no %d",num); } getch(); } #OUTPUT:

19. Write a program to generate first n number of Fibonacci series

Image
#include<stdio.h> #include<conio.h> void main() { int n,a=0,b=1,c,i; printf("Enter the number:"); scanf("%d",&n); printf("0"); for(i=1;i<=n;i++) { c=a+b; a=b; b=c; printf("%d",a,b); } getch(); } #OUTPUT: