C Program
#include<stdio.h>
void main()
{
int n;
printf("Enter an integer\n");
scanf("%d",&n);
if ( n%2 == 0 )
printf("Even");
else
printf("Odd");
}
#include<stdio.h>
void main()
{
int n;
printf("Enter an integer\n");
scanf("%d",&n);
if ( n%2 == 0 )
printf("Even");
else
printf("Odd");
}
![]() |
| A 4x4 Spiral matrix |
#include<stdio.h>
void main()
{
int a[10][10],o,cellcount,cc=0,c=0,i=0,j=0,g=1;
printf("\nEnter the order of spiral matrix: ");
scanf("%d",&o);
cellcount=o;
printf("\nEnter the elements:\n");
while(c<o*o)
{
cc=0;
for(;cc<cellcount;j=j+g,cc++)
scanf("%d",&a[i][j]);
j=j-g;
c+=cc;
cc=0;
i=i+g;
cellcount--;
if(c>=o*o)
break;
for(;cc<cellcount;i=i+g,cc++)
scanf("%d",&a[i][j]);
c+=cc;
i=i-g;
j=j-g;
g*=-1;
}
printf("\nThe spiral matrix is:");
for(i=0;i<o;i++)
{
printf("\n");
for(j=0;j<o;j++)
printf("%d\t",a[i][j]);
}
}
The above program reads the matrix in spiral fashion and the displays the matrix in normal way. You can use this code not only to read and store the elements in matrix spirally but also to access the elements in the matrix in spiral order.
#include<stdio.h>
#include<dirent.h>
main()
{
char dirname[10];
DIR*p;
struct dirent *d;
printf("Enter directory name\n");
scanf("%s",dirname);
p=opendir(dirname);
if(p==NULL)
{
perror("Cannot find directory");
exit(-1);
}
while(d=readdir(p))
printf("%s\n",d->d_name);
}
#include<stdio.h>
#include<string.h>
void main()
{
char fn[10],pat[10],temp[200];
FILE *fp;
printf("Enter file name\n");
scanf("%s",fn);
printf("Enter pattern to be searched\n");
scanf("%s",pat);
fp=fopen(fn,"r");
while(!feof(fp))
{
fgets(temp,1000,fp);
if(strstr(temp,pat))
printf("%s",temp);
}
fclose(fp);
}
![]() |
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
struct node
{
int info;
struct node *link;
}*last;
void create_list(int num);
void addatbeg(int num);
void addafter(int num,int pos);
void del(int num);
void display();
void main()
{
int choice,n,m,po,i;
last=NULL;
while(1)
{
printf("1.Create List\n");
printf("2.Add at begining\n");
printf("3.Add after \n");
printf("4.Delete\n");
printf("5.Display\n");
printf("6.Quit\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("How many nodes you want : ");
scanf("%d",&n);
for(i=0; i < n;i++)
{
printf("Enter the element : ");
scanf("%d",&m);
create_list(m);
}
break;
case 2: printf("Enter the element : ");
scanf("%d",&m);
addatbeg(m);
break;
case 3: printf("Enter the element : ");
scanf("%d",&m);
printf("nter the position after which this element is inserted: ");
scanf("%d",&po);
addafter(m,po);
break;
case 4: if(last == NULL)
{
printf("List underflow\n");
continue;
}
printf("Enter the number for deletion : ");
scanf("%d",&m);
del(m);
break;
case 5: display();
break;
case 6: exit(0);
default:printf("Wrong choice\n");
}
}
}
void create_list(int num)
{
struct node *temp;
temp=(node*)malloc(sizeof(struct node));
temp->info = num;
if(last == NULL)
{
last = temp;
temp->link = last;
}
else
{
temp->link = last->link;
last->link = temp;
last = temp;
}
}
void addatbeg(int num)
{
struct node *temp;
temp =(node*)malloc(sizeof(struct node));
temp->info = num;
temp->link = last->link;
last->link = temp;
}
void addafter(int num,int pos)
{
struct node
*temp,*q;
int i;
q = last->link;
for(i=0;i<pos-1;i++)
{
q = q->link;
if(q==last->link)
{
printf("There are less than %d elements\n",pos);
return;
}
}
temp =(node*)malloc(sizeof(struct node) );
temp->link = q->link;
temp->info = num;
q->link = temp;
if(q==last) //Element inserted at the end
last=temp;
}//End of addafter()
void del(int num)
{
struct node *temp,*q;
if( last->link == last && last->info == num) //Only one element
{
temp=last;
last = NULL;
free(temp);
return;
}
q = last->link;
if(q->info == num)
{
temp = q;
last->link = q->link;
free(temp);
return;
}
while(q->link !=last)
{
if(q->link->info==num) //Element deleted in between
{
temp = q->link;
q->link = temp->link;
free(temp);
printf("%d deleted\n",num);
return;
}
q = q->link;
}
if(q->link->info==num) //Last element deleted q->link=last
{
temp = q->link;
q->link = last->link;
free(temp);
last = q;
return;
}
printf("Element %d not found\n",num);
}
void display()
{
struct node *q;
if(last == NULL)
{
printf("List is empty\n");
return;
}
q = last->link;
printf("List is :\n");
while(q != last)
{
printf("%d ", q->info);
q = q->link;
}
printf("%d\n",last->info);
}
# include <stdio.h>
# include <conio.h>
# include <math.h>
void main ()
{
int a,b=0,sum=0;
long int n;
printf("Enter the number to check\n ");
scanf("%i",&n);
if(n<1)
{
printf ("\nThe number should be greater than 0");
}
else
{
a=n;
//counting the digits
while (a>0)
{
a=a/10;
b++;
}
a=n;
//adding up bth power of digits
while(a>0)
{
sum=sum+pow(( a%10) ,b);
a=a/10;
}
if(sum==n)
printf ("\nThe number is an ARMSTRONG number");
else
printf ("\nThe number is NOT an ARMSTRONG number");
}
getch();
}
#include<stdio.h>
main()
{
int a[7][7],i,j,k,m,n,temp;
//reading
printf("enter number of rows of matrix\n");
scanf("%i",&m);
printf("enter number of columns of matrix\n");
scanf("%i",&n);
printf("enter the elements\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
}
//displaying
printf("the matrix you entered:\n");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
{
printf("%4d",*(*(a+i)+j));
}
}
//sorting
for(i=0;i<n;i++)
{
for(k=0;k<m-1;k++)
{
for(j=0;j<m-k-1;j++)
{
//comparison
if( *(*(a+j)+i)> *(*(a+(j+1))+i))
{
temp=*(*(a+j)+i);
*(*(a+j)+i)=*(*(a+(j+1))+i);
*(*(a+(j+1))+i)=temp;
}
}
}
}
//displaying
printf("\nSorted Matrix:\n");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
{
printf("%4d",*(*(a+i)+j));
}
}
}
#include<stdio.h>
void main() { int a[10][10],i,m,j; int det=0; printf("\nEnter the order of matrix:\n "); scanf("%i",&m); printf("\nenter elements\n"); for(i=0;i<m;i++) { for(j=0;j<m;j++) scanf("%d",&a[i][j]); } printf("\nThe matrix is\n"); for(i=0;i<m;i++) { printf("\n"); for(j=0;j<m;j++) printf("%3d",a[i][j]); } printf("\nthe transpose is\n"); for(i=0;i<m;i++) { for(j=0;j<m;j++) printf("%3i",a[j][i]); printf("\n"); } if(m==1) { det=a[0][0]; printf("det=%i",det); } else if(m==2) { int k=1,l=1; for(i=0;i<2;i++) for(j=0;j<2;j++) { if(i==j) k*=a[i][j]; else l*=a[i][j]; } printf("\ndeterminant=%i",k-l); } else if(m==3) { for(i=0;i<3;i++) det=det+(a[0][i]*(a[1][(i+1)%3]*a[2][(i+2)%3]-a[1][(i+2)%3]*a[2][(i+1)%3])); printf("\nDeterminant of matrix is: %d",det); } else printf("This program can't calculate determinant of matrix of order greater than 3"); }