1st B.SC (C) Edited
1st B.SC (C) Edited
PROGRAMMING IN C LAB
#include<stdio.h>
int main(){
int n,i=1,sum=0;
while(i<n){
if(n%i==0)
sum=sum+i;
i++;
}
if(sum==n)
printf("%d is a perfect number",i);
else
printf("%d is not a perfect number",i);
return 0;
}
output:
Enter a number: 6
6 is a perfect number
2.whether a number is Armstrong or not.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
clrscr();
printf(“enter a number:”);
scanf(“%d”,&num);
n=num;
while(n>0)
r=n%10;
sum+=pow(r,3);
n=n/10;
If(sum==num)
else
Out put:
Enter a number:432
#include<stdio.h>
#include<conio.h>
main()
{
int n,s,p;
clrscr();
printf("enter the vaue for n:\n");
scanf("%d",&n);
s=0;
if(n<0)
printf("The given number is not valid");
else
{
while(n!=0) /* check the given value =0 or not */
{
p=n%10;
n=n/10;
s=s+p;
}
printf("sum of individual digits is %d",s);
}
getch();
}
Output:
1.Enter the value for n: 333
Sum of individual digits is 9
2.Enter the value for n: 4733
Sum of individual digits is 17
3. Enter the value for n: -111
The given number is not valid
4.Fibonacci sequence
4. A Fibonacci sequence is defined as follows: the first and second terms in the
sequence are 0 and 1. Subsequent terms are found by adding the preceding two terms
in the sequence.
#include<stdio.h>
void main()
{
int a,b,c,n,i;
clrscr();
printf("enter n value");
scanf("%d",&n);
a=0; b=1;
if(n==1)
printf("%d",a);
else if(n==2)
printf("%d%d",a,b);
else
{ printf("%d%d",a,b);
//LOOP WILL RUN FOR 2 TIME LESS IN SERIES AS THESE WAS
PRINTED IN ADVANCE
for(i=3;i<=n;i++)
{ c=a+b;
printf("%d",c);
a=b; b=c;
}
getch();
}}
Output:
1. Enter n value : 5
01123
2. Enter n value : 7
0112358
3. Enter n value : -6
0 1
Source code:
#include<stdio.h>
#include<conio.h>
Int main()
int i=0,n,arr[20];
Clrscr();
Scanf(“%d”,&n);
for(i=0;i<n;i++)
Printf(“\n arr[%d]=”,i);
Scanf(“%d”,&arr[i]);
For(i=0;i<n;i++)
Printf(“arr[%d]=%d\t”,I,arr[i]);
Return 0;
Output:
Arr[0]=1
Arr[1]=2
Arr[3]=3
Arr[4]=4
Arr[5]=5
Arr[4]=4 Arr[5]=5
Aim: to generate all the prime numbers between 1 and n, using C programing .
Source code:
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,fact,j;
clrscr();
printf("enter the number:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
fact=0;
//THIS LOOP WILL CHECK A NO TO BE PRIME NO. OR NOT.
for(j=1;j<=i;j++)
{
if(i%j==0)
fact++;
}
if(fact==2)
printf("\n %d",i);
}
getch( );
}
Output:
Enter the number : 5
235
Enter the number : 10
2357
Enter the number : 12
1 3 5 7, 11
#include<stdio.h>
void main()
{
int a[10],i,n,min,max;
clrscr();
printf("enter the array size:");
scanf("%d",&n);
printf("Enter the elements of array");
for(i=0;i<n;i++) // read the elements of an array
scanf("%d",&a[i]);
min=a[0];
max=a[0];
for(i=0;i<n;i++)// read the elements of an array
{
if(a[i]<min)// check the condition for minimum value
min=a[i];
if(a[i]>max)//check the condition for maximum value
max=a[i];
}
printf("maximum value is:%d\n",max);
printf("minimum value is:%d\n",min);
getch();
}
Output:
1.enter the array size:4
Enter the elements of array 36 13 2 45
maximum value is:45
minimum value is:2
2.enter the array size:5
Enter the elements of array 6 2 1 3 8
maximum value is:8
minimum value is:1
3.enter the array size:5
Enter the elements of array-6 9 -9 2 5
maximum value is:9
minimum value is:-9
8.Matrices
8.Write a C program that uses functions to perform the following:
a. Addition of Two Matrices
b. Multiplication of Two Matrices
Aim:. functions to perform the following by using C program
c. Addition of Two Matrices
d. Multiplication of Two Matrices
Source code:
#include<stdio.h>
void main()
{
int ch,i,j,m,n,p,q,k,r1,c1,a[10][10],b[10][10],c[10][10];
clrscr();
printf("************************************");
printf("\n\t\tMENU");
printf("\n**********************************");
printf("\n[1]ADDITION OF TWO MATRICES");
printf("\n[2]MULTIPLICATION OF TWO MATRICES");
printf("\n[0]EXIT");
printf("\n**********************************");
printf("\n\tEnter your choice:\n");
scanf("%d",&ch);
if(ch<=2 & ch>0)
{
printf("Valid Choice\n");
}
switch(ch)
{
case 1:
printf("Input rows and columns of A & B Matrix:");
scanf("%d%d",&r1,&c1);
printf("Enter elements of matrix A:\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
scanf("%d",&a[i][j]);
}
printf("Enter elements of matrix B:\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
scanf("%d",&b[i][j]);
}
printf("\n =====Matrix Addition=====\n");
for(i=0;i<r1;i++)
{
For(j=0;j<c1;j++)
printf("%5d",a[i][j]+b[i][j]);
printf("\n");
}
break;
case 2:
printf("Input rows and columns of A matrix:");
scanf("%d%d",&m,&n);
printf("Input rows and columns of B matrix:");
scanf("%d%d",&p,&q);
if(n==p)
{
printf("matrices can be multiplied\n");
printf("resultant matrix is %d*%d\n",m,q);
printf("Input A matrix\n");
read_matrix(a,m,n);
printf("Input B matrix\n");
read_matrix(b,p,q);
printf("\n =====Matrix Multiplication=====\n");
for(i=0;i<m;++i)
for(j=0;j<q;++j)
{
c[i][j]=0;
for(k=0;k<n;++k)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
printf("Resultant of two matrices:\n");
write_matrix(c,m,q);
}
else
{
printf("Matrices cannot be multiplied.");
}
break;
case 0:
printf("\n Choice Terminated");
exit();
break;
default:
printf("\n Invalid Choice");
}
getch();
}
int read_matrix(int a[10][10],int m,int n)
{
int i,j;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
return 0;
}
int write_matrix(int a[10][10],int m,int n)
{
int i,j;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%5d",a[i][j]);
printf("\n");
}
return 0;
}
Output:
1.
************************************
MENU
**********************************
[1]ADDITION OF TWO MATRICES
[2]MULTIPLICATION OF TWO MATRICES
[0]EXIT
**********************************
Enter your choice: 1
Valid Choice
Input rows and columns of A & B Matrix:2
2
Enter elements of matrix A:
2
2
2
2
Enter elements of matrix B:
2
2
2
2
=====Matrix Addition=====
44
44
************************************
MENU
**********************************
[1]ADDITION OF TWO MATRICES
[2]MULTIPLICATION OF TWO MATRICES
[0]EXIT
**********************************
Enter your choice:2
Valid Choice
Input rows and columns of A matrix:2
3
Input rows and columns of B matrix:2
2
Matrices cannot be multiplied.
************************************
MENU
**********************************
[1]ADDITION OF TWO MATRICES
[2]MULTIPLICATION OF TWO MATRICES
[0]EXIT
**********************************
Enter your choice:2
Valid Choice
Input rows and columns of A matrix:2
2
Input rows and columns of B matrix:2
2
matrices can be multiplied
resultant matrix is 2*2
Input A matrix
2
2
2
2
Input B matrix
2
2
2
2
=====Matrix Multiplication=====
Resultant of two matrices:
88
88
9.string operations
9. Write a program to perform various string operations
i. Comparison of two strings
ii. Concatenate of two strings
iii. Length of two strings
Source code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
char str1[50],str2[50];
int i=0,len1=0,len2=0,same=0;
clrscr();
gets(str1);
gets(str2);
len1=strlen(str1);
len2=strlen(str2);
if(len1==len2)
while(i<len1)
{
if(str1[i]==str2[i]);
i++;
else break;
if (i==len1)
same=1;
}
}
if(len!=len2)
if(same==0)
if(str1[i]>str2[i])
else if(str[i]<str2[i])
getch();
return 0;
Output:
source code:
#include<stdio.h>
#include<conio.h>
int main()
char str1[100],str2[100],str3[100];
int i=0,j=0;
clrscr();
gets(str1);
gets(str2);
while(str1[i]!= ‘\0’);
str3[j]=str1[i];
i++;
j++;
i=0;
while(str2[i]!= ‘\0’)
str3[j]=str2[i];
i++;
j++;
str3[j]= ‘\0’;
puts(str3);
getch();
return 0;
Output:
source code:
#include<stdio.h>
#include<conio.h>
int main( )
char str[100],i=0,lenth;
clrscr();
gets(str);
while(str[i]!= ‘\0’)
i++;
length=i;
getch();
Output:
Output:
11.Ascending order
11. Write a C program to sort a given list of integers in ascending order