National - I YEAR C New Program
National - I YEAR C New Program
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:
OUTPUT:
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:
#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
OUTPUT:
OUTPUT:
#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>
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);
Output
Queue Front : 40
Queue Rear : 50