Professional Documents
Culture Documents
Day 4
Day 4
#include <stdio.h>
int main()
{
int number, i; //declaraton
return 0;
}
Primality check:
#include <stdio.h>
int main()
{
int n, i, flag = 0;
if (n == 1)
{
printf("1 is neither a prime nor a composite number.");
}
else
{
if (flag == 0)
printf("%d is a prime number.", n);
else
printf("%d is not a prime number.", n);
}
return 0;
}
Day 4-session 2
Pointer declaration:
#include <stdio.h>
int main () {
#include <stdio.h>
int main()
return 0;
#include <stdio.h>
int main()
return 0;
Session 3
Distance between two points:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
struct Point a, b; //structure initialization
printf("Enter coordinate of point a: ");
scanf("%d %d", &a.x, &a.y);
printf("Enter coordinate of point b: ");
scanf("%d %d", &b.x, &b.y);
printf("Distance between a and b: %lf\n", getDistance(a, b));
//function call
return 0;
}
#include <stdio.h>
struct student
{
char name[50];
float marks;
} s[10];
int main()
{
int i;
// storing information
for(i=1; i<10; ++i)
{
printf("Enter name: ");
scanf("%s",s[i].name);
printf("\n");
}
printf("Top scorer:\n\n");
// displaying information
for(i=0; i<10; ++i)
{
if(s.[i]>s.[i-1)
{
top=i; //finding the student with best score
}
puts(s[top].name);
return 0;
}
#include<stdio.h>
#include<conio.h>
struct people {
char name[20];
int age;
} person[10], temp;
void main() {
int i, j, n=10;
clrscr();
#include<stdio.h>
int main()
{
int n,i,j,c=0,t,t1;
scanf("%d",&n);
int a[n],b[n];
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
for(j=1;j<=a[i];j++)
{
if(a[i]%j==0)
c++;
}
b[i]=c;
c=0;
}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(b[i]>b[j])
{
t1=b[i];
b[i]=b[j];
b[j]=t1;
t=a[i];
a[i]=a[j];
a[j]=t;
}
else if(b[i]==b[j])
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
}
for(i=0;i<n;i++)
printf("%d ",a[i]);
#include<stdio.h>
main()
{
int i,flag,temp,size;
printf("Enter number of elements in array\n");
scanf("%d",&size);
int a[size];
printf("Enter Elements of Array for Odd Even Sort\n");
for(i=0;i<size;i++)
{
scanf("%d",&a[i]);
}
do{
flag=0;
//For Even Sort
for(i=0;i<size-1;i+=2)
{
//Swapping
if(a[i]>a[i+1])
{
temp=a[i];
a[i]=a[i+1];
a[i+1]=temp;
flag=1;
}
}
//For Odd Sort
for(i=1;i<size-1;i+=2)
{
//Swapping
if(a[i]>a[i+1])
{
temp=a[i];
a[i]=a[i+1];
a[i+1]=temp;
flag=1;
}
}
}while(flag);
#include<stdio.h>
int stack[100],choice,n,top,x,i;
void push(void);
void pop(void);
void display(void);
int main()
{
clrscr();
top=-1;
printf("\n Enter the size of STACK[MAX=100]:");
scanf("%d",&n);
printf("\n\t STACK OPERATIONS USING ARRAY");
printf("\n\t--------------------------------");
printf("\n\t 1.PUSH\n\t 2.POP\n\t 3.DISPLAY\n\t 4.EXIT");
do
{
printf("\n Enter the Choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
display();
break;
}
case 4:
{
printf("\n\t EXIT POINT ");
break;
}
default:
{
printf ("\n\t Please Enter a Valid Choice(1/2/3/4)");
}
}
}
while(choice!=4);
return 0;
}
void push() //stack push
{
if(top>=n-1) //stack overflow condition
{
printf("\n\tSTACK is over flow");
}
else
{
printf(" Enter a value to be pushed:");
scanf("%d",&x);
top++;
stack[top]=x;
}
}
void pop() //stack pop
{
if(top<=-1) //stack underflow condition
{
printf("\n\t Stack is under flow");
}
else
{
printf("\n\t The popped elements is %d",stack[top]);
top--;
}
}
void display()
{
if(top>=0)
{
printf("\n The elements in STACK \n");
for(i=top; i>=0; i--)
printf("\n%d",stack[i]);
printf("\n Press Next Choice");
}
else
{
printf("\n The STACK is empty");
}