Write a ‘C’ Program to accept ‘n’ numbers and store all prime numbers in an array and display this array


#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],n,i,j,sum=0,prime[10],t=0;
clrscr();
printf("Enter the value of n=");
scanf("%d",&n);
printf("Enter the array=\n");
for(i=0;i<n;i++)
{
printf("Enter number=%d",i);
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
sum=0;
for(j=2;j<a[i];j++)
{
if(a[i]%j==0)
{
sum=1;
break;
}
}
if(sum==0)
{
prime[t]=a[i];
t++;
}
}
printf("prime number is=");
for(i=0;i<t;i++)
{
printf("%d",prime[i]);
printf("\n");
}
getch();
}
/*out put:-
Enter the value of n=10
Enter the array=
Enter number=10
Enter number=13
Enter number=25
Enter number=27
Enter number=73
Enter number=44
Enter number=53
Enter number=55
Enter number=12
Enter number=20

prime number is=13
73
53
*/

Write a C program to calculate the x to the power y without using standard function.


#include<stdio.h>
#include<conio.h>
void main()
{
int i,x,y, ans;
clrscr();
ans=1;
printf("Enter the value of x");
scanf("%d", &x);
printf("Enter the value of y");
scanf("%d", &y);
for(i=1; i<=y; i++)
{
ans= ans*x;
}
printf(" %d to the power %d is %d", x, y, ans);
getch();
}
/* output
Enter the value of x2
Enter the value of y3
 2 to the power 3 is 8 */

Write a 'C' program to store the records of student (stud_no,stud_name,stud_addr,stud_percentage )in a file using structure.



#include<stdio.h>
#include<conio.h>
void main()
{
  struct stud
{
int sno;
float per;
char name[20],add[20];
}s;
  int i,n;
  char ch;
  FILE *fp;
  fp=fopen("a.txt","w");
  if(fp==NULL)
  {
  printf("\nUnable to open file!");
  }
  else
   {
    printf("\nEnter the stud no:->");
    scanf("%d",&s.sno);
    printf("\nEnter the stud name:->");
    scanf("%s",s.name);
    printf("\nEnter the stud add:->");
    scanf("%s",s.add);
    printf("\nEnter the stud per:->");
    scanf("%f",&s.per);
    fprintf(fp,"%d\n%s\n%s\n%f",s.sno,s.name,s.add,s.per);
            }
   
  fclose(fp);
  getch();
}
/* output

Enter the stud no:->1
Enter the stud name:->amol
Enter the stud add:->baramati
Enter the stud per:->80*/

Write a ‘C’ Program to sort elements of an array in ascending order using dynamic memory allocation


#include<stdio.h>
#include<conio.h>
#include<alloc.h>
void main()
 {
 int a[5];
 int i,j,n,temp;
 clrscr();
 printf("\n enter the limit of array");
 scanf("\n %d",&n);
a[i]=(int) calloc(n,sizeof(int));
 printf("\n enter the array element");
 for(i=0;i<n;i++)
  {
  scanf("\n %d",&a[i]);
  }
  for(i=0;i<n;i++)
   {
   for(j=i+1;j<n;j++)
            {
            if(a[i]>a[j]){
             temp=a[i];
             a[i]=a[j];
             a[j]=temp;
             }}}
   printf("\n the asending elements are");
   for(i=0;i<n;i++)
            {
            printf("\n %d",a[i]);}
   getch();
  }
/* output

 enter the limit of array5

 enter the array element 56  8 34 1 78
 the asending elements are
 1
 8
 34
 56
 78
*/