0% found this document useful (0 votes)
29 views13 pages

Generate N Terms of Fabonici Series by Using Array

The document contains C code snippets demonstrating various array operations including: generating Fibonacci series terms using an array, converting a decimal number to binary and storing in an array, finding maximum and minimum marks of students stored in an array, selection sort of an array, copying one array to another, reversing an array using a single loop, finding highest marks of students and displaying name, passing an array into a function, and sorting an array using pointers.

Uploaded by

Shobhit Goswami
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views13 pages

Generate N Terms of Fabonici Series by Using Array

The document contains C code snippets demonstrating various array operations including: generating Fibonacci series terms using an array, converting a decimal number to binary and storing in an array, finding maximum and minimum marks of students stored in an array, selection sort of an array, copying one array to another, reversing an array using a single loop, finding highest marks of students and displaying name, passing an array into a function, and sorting an array using pointers.

Uploaded by

Shobhit Goswami
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Generate n terms of fabonici series

by using array
#include<stdio.h>
#include<conio.h>
void main()
{
int a[100],n,i;
clrscr();
printf("input all no. of term ");
scanf("%d",&n);
a[0]=0; a[1]=1;
for(i=2;i<=n;i++)
a[i]=a[i-1]+a[i-2];
printf("term of fabonici series\n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
getch();
}

Input decimal no. convert & print its


binary equivalent
#include<stdio.h>
#include<conio.h>
void main()
{
int a[50],n,i,c;
clrscr();
printf("input a decimal no. ");
scanf("%d",&n);
i=0;c=0;
while(n>0)
{
a[i]=n%2; n/=2; c++; i++;
}
printf("binary no.\n");
for(i=c-1;i>=0;i--)
printf("%d",a[i]);
getch();
}

Student marks stor in array &find


max
& min. marks
#include<stdio.h>
#include<conio.h>
void main()
{
int m[100],n,i,l,s;
clrscr();
printf("input no. of student ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("input student mark ");
scanf("%d",&m[i]);
}
l=s=m[0];
for(i=1;i<n;i++)
{
if(l<m[i]) l=m[i];
if(s>m[i]) s=m[i];
}
printf("max=%d\nmin=%d",l,s);
getch();
}

Selection sort
#include<stdio.h>
#include<conio.h>
void main()
{
int i,a[100],j,n,t;
clrscr();
printf("total no. of element ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter the value ");
scanf("%d",&a[i]);
}
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
printf("\nsorted array");
for(i=0;i<n;i++)
{
printf("\n%4d",a[i]);
}
getch();
}

Copy one array to another

#include<stdio.h>
#include<conio.h>
void main()
{
int a[100],b[100],n,i;
clrscr();
printf("total no. of element ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter value arr a ");
scanf("%d",&a[i]);
b[i]=a[i];
}
printf("show arr b\n");
for(i=0;i<n;i++)
printf("%d\n",b[i]);
getch();
}

Reverse array use only single loop


#include<stdio.h>
#include<conio.h>
void main()
{
int a[100],b[100],n,i;
clrscr();
printf("total no. of element ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter value arr a ");
scanf("%d",&a[i]);
b[n-i-1]=a[i];
}
printf("show arr b\n");
for(i=0;i<n;i++)
printf("%d\n",b[i]);
getch();
}

Input name & marks obt. in test of n


students class & show name of student
who has highest marks
#include<stdio.h>
#include<conio.h>
void main()
{
char nam[100][20];
int n,m[100],i,j,max;
clrscr();
printf("enter no. of student in class ");
scanf("%d",&n);
for(i=0;i<n;++i)
{
printf("enter student name & marks\n");
scanf("%s%d",nam[i],&m[i]);
}
max=m[0];
for(i=1;i<n;++i)
{
if(m[i]>max)
{
max=m[i]; j=i;
}
}
printf("\nstudent who gets max. marks\n");
printf("name=%s\nmark=%d",nam[j],m[j]);
getch();
}

Passing array into function

#include<stdio.h>
#include<conio.h>
void main()
{
int m[100],n,i,l;
int lar(int[100],int);
clrscr();
printf("input total no of student: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("input student marks: ");
scanf("%d",&m[i]);
}
l=lar(m,n); /* can pass matrix*/
printf("largest marks=%d",l);
getch();
}
int lar(int m[100],int n)
{
int i,d;
d=m[0];
for(i=1;i<n;i++)
if(m[i]>d)
d=m[i];
return d; /*array can't return*/
}

sorting by pointer
#include<stdio.h>
#include<conio.h>
void main()
{
int a[100],*p[100],i,j,n,*t;
clrscr();

printf("input total no of student: ");


scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("input element: ");
scanf("%d",&a[i]);
p[i]=&a[i];
}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(*p[i]>*p[j])
{
t=p[i];
p[i]=p[j];
p[j]=t;
}
}
}
printf("original array:\n");
for(i=0;i<n;i++)
printf("%4d",a[i]);
printf("\nsorted array:\n");
for(i=0;i<n;i++)
printf("%4d",*p[i]);
getch();
}

You might also like