কোডগুলো “CodeBlocks” এ করা। অন্য কম্পাইলার (যেমনঃ
Boreland C++, Devc++, Turbo C) এ একটু ঝামেলা করতে পারে। given কোডের
main()এর পূর্বে void অ্যাড করলে আর ঝামেলা হবে না আশা করি।
Codeblocks এ void টা by default. Happy Coding :)
1. 1+2+________+n
#include<stdio.h>
#include<conio.h>
int main()
{ float a,n;
float sum=0;
scanf("%f",&n);
for(a=1;a<=n;a++)
{
sum=sum+(1/a); }
printf("%f",sum);
}
2. 10 er ghorer namota
#include<stdio.h>
#include<conio.h>
main()
{
int i,j;
for(i=1;i<=10;i++)
{
for(j=1;j<=10;j++)
{
printf("%4d",i*j);/*4 ghor pore print korbe*/
}
printf("\n");
}
return 0;
}
3. cg
#include<stdio.h>
int main(){
int i,n;float c[15] ;float g[15];float sum1=0,sum2=0,a1,a2,cg;
printf("*******************************************************************************\n");
printf("calculation of cg:\n");
scanf("%d",&n);
printf("enter credit:");
for(i=1;i<=n;i++)
{scanf("%f",&c[i]);}
printf("enter gpa:");
for(i=1;i<=n;i++)
{scanf("%f",&g[i]);}
for(i=1;i<=n;i++){a1=c[i]*g[i]; sum1=sum1+a1;}
for(i=1;i<=n;i++) {a2=c[i]; sum2=sum2+a2;}
cg=sum1/sum2;
printf("cg=%f",cg);
return 0;
}
4. cosx
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
int fact(int m);
int a,i,m;
float s=0,t,x;
printf("Enter the value of x in DEGREE:");
scanf("%f",&x);
x=(x*3.1416)/180;
for(i=1;;i++)
{
t=(pow(-1,i-1)*pow(x,2*i-2))/fact(2*i-2);
s=s+t;
if(t<.001)
break;
}
printf("\nThe value of cosx=%.2f",s);
getch();
}
int fact(int m)
{
int a;
if(m==0)return(1);
else
a=m*fact(m-1);
return(a);
}
5. e^x
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
int fact(int f);
float x,a,sum=1;int i;
printf("enter x:");
scanf("%f",&x);
for(i=1;;i++)
{
a=pow(x,i)/fact(i);
sum=sum+a;
if(a<.0001)
break;
}
printf("%.2f",sum);
getch();
}
int fact(int f)
{float j,sum1=1;
for(j=1;j<=f;j++){sum1=sum1*j;}
return(sum1);
}
6. fibonacci
#include<stdio.h>
#include<conio.h>
int main()
{
int i,n,f1=1,f2=1,fn;
printf("\nHow many numbers do you want to display:");
scanf("%d",&n);
if(n==1)
printf("%d",f1);
else if(n==2)
printf("%d,%d",f1,f2);
else
{
printf("\nThe fibonacci series is = %d,%d",f1,f2);
for(i=3;i<=n;i++)
{
fn=f1+f2;
f1=f2;
f2=fn;
printf(",%d",fn);
}
}
getch();
}
7. File open
#include<stdio.h>
#include<conio.h>
int main()
{
int a=10;int i;float sum,avg;sum=0;
FILE *f1;
f1=fopen("input.txt","r");
for(i=0;i<10;i++)
{
fscanf(f1,"%d",&a);/*FILE theke scan korbe*/
printf("%d\t",a);/*main ar output a print korbe*/
sum=sum+a;}
fclose(f1);
avg=sum/10;
printf("avg=%f",avg);
getch();
}
8. for piramid
#include<stdio.h>
void main()
{
int i,j,k,n;scanf("%d",&n);
for(i=1;i<=n;i++)
{for(j=1;j<=(n-i);j++){printf(" ");}
{for(k=1;k<=i;k++)
{
printf("*\a");
}
}
}return 0;}
9.function( have input no output)
#include<stdio.h>
#include<conio.h>
main()
{
int length;
printline(20);
printf("inside main function\n");
printline(10);
scanf("%d",&length);
printline(length);
getch();
}
void printline(int n)
{
int i;
for(i=0;i<=n;i++)
printf("%c",'-');
printf("\n");
}
10.grade else if
#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{int a,m;char *s;
printf("subject\t\t\t\tgrade/gpa\n");printf("*******************************************************************************\n");
for(a=0;a<=5;a++)
{printf("\nsubject:\n");scanf("%s",s);printf("enter marks for subject:");scanf("%d",&m);
if(80<=m&&m<100)printf("%s\t\t\t\t 4/A+",s);
else if(75<=m&&m<80)printf("%s\t\t\t 3.75/A",s);
else if(70<=m&&m<75)printf("%s\t\t\t\t 3.50/A-",s );
else if(65<=m&&m<70)printf("%s\t\t\t\t 3.25/B+",s);
else if(60<=m&&m<65)printf("%s\t\t\t\t 3/B",s);
else if(55<=m&&m<60)printf("%s\t\t\t\t 2.75/B-",s);
else if(50<=m&&m<55)printf("%s\t\t\t\t 2.50/C+",s);
else if(45<=m&&m<50)printf("%s\t\t\t\t 2.25/C",s);
else if(40<=m&&m<45)printf("%s\t\t\t\t 2.20/D",s);
else if(0<=m&&m<40)printf("%s\t\t\t\t 0.00/F",s);
else printf("invalid, inter marks from 0 to 100");}
return 0;
}
11.grade switch
#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{int a,m,i;char *s;
printf("subject\t\t\t\tgrade/gpa\n");printf("*******************************************************************************\n");
for(i=1;;i++){
printf("\nsubject:\n");scanf("%s",s);printf("enter marks for subject:");scanf("%d",&m);
switch(m/5)
{ /*if(80<=m&&m<100)*/case 20:printf("%s\t\t\t\t 4/A+",s);break;
case 19:printf("%s\t\t\t\t 4/A+",s);break;
case 18:printf("%s\t\t\t\t 4/A+",s);break;
case 17:printf("%s\t\t\t\t 4/A+",s);break;
case 16:printf("%s\t\t\t\t 4/A+",s);break;
case 6:printf("%s\t\t\t\t 0.00/F",s);break;
case 5:printf("%s\t\t\t\t 0.00/F",s);break;
case 4:printf("%s\t\t\t\t 0.00/F",s);break;
case 3:printf("%s\t\t\t\t 0.00/F",s);break;
case 2:printf("%s\t\t\t\t 0.00/F",s);break;
case 1:printf("%s\t\t\t\t 0.00/F",s);break;
case 0:printf("%s\t\t\t\t 0.00/F",s);break;
/*else */default: printf("invalid, inter marks from 0 to 100");}}
return 0;
}
No comments:
Post a Comment