100% found this document useful (2 votes)
3K views36 pages

C Program Practical File

It's a practical file meant to submitted for grading purposes. There are many C programs available in it.

Uploaded by

Sukhchain Singh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
100% found this document useful (2 votes)
3K views36 pages

C Program Practical File

It's a practical file meant to submitted for grading purposes. There are many C programs available in it.

Uploaded by

Sukhchain Singh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 36

Program Name:

To swap the values between two variables.


Program Code:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("enter the value of a");
scanf("%d",&a);
printf("enter the value of b");
scanf("%d",&b);
a=a+b;
b=a-b;
a=a-b;
printf("\nafter swapping a=%d",a);
printf("\nafter swapping b=%d",b);
getch();
}
Output:
Program Name:
To calculate simple interest from user input
principle amount, periods in number of year and
rate of interest.
Program Code:
#include<stdio.h>
#include<conio.h>
void main()
{
int years;
float amount, rate, interest;
clrscr();
printf("Enter Principle Amount:");
scanf("%f",&amount);
printf("Enter Rate of Interest:");
scanf("%f",&rate);
printf("Enter Period in Years:");
scanf("%d",&years);
interest=(amount*rate*years)/100;
printf("\nSimple Interest=%f",interest);
getch();
}
Output:
Program Name:
To read Celsius from user input and convert it into
Fahrenheit.
Program Code:
#include<stdio.h>
#include<conio.h>
void main()
{
float c, f;
clrscr();
printf("Enter Temperature in Celsius:");
scanf("%f",&c);
printf("\nFahrenheit=%f",f);
getch();
}
Output:
Program Name:
To find greatest number among entered 2 integer
values.
Program Code:
#include<stdio.h>
#include<conio.h>
void main()
{
int x, y;
clrscr();
printf("Enter two numbers:");
scanf("%d%d",&x, &y);
if(x>y)
{
printf("\n%d is Greatest Number ");
}
if(x<y)
{
printf("\n%d is Greatest Number ");
}
getch();
}
Output:
Program Name:
To read three integers and print largest among
three integers.
Program Code:
#include<stdio.h>
#include<conio.h>
void main()
{
int x, y, z;
clrscr();
printf("Enter Three Integers:");
scanf("%d%d%d",&x, &y, &z);
if(x>y && x>z)
{
printf("\n%d is Greatest Number", x);
}
if(y>z)
{
printf("\n%d is Greatest Number", y);
}
else
{
printf("\n%d is Greatest Number", z);
}
getch();
}
Output:
Program Name:
To read character from user and print if it is a
vowel or not.
Program Code:
#include<stdio.h>
void main()
{
char c;
clrscr();
printf("Enter The Character:");
scanf("%c",&c);
switch(c)
{
case 'a':
case 'A':printf("\n%c is a vowel", c);break;
case 'e':
case 'E':printf("\n%c is a vowel", c);break;
case 'i':
case 'I':printf("\n%c is a vowel", c);break;
case 'o':
case 'O':printf("\n%c is a vowel", c);break;
case 'u':
case 'U':printf("\n%c is a vowel", c);break;
default :printf("\n%c is not a vowel", c);
}
getch();
}
Output:
Program Name:
To read integer from user and perform operation
according to number.
Program Code:
#include<stdio.h>
#include<conio.h>
Void main()
{
Int n, x;
Print(“\nEnter 1 or 2 : “);
scan(“%d”, &n);
X=(n==1)?10:20;
Print(“\nX=%d”,x);
getch();
}
Output:
Program Name:
To read integer from user then calculate and print
its factorial using ‘for’ loop.
Program Code:
#include<stdio.h>
#include<conio.h>
void main()
{
int i, n, fact=1;
printf("\nEnter the Number:");
scanf("%d", &n);
for(i=1; i<=n; i++)
{
fact=fact*i;
}
printf("\n%d! =%d", n, fact);
getch();
}
Output:
Program Name:
To read integer from user and calculate if it is
prime number or not.
Program Code:
#include<stdio.h>
#include<conio.h>
void main()
{
int i, n, c=0;
clrscr();
printf("\nEnter the Number : ");
scanf("%d", &n);
for(i=2; i<n; i++)
{
if(n%i==0)
{
c++;
}
}
if(c==0)
{
printf("\n%d is Prime Number", n);
}
else
{
printf("\n %d is not a Prime Number", n);
}
getch();
}
Output:
Program Name:
To Print Fibonacci series.
Program Code:
#include<stdio.h>
#include<conio.h>
void main()
{
int i, x=0, y=1, s;
clrscr();
printf("\nFibonacci series : ");
printf("\n%d\t%d", x, y);
for(i=1; i<20; i++)
{
s=x+y;
printf("\t%d", s);
x=y;
y=s;
}
getch();
}
Output:
Program Name:
To print Symbolic pattern.
Program Code:
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n;
clrscr();
printf("How many Rows : ");
scanf("%d", &n);
for(i=1; i<=n; i++)
{
for(j=1; j<=i; j++)
printf("* ");
printf("\n");
}
getch();
}
Output:
Program Name:
To sort numeric elements in array.
Program Code:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10], i, j, temp;
clrscr();
for(i=0;i<10;i++)
{
printf("Enter A[%d] : ", i);
scanf("%d", &a[i]);
}
//Sorting Starts
for (i=1;i<10;i++)
{
for(j=0;j<10;j++)
{
if(a[j] > a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
//Sorting Complete
printf("\nSorted Array : ");
for(i=0;i<10;i++)
{
printf("\t%d", a[i]);
}
getch();
}
Output:
Program Name:
To read a string from user then print its reverse on
monitor.
Program Code:
#include<stdio.h>
#include<conio.h>
void main()
{
char str[30];
int i=0;
clrscr();
printf("\nEnter The String : ");
gets(str);
while(str[i]!='\0')
{
i++;
}
i--;
printf("\nReverse String : ");
while(i>=0)
{
printf("%c", str[i]);
i--;
}
getch();
}
Output:
Program Name:
To read a string from user, copy it into another
string variable.
Program Code:
#include<stdio.h>
#include<conio.h>
void main()
{
char str1[30], str2[30];
int i=0;
clrscr();
printf("\nEnter The String : ");
gets(str1);
while(str1[i]!='\0')
{
str2[i]=str1[i];
i++;
}
str2[i]='\0'; //Terminating
printf("\nCopied String : %s", str2);
getch();
}
Output:
Program Name:
To demonstrate external/global storage class.
Program Code:
#include<stdio.h>
#include<conio.h>
int x=10;
void demoglobal();
void main()
{
demoglobal();
x=x+1;
printf("\nX=%d", x);
demoglobal();
}
void demoglobal()
{
x=x+1;
printf("\nX=%d", x);
getch();
}
Output:
Program Name:
To exchange the value of two numbers using Call
by Value method.
Program Code:
#include<stdio.h>
#include<conio.h>
void main()
{
int x, y;
void exchange (int, int);
clrscr();
printf("\n Enter two numbers");
scanf("%d%d", &x, &y);
printf("\nValue of x=%d and y=%d before
function call", x, y);
exchange(x, y);
printf("\nValue of x=%d and y=%d after
function call", x, y);
getch();
}
void exchange(int a, int b)
{
int t;
t=a;
a=b;
b=t;
}
Output:
Program Name:
To exchange the value of two numbers using Call
by Reference method.
Program Code:
#include<stdio.h>
#include<conio.h>
void main()
{
int x, y;
void exchange (int*, int*);
clrscr();
printf("\n Enter two numbers");
scanf("%d%d", &x, &y);
printf("\nValue of x=%d and y=%d before
function call", x, y);
exchange(&x, &y);
printf("\nValue of x=%d and y=%d after
function call", x, y);
getch();
}
void exchange(int *a, int *b)
{
int t;
t=*a;
*a=*b;
*b=t;
}
Output:
Program Name:
To declare structure student and read or print data
of students.
Program Code:
#include<stdio.h>
#include<conio.h>
struct student
{
int roll;
char name[30];
float marks;
};
void main()
{
struct student s;
printf("Enter Roll Number : ");
scanf("%d", &s.roll);
printf("Enter The Name : ");
scanf("%s", &s.name);
printf("Enter The Marks : ");
scanf("%f", &s.marks);

printf("\n************************\n");
printf("\nRoll Number : %d", s.roll);
printf("\nName : %s", s.name);
printf("\nMarks : %.2f", s.marks);
//%.2f to show two decimal places
getch();
}
Output:

You might also like