You are on page 1of 23

UNIT-1

1.Write A C Program To Find The Sum Of First N Natural Numbers.


program:
#include <stdio.h>
void main()
{
int a=0,i,n;
printf("enter a number:");
scanf("%d",&n);
for (i=1;i<=n;i++)
a=a+i;
printf("the sum of %d first natural numbers is %d\n",n,a);
}
output:
enter a number:5
the sum of 5 first natural numbers is 15
2.C Program To Generate Multiplication Table Of A Given Number.
program:
#include <stdio.h>
void main()
{
int a,i;
printf("which table do u need:");
scanf("%d",&a);
for (i=1;i<=20;i++)
printf("%d * %d = %d\n",a,i,a*i);
}
output:
which table do u need:17
17 * 1 = 17
17 * 2 = 34
17 * 3 = 51
17 * 4 = 68
17 * 5 = 85
17 * 6 = 102
17 * 7 = 119
17 * 8 = 136
17 * 9 = 153
17 * 10 = 170
17 * 11 = 187
17 * 12 = 204
17 * 13 = 221
17 * 14 = 238
17 * 15 = 255
17 * 16 = 272
17 * 17 = 289
17 * 18 = 306
17 * 19 = 323
17 * 20 = 340
3.C Program To Develop Fabinacci Sequence (Upto Given Number).
program:
#include<stdio.h>
void main()
{
int n1=0,n2=1,n3=1,i=1,v;
printf("no of terms:");
scanf("%d",&v);
for (;i<=v;i++)
{
printf("%d,",n3);
n3=n1+n2;
n1=n2;
n2=n3;
}
}
output:
no of terms:8
1,1,2,3,5,8,13,21,
4.C Program to Check whether a given number is prime or not.
program:
#include<stdio.h>
void main()
{
int a,i,fact=0;
printf("enter a number:");
scanf("%d",&a);
for (i=1;i<=a;i++)
{
if (a%i==0)
fact=fact+1;
}
if (fact==2)
printf("%d is a prime number\n",a);
else
printf("%d is not a prime number\n",a);
}
output:
enter a number:6
6 is not a prime number
5.C Program To Make A Simple Calculator Using Switch Case.
program:
#include <stdio.h>
#include <stdlib.h>
void main()
{
printf(“arethimetic operations\n______________________\n");
printf("1.Addition:\n2.substraction:\n3.multiplication:\n4.division:\
n5.modulo:\nenter choice:");
scanf("%d",&choice);
printf("enter two numbers:");
scanf("%d%d",&a,&b);
switch(choice)
{
case 1:c=a+b;
break;
case 2:c=abs(a-b);
break;
case 3:c=a*b;
break;
case 4:c=a/b;
break;
case 5:c=a%b;
break;
}
printf(“result=%d”,result);
}
output:
arthemetic aperations
______________________
1.Addition:
2.substraction:
3.multiplication:
4.division:
5.modulo:
enter choice:3
enter two numbers:2
6
result=12
6.C Program To Check Whether A Number Is Palindrome Or Not.
program:
#include<stdio.h>
void main()
{
int a,i,temp,b,c=0;
printf("enter a number:");
scanf("%d",&a);
temp=a;
while (a>0)
{
b=a%10;
c=c*10+b;
a=a/10;
}
if (c==temp)
printf("%d is a palingdrome\n",temp);
else
printf("%d is not a palingdrome\n",temp);
}
output:
enter a number:424
424 is a palingdrome
7.C Program To Display Factors Of A Given Number.
program:
#include <stdio.h>
void main()
{
int a,i;
printf("enter a number:");
scanf("%d",&a);
for (i=1;i<=a;i++)
{
if (a%i==0)
printf("%d,",i);
}
printf("\n");
}
output:
enter a number:24
1,2,3,4,6,8,12,24,

8.Pyranids And Triangles:


I)Print The Shape
"*
**
*** ”
program:
#include<stdio.h>
void main()
{
int r,i,j,c;
printf("enter number of rows and columns in the square
matrix:");
scanf("%d",&r);
c=r
for (i=1;i<=r;i++)
{
for (j=1;j<=c;j++)
{
if (j<=i)
printf("*");
else
printf(" ");
}
printf("\n");
}
}
output:
enter number of rows and columns in the square matrix:5
*
**
***
****
*****
ii)print the shape
“*
***
***** “
program:
#include<stdio.h>
void main()
{
int r,i,j,c;
printf("enter number of rows:");
scanf("%d",&r);
c=2*r-1;
for (i=1;i<=r;i++)
{
for (j=1;j<=c;j++)
{
if ((j>=r+1-i)&&(j<=r-1+i))
{
printf("*");
}
else
printf(" ");
}
printf("\n");
}
}
output:
enter number of rows:6
*
***
*****
*******
*********
***********
iii)print the shape:
"
1
121
12321
"
program:
#include<stdio.h>
void main()
{
int r,i,j,c;
printf("enter number of rows:");
scanf("%d",&r);
c=2*r-1;
for (i=1;i<=r;i++)
{
for (j=1;j<=c;j++)
{
if ((j>=r+1-i)&&(j<=r-1+i))
{
if (j<=c/2)
printf("%d",i+j-r);
else
printf("%d",i+r-j);
}
else
printf(" ");
}
printf("\n");
}
}
output:
enter number of rows:5
1
121
12321
1234321
123454321

UNIT-II
1)C program to print the address of each element in an array.
Program:
#include<stdio.h>
void main()
{
int a[5]={1,2,3,4,5},i;
for (i=0;i<5;i++)
printf("the address of element of index %d is %d\n",i,&a[i]);
}

output:
the address of element of index 0 is 606155904
the address of element of index 1 is 606155908
the address of element of index 2 is 606155912
the address of element of index 3 is 606155916
the address of element of index 4 is 606155920

2)C program to print negative numbers and count of negative numbers.


Program:
#include<stdio.h>
void main()
{
int a[100],i,n,neg_count=0;
printf("enter no of elements in array-");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("a[%d] = ",i);
scanf("%d",&a[i]);
}
printf("negative numbers in the array are:");
for (i=0;i<n;i++)
{
if(a[i]<0)
{
printf("%d,",a[i]);
neg_count++;
}
}
printf("\nAnd total no of negative numbers present in the array are %d.\
n",neg_count);
}
output:
enter no of elements in array-6
a[0] = -4
a[1] = 2
a[2] = -5
a[3] = -7
a[4] = 3
a[5] = 7
negative numbers in the array are:-4,-5,-7,
And total no of negative numbers present in the array are 3.

3)C program to print the array elements in reverse order.


Program:
#include<stdio.h>
void main()
{
int a[500],i;
printf("enter no of elements in array : ");
scanf(“%d”,&n);
for (i=0;i<n;i++)
{
printf("enter a number:");
scanf("%d",&a[i]);
}
for (i=n-1;i>=0;i--)
{
printf("%d,",a[i]);
}
printf("\n");
}

output:
enter no of elements in array : 5
enter a number:4
enter a number:2
enter a number:5
enter a number:2
enter a number:4
4,2,5,2,4,

4)C program to print the index of given number:


program:
#include<stdio.h>
void main()
{
int a[5],i,n,k,v=0;
printf("enter no of elements in array : ");
scanf("%d",&k);
for (i=0;i<k;i++)
{
printf("enter a number:");
scanf("%d",&a[i]);
}
printf("enter a number to search:");
scanf("%d",&n);
for (i=0;i<5;i++)
{
if (a[i]==n)
{ printf("entered element index:%d\n",i);
v=v+1;
break;
}
}
if (v==0)
printf("entered number is not in the array");
}
output:
enter no of elements in array : 5
enter a number:4
enter a number:3
enter a number:7
enter a number:2
enter a number:5
enter a number to search:7
entered element index:2

5)C program to delete the element at given index:


Program:
#include<stdio.h>
void main()
{
int a[500],k,n,i;
printf("enter no of elements in array : ");
scanf("%d",&n);
for (i=0;i<n;i++)
{
printf("enter a number:");
scanf("%d",&a[i]);
}
printf("enter index to delete:");
scanf("%d",&k);
for (i=k;i<n;i++)
a[i]=a[i+1];
for (i=0;i<n-1;i++)
printf("%d,",a[i]);
}

output:
enter no of elements in array : 5
enter a number:3
enter a number:6
enter a number:1
enter a number:3
enter a number:4
enter index to delete:3
3,6,1,4,

6)C program to insert a element at the given index.


Program:
#include<stdio.h>
void main()
{
int a[500],b,i,a,n;
int a[500],k,n,i;
printf("enter no of elements in array : ");
scanf("%d",&n);
for (i=0;i<n;i++)
{
printf("enter a number:");
scanf("%d",&a[i]);
}

printf("enter a number to insert:");


scanf("%d",&a);

printf("enter the index to input:");


scanf("%d",&b);

for (i=3;i>=b;i--)
a[i+1]=a[i];
a[b]=n;

for (i=0;i<5;i++)
printf("%d,",a[i]);
}
output:
enter no of elements in array : 5
enter a number:3
enter a number:5
enter a number:2
enter a number:3
enter a number:4
enter a number to insert:5
enter the index to input:1
3,5,5,2,3,

UNIT-III
1)C program to add two matrices :
program:
#include<stdio.h>
#include<stdlib.h>
void main()
{
int i,j,A[20][20],B[20][20],C[20][20],m,n,o,p;
printf("enter first matrix rows and columns:");
scanf("%d %d",&m,&n);
printf("enter second matrix rows and columns:");
scanf("%d %d",&o,&p);
if ((m!=o)||(n!=p))
{
printf("matrix addition is not possible.\n");
exit(0);
}
printf("enter first matrix elements :\n");
for (i=0;i<m;i++)
{
for (j=0;j<n;j++)
{
printf("\ta[%d][%d] : ",i,j);
scanf("%d",&A[i][j]);
}
}
printf("enter second array elements:\n");
for (i=0;i<o;i++)
{
for (j=0;j<p;j++)
{
printf("\tb[%d][%d] : ",i,j);
scanf("%d",&B[i][j]);
}
}
printf("\nadded matrix elements:\n");
printf("C=(A+B)=\n");
for (i=0;i<m;i++)
{
for (j=0;j<n;j++)
C[i][j]=A[i][j]+B[i][j];
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("\t%d",C[i][j]);
printf("\n\n");
}
}
output:
enter first matrix rows and columns:2
2
enter second matrix rows and columns:2
2
enter first matrix elements :
a[0][0] : 4
a[0][1] : 6
a[1][0] : 7
a[1][1] : 4
enter second array elements:
b[0][0] : 6
b[0][1] : 3
b[1][0] : 8
b[1][1] : 7
added matrix elements:
C=(A+B)=
10
9
15
11
2.C program to substract a matrix from another matrix.
Program:
#include<stdio.h>
#include<stdlib.h>
void main()
{
int i,j,A[20][20],B[20][20],C[20][20],m,n,o,p;
printf("enter first matrix rows and columns:");
scanf("%d %d",&m,&n);
printf("enter second matrix rows and columns:");
scanf("%d %d",&o,&p);
if ((m!=o)||(n!=p))
{
printf("matrix addition is not possible.\n");
exit(0);
}
printf("enter first matrix elements :\n");
for (i=0;i<m;i++)
{
for (j=0;j<n;j++)
{
printf("\ta[%d][%d] : ",i,j);
scanf("%d",&A[i][j]);
}
}
printf("enter second array elements:\n");
for (i=0;i<o;i++)
{
for (j=0;j<p;j++)
{
printf("\tb[%d][%d] : ",i,j);scanf("%d",&B[i][j]);
}
}
printf("\nsubtracted matrix elements:\n");
printf("C=(A-B)=\n");
for (i=0;i<m;i++)
{
for (j=0;j<n;j++)
C[i][j]=A[i][j]-B[i][j];
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("\t%d",C[i][j]);
printf("\n\n");
}
}
output:
enter first matrix rows and columns:2
2
enter second matrix rows and columns:2
2
enter first matrix elements :
a[0][0] : 4
a[0][1] : 6
a[1][0] : 8
a[1][1] : 5
enter second array elements:
b[0][0] : 1
b[0][1] : 4
b[1][0] : 2
b[1][1] : 3
subtracted matrix elements:
C=(A-B)=
3
2
6
2
3)c program to a multiply a matrix with another matrix.
Program:
#include<stdio.h>
#include<stdlib.h>
void main()
{
int i,j,k,A[20][20],B[20][20],C[20][20],m,n,o,p;
printf("enter first matrix rows and columns:");
scanf("%d %d",&m,&n);
printf("enter second matrix rows and columns:");
scanf("%d %d",&o,&p);
if (n!=o)
{
printf("matrix product is not possible.\n");
exit(0);
}
printf("enter first matrix elements :\n");
for (i=0;i<m;i++)
{
for (j=0;j<n;j++)
{
printf("\tA[%d][%d] : ",i,j);
scanf("%d",&A[i][j]);
}
}
printf("enter second array elements:\n");
for (i=0;i<o;i++)
{
for (j=0;j<p;j++)
{
printf("\tB[%d][%d] : ",i,j);
scanf("%d",&B[i][j]);
}
}
printf("\nmultiplied matrix elements:\n");
printf("C=(AB)=\n");
for (i=0;i<m;i++)
{
for (j=0;j<p;j++)
{
C[i][j]=0;
for (k=0;k<n;k++)
C[i][j]+=A[i][k]*B[k][j];
}
}
for(i=0;i<p;i++)
{
for(j=0;j<p;j++)
printf("\t%d",C[i][j]);
printf("\n\n");
}
}

output:
enter first matrix rows and columns:2
3
enter second matrix rows and columns:3
2
enter first matrix elements :
A[0][0] : 5
A[0][1] : 3
A[0][2] : 7
A[1][0] : 1
A[1][1] : 4
A[1][2] : 8
enter second array elements:
B[0][0] : 4
B[0][1] : 6
B[1][0] : 2
B[1][1] : 7
B[2][0] : 5
B[2][1] : 3

multiplied matrix elements:


C=(AB)=
61 72

52 58
4)C program to print transpose of given matrix.
Program:
#include<stdio.h>
void main()
{
int i,j,a[20][20],b[20][20],r,c;
printf("enter no of rows :");
scanf("%d",&r);
printf("enter no of columns:");
scanf("%d",&c);
for (i=0;i<c;i++)
{
for (j=0;j<c;j++)
{
printf("enter a value:");
scanf("%d",&a[i][j]);
}
}
printf("a transpose=\n");
for (j=0;j<r;j++)
{
for (i=0;i<c;i++)
{
b[j][i]=a[i][j];
}
}
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
printf("\t%d",b[i][j]);
printf("\n");
}
}

output:
enter no of rows :2
enter no of columns:3
enter a value:2
enter a value:3
enter a value:45
enter a value:2
enter a value:4
enter a value:5
a transpose=
2 2
3 4
45 5

5)bubble sort:
program:
#include<stdio.h>
void main()
{
int a[50],i,j,temp,v=1,n;
printf("enter no of elements need to sort:");
scanf("%d",&n);
for (i=0;i<n;i++)
{
printf("enter a number:");
scanf("%d",&a[i]);
}
for (i=0;(i<n-1);i++)
{
v=0;
for (j=0;j<n-1-i;j++)
{
if (a[j]>a[j+1])
{
v=1;
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
if (v==0)
break;
}
printf("elements after getting sorted:");
for (i=0;i<n;i++)
printf("%d,",a[i]);
printf("\n");
}

output:
enter no of elements need to sort:5
enter a number:3
enter a number:6
enter a number:3
enter a number:8
enter a number:1
elements after getting sorted:1,3,3,6,8,

6)selection sort:
program:
#include<stdio.h>
void main()
{
int a[50],i,j,temp,n,min;
printf("enter no of elements in the list:");
scanf("%d",&n);
for (i=0;i<n;i++)
{
printf("a[%d] = ",i);
scanf("%d",&a[i]);
}
for(i=0;i<n-1;i++)
{
min=i;
for (j=i+1;j<n;j++)
{
if (a[j]<a[min])
min=j;
}
temp=a[i];
a[i]=a[min];
a[min]=temp;
}
printf("\nsorted list=");
for (i=0;i<n;i++)
printf("\t%d",a[i]);
printf("\n");
}
output:
enter no of elements in the list:5
a[0] = 4
a[1] = 6
a[2] = 2
a[3] = 8
a[4] = 4

sorted list= 2 4 4 6 8

7)insertion sort
program:
#include<stdio.h>
void main()
{
int a[50],i,j,temp,n;
printf("enter no of elements needed to sort:");
scanf("%d",&n);
for (i=0;i<n;i++)
{
printf("a[%d] = ",i);
scanf("%d",&a[i]);
}
for(i=1;i<n;i++)
{
temp=a[i];
j=i-1;
while(temp<a[j])
{
a[j+1]=a[j];
j--;
}
a[j+1]=temp;
}
printf("\nsorted list=");
for (i=0;i<n;i++)
printf("\t%d",a[i]);
printf("\n");
}

output:
enter no of elements needed to sort:5
a[0] = 7
a[1] = 3
a[2] = 5
a[3] = 1
a[4] = 8

sorted list= 1 3 5 7 8

8)binary search:
program:
#include<stdio.h>
void main()
{
int a[50],i,j,temp,n,key,low,k=0,high,mid;
printf("enter no of elements in the list:");
scanf("%d",&n);
for (i=0;i<n;i++)
{
printf("a[%d] = ",i);
scanf("%d",&a[i]);
}
for(i=1;i<n;i++)
{
temp=a[i];
for(j=i-1;temp<a[j];j--)
a[j+1]=a[j];
a[j+1]=temp;
}
printf("enter element needed to search:");
scanf("%d",&key);
low=0;
high=n-1;
while (low<=high)
{
mid=(low+high)/2;
if (a[mid]==key)
{
printf("enter value is founded at the index of %d in sorted list \
n",mid);
k=1;
break;
}
else if (key<a[mid])
high=mid-1;
else
low=mid+1;
}
if (k==0)
printf("entered element is not found.\n");
}

output:
enter no of elements in the list:5
a[0] = 3
a[1] = 7
a[2] = 2
a[3] = 5
a[4] = 3
enter element needed to search:7
enter value is founded at the index of 4 in sorted list

STRING FUNCTIONS
1)strlen()
program:
#include<stdio.h>
#include<string.h>
void main()
{
int i;
char str1[1000];
printf("enter a string:");
scanf("%[^\n]",str1);
printf(“string length is %d.\n”,strlen(str1));
}
output:
enter a string:google
string length is 6.
2)strcpy()
program:
#include<stdio.h>
#include<string.h>
void main()
{
int i;
char str1[1000],str2[100],ch;
printf("enter a string:");
scanf("%[^\n]",str1);
printf("strings before copy are str1=%s and str2=%s",str1,str2);
strcpy(str2,str1);
printf("\nstrings after copy are str1=%s and str2=%s",str1,str2);
printf("\n");
}
output:
enter a string:Keep calm
strings before copy are str1=Keep calm and str2=
strings after copy are str1=Keep calm and str2=Keep calm
3)strcat()
program:
#include<stdio.h>
#include<string.h>
void main()
{
int i;
char str1[1000],str2[100]="hi",ch;
printf("enter a string:");
scanf("%[^\n]",str1);
printf("strings before catenation are str1=%s and str2=%s",str1,str2);
strcat(str2,str1);
printf("\nstrings after catenation are str1=%s and str2=%s",str1,str2);
printf("\n");
}
output:
enter a string:go
strings before catenation are str1=go and str2=hi
strings after catenation are str1=go and str2=higo

4)strcmp()
program:
#include<stdio.h>
#include<string.h>
void main()
{
char a[100],b[100];
printf("enter two strings:");
gets(a);
gets(b);
if(strcmp(a,b)==0)
printf("both strings are identical");
else if(strcmp(a,b)>0)
printf("string %s comes first in lexilogical order",b);
else
printf("string %s comes first in lexicogical order",a);
printf("\n");
}
output:
enter two strings:hi
hello
string hello comes first in lexicographical order

UNIT-IV
1)C program to check whether given number is prime or not using user defined function.
Program:
#include<stdio.h>
int prime(int);
void main()
{
int a;
printf("enter a number:");
scanf("%d",&a);
if (prime(a)!=0)
printf("%d is a prime number\n",a);
else
printf("%d is not a prime number\n",a);
}
int prime(int a)
{
int i,fact=0;
for (i=1;i<=a;i++)
{
if (a%i==0)
{
fact=fact+1;
}
}
if(fact==2)
return 1;
return 0;
}
output:
enter a number:5
5 is a prime number

2)C program to swap two integers using call by value and call by reference.
i.call by value:
Program:
#include<stdio.h>
void swap();
int a=2,b=3;
void main()
{
printf("before swap :\na = %d \nb = %d\n.",a,b);
swap();
printf("after swap :\na = %d \nb = %d\n.",a,b);
}
void swap()
{
int temp;
temp=a;
a=b;
b=temp;
}
output:
before swap :
a=2
b=3
.after swap :
a=3
b=2
ii.call by reference:
Program:
#include<stdio.h>
void swap(int *a,int *b);
void main()
{
int a,b;
printf("enter a value :");
scanf("%d",&a);
printf("enter b value:");
scanf("%d",&b);
printf("before swap :\na = %d \nb = %d.\n.",a,b);
swap(&a,&b);
printf("after swap:\na = %d \nb = %d.\n",a,b);
}
void swap(int *a,int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
}
output:
enter a value:4
enter b value:6
before swap :
a=4
b = 6.
.after swap:
a=6
b = 4.

3)C program to find the factorial of a given numbers using recursion.


Program:
#include<stdio.h>
int fact(int);
void main()
{
int f,a;
printf("enter a number : ");
scanf("%d",&a);
f=fact(a);
printf("factorial of %d is %d\n",a,f);
}
int fact(int n)
{
int num;
if(n==1)
return 1;
else
num=n*fact(n-1);
return num;
}
output:
enter a number : 5
factorial of 5 is 120

4)C program to find length of a string without using strlen() functions.


Program:
#include<stdio.h>
int length(char*);
void main()
{
char a[1000];
printf(“enter a string : ”);
gets(a);
length(a);
printf("%d\n",length(a));
}
int length(char*a)
{
int i;
for(i=0;(*(a+i)!='\0');i++)
{}
return i;
}
output:
enter a string : There will be always a way.All you need to do is to search for it.
66

5)C program to sort elements in lexicographical order using inbuilt string functions.
Program:
#include<stdio.h>
#include<string.h>
void main()
{
char a[50][1000],temp[1000];
int i,j,n;
printf("enter no of strings needed to sort:");
scanf("%d",&n);
printf("enter strings:");
for (i=0;i<=n;i++)
gets(a[i]);
for(i=2;i<=n;i++)
{
strcpy(temp,a[i]);
j=i-1;
while(strcmp(a[j],temp)>0)
{
strcpy(a[j+1],a[j]);
j--;
}
strcpy(a[j+1],temp);
}
printf("the elements in lexicographical order are :");
for (i=1;i<=n;i++)
printf(" %s ,",a[i]);
printf("\n");
}
output:
enter no of strings needed to sort:6
enter strings:focus
on
what
you
left
with
the elements in lexicographical order are : focus , left , on , what , with , you ,

You might also like