0% found this document useful (0 votes)
74 views36 pages

C Programming Exercises

The document contains programs written in C programming language to perform various tasks like finding the ASCII value of a number, calculating the square of numbers using different loops, counting vowels and consonants, calculating the sum of a 3 dimensional matrix, finding prime numbers between two numbers, Fibonacci series, factorial, power of a value, sorting arrays using different algorithms, and storing student records. Output is displayed after each program showing the results of sample test runs.

Uploaded by

Ramachandran
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)
74 views36 pages

C Programming Exercises

The document contains programs written in C programming language to perform various tasks like finding the ASCII value of a number, calculating the square of numbers using different loops, counting vowels and consonants, calculating the sum of a 3 dimensional matrix, finding prime numbers between two numbers, Fibonacci series, factorial, power of a value, sorting arrays using different algorithms, and storing student records. Output is displayed after each program showing the results of sample test runs.

Uploaded by

Ramachandran
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/ 36

*PROGRAM TO FIND ASCII VALUE*

#include<stdio.h>
#include<conio.h>
main()
{
int n;
clrscr();
printf("Enter n value");
scanf("%d",&n);
printf("\n Ascii value of %d is %c",n,n);
getch();
}
Output:

Enter n value7
Ascii value of 7 is 55
*SQUARE OF NUMBERS USING FOR LOOP*

#include<stdio.h>
main()
{
int i;
clrscr();
for(i=0;i<=10;i++)
printf(“\n Square of %d is %d”,i,i*i);
getch();
}
Output:

Square of 1 is 1
Square of 2 is 4
Square of 3 is 9
Square of 4 is 16
Square of 5 is 25
Square of 6 is 36
Square of 7 is 49
Square of 8 is 64
Square of 9 is 81
Square of 10 is 100
*SQUARE OF NUMBERS USING WHILE LOOP*

#include<stdio.h>
main()
{
int i=1;
clrscr();
while(i<=10)
{
printf(“\n square of %d is %d”,i,i*i);
i++;
}
getch();
}
Output:

Square of 1 is 1
Square of 2 is 4
Square of 3 is 9
Square of 4 is 16
Square of 5 is 25
Square of 6 is 36
Square of 7 is 49
Square of 8 is 64
Square of 9 is 81
Square of 10 is 100
*SQUARE OF NUMBERS USING DO-WHILE
LOOP*

#include<stdio.h>
main()
{
int i=1;
clrscr();
do
{
printf(“\n square of %d is %d”,i,i*i);
i++;
}
while(i<=10);
getch();
}
Output:

Square of 1 is 1
Square of 2 is 4
Square of 3 is 9
Square of 4 is 16
Square of 5 is 25
Square of 6 is 36
Square of 7 is 49
Square of 8 is 64
Square of 9 is 81
Square of 10 is 100
*SQUARE OF NUMBERS USING GOTO
STATEMENT*

#include<stdio.h>
main()
{
int i=1;
clrscr();
AA:
if(i<=10)
{
printf(“\n square of %d is %d”,i,i*i);
i++;
goto AA;
}
getch();
}
Output:

Square of 1 is 1
Square of 2 is 4
Square of 3 is 9
Square of 4 is 16
Square of 5 is 25
Square of 6 is 36
Square of 7 is 49
Square of 8 is 64
Square of 9 is 81
Square of 10 is 100
*PROGRAM TO PRINT CHARACTERS BETWEEN
TWO CHARACTERS*

#include<stdio.h>
#include<conio.h>
main()
{
char ch1,ch2;
clrscr();
printf("Enter first,sec char");
scanf("%c",&ch1);
fflush(stdin);
scanf("%c",&ch2);
if(ch1==ch2)
printf("%c",ch1);
else if(ch1<ch2)
{
while(ch1-1!=ch2)
printf("%c",ch1++);
}
else
{
while(ch1!=ch2)
printf("%c",ch1--);
}
getch();
}
Output:

Enter first,sec char a f


abcdef
*PROGRAM TO CALCULATE SUM OF THREE
DIMENSIONAL MATRIX*

#include<stdio.h>
#include<math.h>
main()
{
int n[10][10][10];
int i,j,k,l=1,sum=0;
clrscr();
for(i=0;i<3;i++)
for(j=0;j<3;j++)
for(k=0;k<3;k++)
{
n[i][j][k]=l;
sum=sum+n[i][j][k];
l++;
}
printf("\n3 dim. mat\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
for(k=0;k<3;k++)
{
printf("\t%d",n[i][j][k]);
}
printf("\n");
}
printf("\n");
}
printf("\nsum of elements=%d",sum);
getch();
}
Output:

3 dim. mat

1 2 3
4 5 6
7 8 9

10 11 12
13 14 15
16 17 18

19 20 21
22 23 24
25 26 27

sum of elements=378
*PROGRAM TO COUNT VOWELS AND
CONSONANTS *

#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char ch,str[50];
int i,v=0,c=0,len;
clrscr();
printf("Enter string:");
scanf("%s",str);
len=strlen(str);
for(i=0;i<len;i++)
{
ch=tolower(str[i]);
switch(ch)
{
case'a':
case'e':
case'i':
case'o':
case'u':
v++;
break;
default:
c++;
}
}
printf("\n vowels=%d\nconsonants=%d",v,c);
getch();
}
Output:

Enter string: welcome


vowels=3
consonants=4
*PROGRAM TO PRINT PRIME NUMBER BETWEEN
TWO NUMBERS*

#include<stdio.h>
#include<math.h>
main()
{
int i1,i2,i,j,flag;
clrscr();
printf("\nEnter two pos integer");
scanf("%d%d",&i1,&i2);
printf("\nPrime no. between %d and %d",i1,i2);
if(i1>2)
{
i1=2;
printf("%d",i1);
i1++;
}
else if(i1%2==0)
i1++;
for(i=i1;i<i2;i+=2)
{
flag=0;

for(j=3;j<sqrt(i);j+=2)
{
if(i%j==0)
{
flag=1;
break;
}
}
if(flag==0)
printf("%5d",i);
}
getch();
}
Output:

Enter two pos integer1 17


Prime no.between 1 and 17 1 3 5 7 11
13
*PROGRAM TO PRINT FIBONACCI SERIES*

#include<stdio.h>
main()
{
int n,i;
int fibo(int);
printf("\nEnter n value");
scanf("%d",&n);
for(i=0;i<n;i++)
printf("\n\t\t%d",fibo(i));
getch();
}
int fibo(int x)
{
if(x==0)
return 0;
else
if(x==1)
return 1;
else
return(fibo(x-1)+fibo(x-2));
}
Output:

Enter n value 5
0
1
1
2
3
*PROGRAM TO FIND FACTORIAL VALUE*

#include<stdio.h>
main()
{
int n;
int fact(int);
printf("\nEnter value");
scanf("%d",&n);
printf("\nvalue of %d! is %d",n,fact(n));
getch();
}
int fact(int x)
{
(x<=1)?1:x*fact(x-1);
}
Output:

Enter value 4
value of 4! is 24
*PROGRAM TO FIND POWER OF A VALUE*

#include<stdio.h>
#include<math.h>
main()
{
int x,y;
long int d;
long int power(int,int);
printf("\nEnter x,y values");
scanf("%d%d",&x,&y);
d=power(x,y);
printf("\n\t %d power of %d is %ld",x,y,d);
getch();
}
long int power(int p,int q)
{
int i;
long int pow=1;
for(i=1;i<=q;i++)
pow=pow*p;
return(pow);
}
Output:

Enter x,y values 2 6


2 power of 6 is 64
*PROGRAM FOR INTERCHANGE SORT*

#include<stdio.h>
main()
{
int i,j,n,temp,a[10];
printf("enter no of elements");
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("\nAscending order is");
for(i=0;i<n;i++)
printf("\t%d",a[i]);
getch();
}
Output:

Enter no of elements 5
12 3 69 4 92
Ascending order is 3 4 12 69 92
*PROGRAM FOR SHELL SORT*

#include<stdio.h>
main()
{
int a[20],n,i;
void shell();
printf("\nEnter no of elements in an array");
scanf("%d",&n);
printf("Enter matrix");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
shell(a,n);
printf("Sorted array using shell sort");
for(i=0;i<n;i++)
printf("\t%d",a[i]);
}
void shell(b,n)
int b[20],n;
{
int i,j,gap,temp,swaps;
gap=n/2;
do
{
printf("\n\tgap=%d",gap);
printf("Element at a distance %d are swapped if
necessary",gap);
do
{
swaps=0;
for(i=0;i<(n-gap);i++)
{
if(b[i]>b[i+gap])
{
temp=b[i];
b[i]=b[i+gap];
b[i+gap]=temp;
swaps=1;
}
}
}
while(swaps);
printf("\n\t Array at the end of pass\n");
for(j=0;j<n;j++)
printf("\t%d",b[j]);
}
while((gap=gap/2)>0);
return;
}
Output:

Enter number of elements in an array 5


Enter matrix 23 54 6 43 89 60
Gap=3
Element at a distance 3 are swapped if necessary:
Array at the end of pass:
23 54 6 43 89 60
Gap=1
Element at a distance 1 are swapped if necessary:
Array at the end of pass:
6 23 43 54 60 89
Sorted array using shell sort
6 23 43 54 60 89
*PROGRAM FOR STUDENT DETAILS*

#include<stdio.h>
#include<conio.h>
struct student
{
char name[20],grade;
int score[10],high,low;
float avg;
}
cand;
main()
{
input();
output();
}
input()
{
int j,sum,mk;
float avgmk;
clrscr();
printf("\nenter name of candidate");
scanf("%s",cand.name);
printf("\nten marks of the student");
cand.low=100;
cand.high=0;
sum=0;
for(j=0;j<10;j++)
{
printf("\ntest%d",j+1);
scanf("%d",&mk);
cand.score[j]=mk;
sum=sum+mk;
if(cand.high<mk)
cand.high=mk;
if(cand.low>mk)
cand.low=mk;
}
avgmk=sum/10.0;
cand.avg=avgmk;
if(avgmk<40)
cand.grade='F';
else if(avgmk<50)
cand.grade='D';
else if(avgmk<60)
cand.grade='C';
else if(avgmk<75)
cand.grade='B';
else
cand.grade='A';
}
output()
{
int j;
printf("\ncand name is %s\nhigh mark=%d\nlow mark=
%d\navg=%4.2f\ngrade=%c",cand.name,
cand.high,cand.low,cand.avg,cand.grade);
getch();
}
Output:
Enter the name of candidate: Sashti
Ten marks of the student
Test 1: 100
Test 2: 90
Test 3: 98
Test 4: 100
Test 5: 80
Test 6: 90
Test 7: 99
Test 8: 98
Test 9: 100
Test 10: 100

Candidate name is:Sashti


High mark =100
Low mark=80
Avg=95.50
Grade=A
EX. PAGE
DATE EXPERIMENT NAME SIGNATURE
NO NO

1 ASCII VALUE OF NUMBER

SQUARE OF NUMBERS
2(i)
USING FOR LOOP
SQUARE OF NUMBERS
2(ii)
USING WHILE LOOP
SQUARE OF NUMBERS
3(i)
USING DO-WHILE LOOP
SQUARE OF NUMBERS
3(ii)
USING GOTO STATEMENT
CHARACTERS BETWEEN
4
TWO GIVEN CHARACTERS
COUNT NUMBER OF
5 VOWELS AND
CONSONANTS
SUM OF THREE
6
DIMENSIONAL MATRIX
PRIME NUMBERS
7
BETWEEN TWO NUMBERS

8 FIBONACCI SERIES

9 FACTORIAL NUMBER

10 POWER OF A VALUE

11 INTERCHANGE SORT

12 SHELL SORT

13 STUDENT RECORD

You might also like