0% found this document useful (0 votes)
53 views28 pages

National - I YEAR C New Program

The document contains C code examples for implementing various data structures and algorithms. The examples include: 1. Converting between Celsius and Fahrenheit temperatures. 2. Checking if a number is even or odd. 3. Finding the greatest of three numbers. 4. Displaying the day of the week for a given number. 5. Calculating the sum of natural numbers. 6. Multiplying two matrices. 7. Finding the maximum element in an array. 8. Reversing a number. 9. Adding two numbers using pointers. 10. Solving a quadratic equation. 11. Calculating the factorial of a number. 12. Demonstrating

Uploaded by

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

National - I YEAR C New Program

The document contains C code examples for implementing various data structures and algorithms. The examples include: 1. Converting between Celsius and Fahrenheit temperatures. 2. Checking if a number is even or odd. 3. Finding the greatest of three numbers. 4. Displaying the day of the week for a given number. 5. Calculating the sum of natural numbers. 6. Multiplying two matrices. 7. Finding the maximum element in an array. 8. Reversing a number. 9. Adding two numbers using pointers. 10. Solving a quadratic equation. 11. Calculating the factorial of a number. 12. Demonstrating

Uploaded by

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

1.

1 Centigrade to Fahrenheit
#include<stdio.h>
#include<conio.h>
void main()
{
int celsius;
float fahrenheit;
clrscr();
printf("\n\tEnter Temperature in Celsius:");
scanf("%d",&celsius);
fahrenheit=(1.8 * celsius)+32;
printf("\n\t Temperature in Fahrenheit:%f",fahrenheit);
getch();
}

Output
Enter Temperature in Celsius: 32
Temperature in Fahrenheit: 89.599998
1.2 Even or Odd.
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
printf("\n\tEnter any no:");
scanf("%d",&n);
if(n%2==0)
printf("\n\t%d NUMBER IS EVEN");
else
printf("\n\t%d NUMBER IS ODD");
getch();
}
Output
Enter any no: 23

23 NUMBER IS ODD
1.3 Greatest of Three numbers
#include<stdio.h>
#include<conio.h>
void main()
{
int n1,n2,n3;
clrscr();
printf("\n\tEnter Three Numbers:");
scanf("%d%d%d",&n1,&n2,&n3);
if(n1>=n2 && n1>=n3)
printf("\n\tThe Largest no:%d",n1);
else if(n2>=n1 && n2>=n3)
printf("\n\tThe Largest no:%d",n2);
else
printf("\n\tThe Largest no:%d",n3);
getch();
}
OUTPUT:

Enter Three Numbers:

The Largest no: 67


2. Display Monday to Sunday
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
printf("\tEnter Any Number Between 1 and 7:");
scanf("%d",&n);
switch(n)
{
case 1:
printf("\n\tMONDAY");
break;
case 2:
printf("\n\tTUESDAY");
break;
case 3:
printf("\n\tWEDNESDAY ");
break;
case 4:
printf("\n\tTHRUSDAY");
break;
case 5:
printf("\n\tFRIDAY");
break;
case 6:
printf("\n\tSATURDAY");
break;
case 7:
printf("\n\tSUNDAY");
break;
dafault:
printf("You Have Entered a Wrong Choice");
}
getch();
}

OUTPUT:

Enter Any Number between 1 and 7: 5

FRIDAY
3. Natural Numbers
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,sum=0;
clrscr();
printf("\n\tEnter The Value of n:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("\n\t%d",i);
sum+=i;
}
printf("\n\n\tSum of The First n Natural Number is:%d",sum);
getch();
}
OUTPUT:
Enter The Value of n:7
1
2
3
4
5
6
7
Sum of the First n Natural Number is: 28
4, Multiplication of Two Matrices
#include<stdio.h>
#include<conio.h>
void main()
{
int MatrixA[9][9],MatrixB[9][9],Matrixproduct[9][9];
int n,i,j,k;
int Row1,Row2,Column1,Column2;
clrscr();
printf("\n\t Enter the order of Matrix A\n");
scanf("%d%d",&Row1,&Column1);
printf("\n\t Enter the order of Matrix B\n");
scanf("%d%d",&Row2,&Column2);
if(Column1==Row2)
{
printf("\n\t Enter the elements of Matrix A\n");
for(i=0;i<Row1;i++)
{
for(j=0;j<Column1;j++)
{
scanf("%d",&MatrixA[i][j]);
}
}
printf("\n\t Enter the element of Matrix B\n");
for(i=0;i<Row2;i++)
{
for(j=0;j<Column2;j++)
{
scanf("%d",&MatrixB[i][j]);
}
}
for(i=0;i<Row1;i++)
{
for(j=0;j<Column2;j++)
{
Matrixproduct[i][j]=0;
for(k=0;k<Row2;k++)
{
Matrixproduct[i][j]=Matrixproduct[i][j]+(MatrixA[i][k]*MatrixB[k][j]);
}
}
}
printf("\n\t Product Matrix\n");
for(i=0;i<Row1;i++)
{
for(j=0;j<Column2;j++)
{
printf("%d\t",Matrixproduct[i][j]);
}
printf("\n");
}
}
else
{
printf("Invalid order so Multiplication not possible\n");
}
getch();
}
OUTPUT:

Enter the order of Matrix A


22
22
Enter the order of Matrix B
22
22
Enter the elements of Matrix A
1
2
3
4
Enter the elements of Matrix B
5
6
7
8
Product Matrix
19 22
43 50
5.1 Maximum number in Array.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],n,i,max;
int *p;
clrscr();
printf("Enter the size of array:");
scanf("%d",&n);
printf("Enter %d elements in The Array:\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("%5d",a[i]);
p=&a[0];
max=a[0];
for(i=0;i<n;i++)
{
if(max<=*p)
max=*p;
p++;
}
printf("\n Maximum Element in The Array is :%d",max);
getch();
}
Output

Enter the size of array:


Enter 3 elements in the Array:
1
2
3
64
Maximum Element in the Array is: 3
5.2. REVERSE OF A NUMBER
#include<stdio.h>
#include<conio.h>
void main()
{
int *n,a,r=0;
clrscr();
printf("\n\t Enter Any Number to Get its Reverse:");
scanf("%d",&*n);
while(*n>=1)
{
a=*n%10;
r=r*10+a;
*n=*n/10;
}
printf("\n\n\t The Reveres Number is=%d",r);
getch();
}

OUTPUT:

Enter Any Number to Get its Reverse: 1 2 3 4

The Reveres Number is=4321


5.3 ADD TWO NUMBERS
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,*p,*q,sum;
clrscr();
printf("\n\tEnter Two Integer Number:");
scanf("%d%d",&a,&b);
p=&a;
q=&b;
sum=*p+*q;
printf("\n\tSum of Entered Numbers=%d",sum);
getch();
}

OUTPUT:

Enter Two Integer Number: 1020

Sum of Entered Numbers=30


6.QUADRATIC EQUATION.

#include<stdio.h>
#include<conio.>
void main()
{
float a,b,c,d;
double r1,r2,r3,r4,r5,r6;
clrscr();
printf(“enter the value of a,b,c,\n”);
scanf(“%f%f%f,&a,&b,&c);
printf(“\t\t\t QUADRATIC EQUATION\n”);
printf(“\t\t\t*****************\n”);
printf(“\n\t\t give values are \n\n”);
printf(“\t\t\t A=%f \n\t\t\t B=%f\n\t\t\t C=%f\n\n”,a,b,c);
d=b*b-4*a*c;
if(d==0)
{
printf(“\n\t\t roots are real and equal\n”);
r1=-b/(2*a);
r2=r1;
printf(“\n\t\t root1=%f\n\t\t root2=%f\n”,r1,r2);
}
else if(d>0)
{
printf(“\n\t\t roots are real and unequal\n”);
r3=(-b+sqrt(d))/(2*a);
r4=(-b-sqrt(d))/(2*a);
printf(“\n\t\t root1=%f \n\t\t root2=%f\n”,r3,r4);
}
else if(d<0)
{
printf(“\n\t\t roots are imaginary\n\n”);
r5=-b/(2*a);
r6=sqrt(abs(d))/(2*a);
printf(“\n\t\t root6=%f+i%f\n”,r5,r6);
printf(“\n\t\t root6=%f-i%f”,r5,r6);
}
getch();
}

OUTPUT:
Enter the value of a,b,c
121
QUADRATIC EQUATION
Given values are
A=1.000000
B=2.000000
C=3.000000
Roots are real and equal
root1=-1.000000
root2=-1.000000
7. FACTORIAL OF A NUMBER.
#include<stdio.h>
#include<conio.h>
int fact(int);
void main()
{
int num,f;
clrscr();
printf("\n\tEnter a Number:");
scanf("%d",&num);
f=fact(num);
printf("\n\tFactorial of %d is=%d",num,f);
getch();
}
int fact(int n)
{
if(n==1)
return(1);
else
return(n*fact(n-1));
}

OUTPUT:
Enter a Number:6
Factorial of 6 is=720
8.1 Call By Value

#include<stdio.h>
#include<conio.h>
void interchange(int number1,int number2)
{
int temp;
clrscr();
temp=number1;
number1=number2;
number2=temp;
}
void main()
{
int num1=80,num2=78;
interchange(num1,num2);
printf("\n\tNumber1=%d",num1);
printf("\n\tNumber2=%d",num2);
getch();
}

Output

Number1=80

Number1=78
8.2 Call By Reference

#include<stdio.h>
#include<conio.h>
void interchange(int *num1,int *num2)
{
int temp;
clrscr();
temp=*num1;
*num1=*num2;
*num2=temp;
}
void main()
{
int num1=50,num2=70;
interchange(&num1,&num2);
printf("\n Number1:%d",num1);
printf("\n Number2:%d",num2);
getch();
}

Output

Number1=70
Number1=50
9.1 Student Details
-----------------------
13 (a). Write operations:
#include<stdio.h>
#include<conio.h>
void main()
{
int rno,i,n;
char sname [20],dept[10];
FILE *fp;
fp=fopen("STUDENT.DAT","w");
clrscr();
printf("How many students?");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n Student rollno, name, department?");
scanf('%d%s%s",&rno,&sname,&dept);
fprintf(fp,"%d%s%s",rno,sname,dept);
printf("\n");
}fclose(fp);
}
OUTPUT :
How many students? 3
Student rollno, name, department? 1001 ramya cs
Student rollno, name, department? 1002 savitha bca
Student rollno, name, department? 1003 preethi it
9.2 Read operations :
#include<stdio.h>
#include<conio.h>
void main()
{
int rno;
cahr sname[20].dept[10];
FILE *fptr;
fptr=fopen("STUDENT.DAT","r");
clrscr();
printf("---------------------/n");
printf("\nRoll no./t Name/t department");
printf("\n------------------/n");
while(!feof(fptr))
{
fscanf(fptr,"%d%s%d",&rno,&sname,&dept);
printf("\n");
printf("n%6d/t%-20s%6d/n",rno,sname,dept);
}
printf("\n---------------------/n");
fclose(fptr);
getch();
}
OUTPUT :
Roll no ` Name Department
1001 RAMYA CS
1002 SAVITHA BCA
1003 PREETHI IT
10. C Program to Implement a Stack using Singly Linked List,
Implement Queue using linked list
#include <stdio.h>
#include <stdlib.h>  
struct node {
    int data;
    struct node *next;
}*top; 
/*
Initialize an empty stack
*/
void initialize() {
    top = NULL;

/*
Checks if Stack is empty or not
*/
int isEmpty() {
    if (top == NULL)
        return 1;
    else
        return 0;

/*
Returns the top element of Stack
*/
int peek() {
    return top->data;

/* Count stack elements */
int getStackSize(struct node *head){
    /* Input Validation */
    if (head == NULL) {
       printf("Error : Invalid stack pointer !!!\n");      
       return; 
    }      
    int length = 0;
    while(head != NULL){
        head = head->next;
        length++;
    }
    return length;

/*
Push an Element in Stack
*/
void push(int num) {
    struct node *temp;
    temp =(struct node *)malloc(1*sizeof(struct node));
    temp->data = num;     
    if (top == NULL) {
        top = temp;
        top->next = NULL;
    } else {
        temp->next = top;
        top = temp;
    }

/*
Pop Operation: Removes Top Element of the Stack
*/
void pop() {
    struct node *temp;
    if (isEmpty(top)) {
        printf("\nStack is Empty\n");
        return;
    } else {
        temp = top;
        top = top->next;
        printf("Removed  Element : %d\n", temp->data);  
        free(temp);
    }
}
 
/*
 Prints the linked list representation of a stack 
*/
void printStack(struct node *nodePtr) {
  while (nodePtr != NULL) {
     printf("%d", nodePtr->data);
     nodePtr = nodePtr->next;
     if(nodePtr != NULL)
         printf("-->");
  }
  printf("\n");

void main() {
   /* Initialize Stack */
   initialize();
   /* Push Elements in stack */
   push(1);
   push(2);
   push(3);
   push(4);
   /* Prints Size of Stack */
   printf("Stack Size : %d\n", getStackSize(top));
   /* Printing top element of Stack */
   printf("\nTop Element : %d\n", peek());
   /* Printing Stack */
   printf("Stack as linked List\n");
   printStack(top);
   /* Removing elements from stack */
   pop();
   pop();
   pop();
   pop();
   pop();
   printStack(top);
    
   return;
}

Output

Stack Size : 4

Top Element : 4
Stack as linked List
4-->3-->2-->1
Removed Element : 4
Removed Element : 3
Removed Element : 2
Removed Element : 1

Stack is Empty
#include <bits/stdc++.h>

using namespace std;

struct QNode {
int data;
QNode* next;
QNode(int d)
{
data = d;
next = NULL;
}
};
struct Queue {
QNode *front, *rear;
Queue() { front = rear = NULL; }

void enQueue(int x)
{
// Create a new LL node
QNode* temp = new QNode(x);

// If queue is empty, then


// new node is front and rear both
if (rear == NULL) {
front = rear = temp;
return;
}
// Add the new node at
// the end of queue and change rear
rear->next = temp;
rear = temp;
}
// Function to remove
// a key from given queue q
void deQueue()
{
// If queue is empty, return NULL.
if (front == NULL)
return;

// Store previous front and


// move front one node ahead
QNode* temp = front;
front = front->next;

// If front becomes NULL, then


// change rear also as NULL
if (front == NULL)
rear = NULL;
delete (temp);
}
};
// Driven Program
int main()
{
Queue q;
q.enQueue(10);
q.enQueue(20);
q.deQueue();
q.deQueue();
q.enQueue(30);
q.enQueue(40);
q.enQueue(50);
q.deQueue();
cout << "Queue Front : " << (q.front)->data << endl;
cout << "Queue Rear : " << (q.rear)->data;
}

Output
Queue Front : 40
Queue Rear : 50

You might also like