You are on page 1of 11

Day 4-Session 1

Finding factors of a number:

#include <stdio.h>
int main()
{
int number, i; ​//declaraton

printf("Enter a positive integer: ");


scanf("%d",&number); ​ //getting the number

printf("Factors of %d are: ", number);


for(i=1; i <= number; ++i) ​//loop condiotion for finding factor
{
if (number%i == 0) ​//check if the number is divisible
{
printf("%d ",i);
}
}

return 0;
}

Primality check:

#include <stdio.h>
int main()
{
int n, i, flag = 0;

printf("Enter a positive integer: ");


scanf("%d", &n);

for(i = 2; i <= n/2; ++i) // condition for non prime number


{
if(n%i == 0)
{
flag = 1;
break;
}
}

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 () {

int var = 20; ​ // actual variable declaration


int *ip; ​//pointer variable declaration

ip = &var; ​// store address of var in pointer variable

printf("Address of var variable: %x\n", &var );

​ //address stored in pointer variable


printf("Address stored in ip variable: %x\n", ip );

​ /access the value using the pointer


/
printf("Value of *ip variable: %d\n", *ip );
return 0;
}
Product of two numbers:

#include <stdio.h>

int main()

int first, second, *p, *q, sum;

printf("Enter two integers to multiply​\n​");

scanf("%d%d", &first, &second);

p = &first; ​//Pointer assignment

q = &second; ​//Pointer assignment

Product = (*p) * (*q);

printf("product of the numbers = %d​\n​", product);

return 0;

Sum and difference of two numbers:

#include <stdio.h>

int main()

int first, second, *p, *q, sum, diff;

printf("Enter two integers(larger number first)​\n​");

scanf("%d%d", &first, &second);

p = &first; ​//Pointer assignment


q = &second;

sum = *p + *q; ​//sum of number

diff= *p - *q; ​//difffencce of numbers

printf("Sum and difference of the numbers = %d and %d​\n​", sum,


diff);

return 0;

Session 3
Distance between two points:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

struct Point { ​//Declaring the structure


int x, y;
};

double getDistance(struct Point a, struct Point b) ​//function


prototype
{
double distance;
distance = sqrt((a.x - b.x) * (a.x - b.x) + (a.y-b.y)
*(a.y-b.y)); ​//Formula for calculating the distance between 2 points
return distance;
}

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;
}

Top scoring student:

#include <stdio.h>
struct student
{
char name[50];
float marks;
} s[10];

int main()
{
int i;

printf("Enter information of students:\n");

​ // storing information
for(i=1; i<10; ++i)
{
printf("Enter name: ");
scanf("%s",s[i].name);

printf("Enter marks: ");


scanf("%f",&s[i].marks);

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;
}

Sorting people based on their age(using structure):

#include<stdio.h>
#include<conio.h>

struct people {
char name[20];
int age;
} person[10], temp;

void main() {
int i, j, n=10;
clrscr();

for (i = 0; i < 10; i++) {


printf("\nEnter Person Name : ");
scanf("%s", &person[i].name);
printf("\nEnter age : ");
scanf("%d", &person[i].age);
printf("\n");
}

for (i = 1; i < n; i++)


for (j = 0; j < n - i; j++) {
if (strcmp(person[j].age, person[j + 1].age) > 0) {
temp = person[j];
person[j] = person[j + 1];
person[j + 1] = temp;
}
}

for (i = 0; i < n; i++) {


printf("\n%s\t%d",person[i].name,person[i].age);
}
getch();
}
Session 4

Sorting numbers using their factor count:

#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]);

Sorting even numbers and odd numbers:

#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);

printf("Array after Sorting\n");


for(i=0;i<size;i++)
{
printf("%d ",a[i]);
}
printf("\n");
}

Stack implementation using array:

#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");
}

You might also like