Code for Fibonacci Series
Code for Fibonacci Series
#include<stdio.h>
#include<math.h>
int main()
{
int i,n,a,prime=1;
printf("enter the no. you want to check: ");
scanf("%d",&n);
for(i=2;i<=sqrt(n);i++)
{
if(n%i==0)
{
prime=0;
break;
}
}
if(prime)
{
printf("prime");
}
else{
printf("not prime");
}
}
2) Code for Fibonacci Series:-
#include <stdio.h>
int main()
{
long long int f,second,i,next,n;
printf("Enter how many Fibonacci numbers to generate: ");
scanf("%lld",&n);
printf("Enter the starting position:");
scanf("%lld",&f);
second=(f+1);
while(i<=n)
{
next=f+second;
printf("%lld\n",next);
f=second;
second=next;
i++ ;
}
}
3) Code for sum n intgers;
#include <stdio.h>
int main()
{
int n,sum;
printf("enter hum many no. of sum you want");
scanf("%d",&n);
sum=n*(n+1)/2;
printf("%d",sum);
}
4) Code for Quadratic Equation
#include <stdio.h>
#include <math.h>
int main()
{
float a,b,c,d,roota,rootb,real,imaginary;
printf("Enter the cofficients a,b,c");
scanf("%f %f %f",&a,&b,&c);
if(a==0)
{
printf("a can't be 0");
return 0;
}
d=b*b-4*a*c;
if(d>0)
{
roota= (-b+sqrt(d))/(2*a);
rootb= (-b-sqrt(d))/(2*a);
printf("roots are real and different %.2f and %.2f",roota,rootb);
}
else if(d==0)
{
roota=rootb= -b/(2*a);
printf("roots are same %.2f and %.2f",roota,rootb);
}
else
{
roota= -b/(2*a);
rootb= sqrt(-d)/(2*a);
printf("roots are real and imaginary %.2f and %.2f",roota,rootb);
}
}
5) Code For star pattern
#include <stdio.h>
int main()
{
int i,j,n;
printf("How many rows of start you want");
scanf("%d",&n);
for(i=1;i<=n;i++){
for(j=1;j<=i;j++)
{
printf("* ");
}
printf("\n");
}
return 0;
}
Output:-
n=3
6) Code to factorial of a number.
#include <stdio.h>
int main()
{
int i,x=1,n;
printf("The factorial you want ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
x=x*i;
}
printf("%d",x);
return 0;
}
7) Code for sum of three number using call by value
#include <stdio.h>
int sum(int,int,int);
int main()
{
int a,b,c,final_sum;
printf("enter three no.");
scanf("%d %d %d",&a,&b,&c);
final_sum= sum(a,b,c);
printf("sum is %d",final_sum);
}
int sum(int a,int b,int c)
{
return a+b+c;
}
8) Code to add three no. by call by refrence
#include <stdio.h>
int sum(int*,int*,int*);
int main()
{
int a,b,c,final_sum;
printf("enter three no.");
scanf("%d %d %d",&a,&b,&c);
final_sum= sum(&a,&b,&c);
printf("sum is %d",final_sum);
}
int sum(int *a,int *b,int *c)
{
return *a+*b+*c;
}