Uploaded by tonmoy01722360581

Tonmoy Lab6

advertisement
CSC – 184
C Programming Lab
Report No. 6
Date of Submission: 26.12.2021
Submitted to,
Nusrath Tabassum
Submitted by,
Md. Al Imran Tonmoy
ID: 21203061
SL – 27
Department of Computer Science and Engineering
IUBAT – International University of Business Agriculture and Technology
Problem-1: Write a c program to read n numbers from keyboard and find their sum and average
#include<stdio.h>
int main()
{
int i,n,sum;
sum=0;
scanf("%d",&n);
for(i=1; i<=n; i++)
{
sum=sum+i;
}
printf("Sum is %d\n",sum);
int avg;
avg=sum/n;
printf("Average is %d",avg);
return 0;
}
OUTPUT:
Problem-2: Write a c program to find the sum of odd number from 1 for 1+3+5+…+n.
#include <stdio.h>
int main()
{
int i, n, sum = 0;
printf("Type Your Number:");
scanf("%d",&n);
for(i = 1; i <= n; i+=2){
sum = sum + i;
printf("Odd Number: %d \n",i);
}
printf("Odd Number Sum : %d", sum);
return 0;
}
OUTPUT:
Problem-3: Write a c program to display the cube of the number up to a given integer.
#include <stdio.h>
int main(){
int i, n, cube;
printf("Type Your Number:");
scanf("%d",&n);
for(i = 1; i <= n; i++){
cube = i*i*i;
printf("Given Number = %d => Power = %d^3 = %d \n",n,i,cube);
}
return 0;
}
OUTPUT:
Problem-4: Write a c program to find the sum 1^2+2^2+…+10^2.
#include <stdio.h>
#include <math.h>
int main(){
int i, n = 10, sum = 0;
for(i=1; i<=n; i+=1){
sum = sum + i*i;
printf("Num : %d^2 = sum = %d \n", i, sum);
}
return 0;
}
OUTPUT:
Problem-5: Write a c program to display the sum of terms harmonic series 1+1/2+1/3+…..1/n.
#include<stdio.h>
int main()
{
float i,n,sum;
sum=0;
scanf("%f",&n);
for(i=1; i<=n; i=i+1)
{
sum=sum+1/i;
}
printf("%f",sum);
return 0;
}
OUTPUT:
Problem-6: Write a c program to find out the sum of 1+1/4+1/9+1/16+1/25.
#include <stdio.h>
int main()
{
float i, n,sum;
sum=0;
for(i = 1; i <= 5; i++)
{
sum = sum+1/(i*i);
}
printf("sum is %f",sum);
return 0;
}
OUTPUT:
Problem-7: Write a c program to print 10 to 1.
#include<stdio.h>
int main()
{
int i;
for(i=10; i>=1; i--)
{
printf("%d\n",i);
}
return 0;
}
OUTPUT:
Download