C Sem Programs
C Sem Programs
C Sem Programs
Source code:
#include<stdio.h>
int main()
{
int i,n;
long int fac=1;
printf("Enter a number\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
fac=fac*i;
}
printf("Factorial of %d is %ld",n,fac);
return 0;
}
OUTPUT:
Enter a number
5
Factorial of 5 is 120
2) Write a C program to generate and print the numbers between 100 and 200 which are
divisible by 3 but not divisible by 4. 8) Write C program to check whether the given number is
even or odd without using % (modulus).
Source code:
#include<stdio.h>
int main()
{
int i,n;
printf("The numbers which are divisible by 3 between 100 to 200 are\n");
for(i=101;i<200;i++)
{
if(i%3==0)
printf("%d\n",i);
}
printf("The numbers which are not divisible by 4 between 100 to 200 are\n");
for(i=101;i<200;i++){
if(i%4!=0)
printf("%d\n",i);
}
return 0;
}
OUTPUT:
The numbers which are divisible by 3 between 100 to 200 are
102
105
108
111
114
117
120
123
126
129
132
135
138
141
144
147
150
153
156
159
162
165
168
171
174
177
180
183
186
189
192
195
198
The numbers which are not divisible by 4 between 100 to 200 are
101
102
103
105
106
107
109
110
111
113
114
115
117
118
119
121
122
123
125
126
127
129
130
131
133
134
135
137
138
139
141
142
143
145
146
147
149
150
151
153
154
155
157
158
159
161
162
163
165
166
167
169
170
171
173
174
175
177
178
179
181
182
183
185
186
187
189
190
191
193
194
195
197
198
199
3) Write C program to check whether the given number is even or odd without using %
(modulus) operator.
Source code:
#include<stdio.h>
int main()
{
int n,temp;
printf("Enter a number\n");
scanf("%d",&n);
temp=n/2;
temp=temp*2;
if(temp==n)
printf("Even number");
else
printf("Odd number ");
return 0;
}
OUTPUT:
Case 1:
Enter a number
5
Odd number
Case 2:
Enter a number
12
Even number
4) Write a C program to check whether the given number n is prime or not.
Source code:
#include<stdio.h>
int main()
{
int n,flag=0, i,j;
printf("Enter a number\n");
scanf("%d",&n);
for(i=1; i<=n; i++) {
if(n%i==0) {
flag++;
}
}
if(flag==2)
printf("Prime number");
else
printf("Not a prime number");
return 0;
}
OUTPUT:
Case 1:
Enter a number
7
Prime number
Case 2:
Enter a number
46
Not a prime number
Source code:
#include<stdio.h>
int main(){
int n,rem,temp,rev=0;
printf("Enter a number\n");
scanf("%d",&n);
temp=n;
while(n>0)
{
rem=n%10;
rev=rev*10+rem;
n=n/10;
}
if(temp==rev)
printf("Palindrome number");
else
printf("Not a Palindrome number");
return 0;
}
OUTPUT:
Case 1:
Enter a number
121
Palindrome number
Case 2:
Enter a number
788
Not a Palindrome number
6). Write a C program to calculate the sum and difference of two 2-dimensional matrices.
Source code:
#include<stdio.h>
int main()
{
int a[10][10],b[10][10],c[10][10],d[10][10],m1,m2,i,j;
printf("Enter size of matrix A\n");
scanf("%d",&m1);
printf("Enter size of matrix B\n");
scanf("%d",&m2);
if(m1==m2)
{
printf("Enter matrix A of %dX%d order\n",m1,m1);
for(i=0;i<m1;i++){
for(j=0;j<m1;j++){
scanf("%d",&a[i][j]);
}
}
printf("Enter matrix B of %dX%d order\n",m2,m2);
for(i=0;i<m2;i++){
for(j=0;j<m2;j++){
scanf("%d",&b[i][j]);
}
}
for(i=0;i<m1;i++){
for(j=0;j<m1;j++){
c[i][j]=a[i][j]+b[i][j];
d[i][j]=a[i][j]-b[i][j];
}
}
printf("Addition of 2 matrices is\n");
for(i=0;i<m1;i++){
for(j=0;j<m1;j++){
printf("%d\t",c[i][j]);
}
printf("\n");
}
printf("Difference of 2 matrices is\n");
for(i=0;i<m1;i++){
for(j=0;j<m1;j++){
printf("%d\t",d[i][j]);}
printf("\n");
}
}
else
printf("Matrix addition and subtraction is not possible");
return 0;
}
OUTPUT:
Case 1:
Enter size of matrix A
2
Enter size of matrix B
2
Enter matrix A of 2X2 order
2 2
2 2
Enter matrix B of 2X2 order
2 2
2 2
Addition of 2 matrices is
4 4
4 4
Difference of 2 matrices is
0 0
0 0
Case 2:
Enter size of matrix A
2
Enter size of matrix B
3
Matrix addition and subtraction is not possible
7) Write a C program to find sum of the digits of any given positive integer.
Source Code:
#include<stdio.h>
int main()
{
int n,rem,sum=0;
printf("Enter a number\n");
scanf("%d",&n);
while(n>0) {
rem=n%10;
sum=sum+rem;
n=n/10;
}
printf("Sum of the digits of the given integer is:%d",sum);
return 0;
}
OUTPUT:
Enter a number
121
Sum of the digits of the given integer is:4
8) Write a C program to find the length of a given string without using built-in functions. Also
check whether the given string is palindrome or not.
Source code:
#include<stdio.h>
#include<string.h>
int main()
{
char str[20];
int l,j=0,i;
printf("Enter a string\n");
gets(str);
for(l=0;str[l]!='\0';l++);
printf("Length of the string is:%d\n",l);
for(i=0,l=l-1;str[i]!='\0';i++,l--)
{
if(str[i]==str[l])
j++;
}
if(j==i)
printf("String is palindrome");
else
printf("String is not a palindrome");
return 0;
}
OUTPUT:
Case 1:
Enter a string
mom
Length of the string is:3
String is palindrome
Case 2:
Enter a string
hello
Length of the string is:5
String is not a palindrome
Source code:
#include<stdio.h>
int main()
{
int a[10][10],b[10][10],c[10][10],i,j,k,r1,c1,r2,c2;
printf("Enter the order of row, column of first Matrix");
scanf("%d%d",&r1,&c1);
printf("Enter the order of row column of second Matrix");
scanf("%d%d",&r2,&c2);
if(c1!=r2)
{
printf("Matrix multiplication is not possible");
}
else
{
printf("Enter elements into the matrix a:\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter elements into the matrix b:\n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
c[i][j]=0;
for(k=0;k<c1;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
printf("The multiplied matrix is\n");
for(i=0;i<r1;i++)
{
for(j=0;j<r2;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
}
return 0;
}
OUTPUT:
Case 1:
Enter the order of row, column of first Matrix2 2
Enter the order of row column of second Matrix2 2
Enter elements into the matrix a:
56
23
Enter elements into the matrix b:
13
13
The multiplied matrix is
11 33
5 15
Case 2:
Enter the order of row, column of first Matrix1 3
Enter the order of row column of second Matrix2
3
Matrix multiplication is not possible
10) Write a C program to check whether the given matrix is symmetric or not.
Source code:
#include<stdio.h>
int main()
{
int a[10][10],n,i,j,count=0;
printf("Enter size\n");
scanf("%d",&n);
printf("Enter a %dX%d matrix\n",n,n);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(a[i][j]==a[j][i])
count++;
}
}
if(count==(n*n))
printf("Symmetric matrix");
else
printf("Not a Symmetric matrix");
return 0;
}
OUTPUT:
Enter size
3
Enter a 3X3 matrix
113
126
363
Symmetric matrix
Case 2:
Enter size
3
Enter a 3X3 matrix
452
123
421
Not a Symmetric matrix
Source code:
#include<stdio.h>
int main()
{
int a,b,temp,*p1,*p2;
printf("Enter 2 numbers\n");
scanf("%d%d",&a,&b);
p1=&a;
p2=&b;
printf("The value of a and b before swapping %d,%d\n",a,b);
temp=*p2;
*p2=*p1;
*p1=temp;
printf("The value of a and b after swapping %d,%d\n",a,b);
return 0;
}
OUTPUT:
Enter 2 numbers
23
The value of a and b before swapping 2,3
The value of a and b after swapping 3,2
12)Write a recursive function to display the first n terms of the Fibonacci series: 0 1 1 2 3 5 8
13 ….Also write the main program.
Source code:
#include<stdio.h>
int main()
{
int n,fibo(int),i=0,c;
printf("Enter a number\n");
scanf("%d",&n);
for(c=1;c<=n;c++)
{
printf("%d\t",fibo(i));
i++;
}
}
int fibo(int n)
{
if(n==0)
return 0;
else if(n==1)
return 1;
else
return (fibo(n-1)+fibo(n-2));
}
OUTPUT:
Enter a number
10
0 1 1 2 3 5 8 13 21 34
Source code:
#include<stdio.h>
int main()
{
int a,b,large(int *,int *),big;
printf("Enter 2 numbers\n");
scanf("%d%d",&a,&b);
big=large(&a,&b);
printf("The largest number is %d",big);
}
large(int *a,int *b){
if(*a>*b)
return *a;
else
return *b;
}
OUTPUT:
Enter 2 numbers
2
3
The largest number is 3
Source code:
#include<stdio.h>
int main()
{
int a,b,swap(int *,int *);
printf("Enter 2 numbers\n");
scanf("%d%d",&a,&b);
printf("The value of a and b before swapping %d,%d\n",a,b);
swap(&a,&b);
printf("The value of a and b after swapping %d,%d\n",a,b);
}
swap(int *a,int *b)
{
int temp;
temp=*b;
*b=*a;
*a=temp;
}
OUTPUT:
Enter 2 numbers
23
The value of a and b before swapping 2,3
The value of a and b after swapping 3,2
return 0;
}
OUTPUT:
Enter size
5
Enter 5 elements
1
2
3
4
5
The elements in the array are:
1
2
3
4
5