0% found this document useful (0 votes)
25 views21 pages

1st B.SC (C) Edited

Uploaded by

SRDC CDE
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views21 pages

1st B.SC (C) Edited

Uploaded by

SRDC CDE
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 21

1

PROGRAMMING IN C LAB

1. Find out the perfect number using c program.


2. Write a C program to check whether a number is Armstrong or not.
3. Write a C program to find the sum of individual digits of a positive
integer.
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.
5. Write a C program to generate the first n terms of the sequence.
6. Write a C program to generate all the prime numbers between 1
and n, where n is a value supplied by the user.
7. Write a C program to find both the largest and smallest number in
a list of integers.
8. Write a C program that uses functions to perform the following:
a. Addition of Two Matrices
b. Multiplication of Two Matrices
9. Write a program to perform various string operations
10.Write C program that implements searching of given item in given
list
11. Write a C program to sort a given list of integers in ascending
order
1.Perfect number

1. Find out the perfect number using c program.

Aim: Finding the perfect number using c program.


Source code:

#include<stdio.h>
int main(){
int n,i=1,sum=0;

printf("Enter a number: ");


scanf("%d",&n);

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.

2.Write a C program to check whether a number is Armstrong or not.


Aim: to check whether a number is Armstrong or not using C programing .
Source code:

#include<stdio.h>

#include<conio.h>

#include<math.h>

void main()

int num, sum=0,r,n;

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)

Printf(“%d is a Armstrong number”,num);

else

Printf(“%d is not a Armstrong number”,num);


return 0;

Out put:

Enter a number:432

432 is not an Armstrong number.

3.Find the sum of individual digits

3.Write a C program to find the sum of individual digits of a positive integer.


Aim: to find the sum of individual digits of a positive integer using C programing .
Source code:

#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.

Aim: To print the Fibonacci series for 1 to n value using C programing .


Source code:

#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

5.generate the first n terms:


5.Write a C program to generate the first n terms of the sequence.
Aim: to generate the first n terms of the sequence by using c program.

Source code:

#include<stdio.h>

#include<conio.h>

Int main()

int i=0,n,arr[20];

Clrscr();

Printf(“\n enter the number of elements:”);

Scanf(“%d”,&n);

for(i=0;i<n;i++)

Printf(“\n arr[%d]=”,i);

Scanf(“%d”,&arr[i]);

Printf(“\n the array elements are”);

For(i=0;i<n;i++)
Printf(“arr[%d]=%d\t”,I,arr[i]);

Return 0;

Output:

Enter the number of arrays:5

Arr[0]=1

Arr[1]=2

Arr[3]=3

Arr[4]=4

Arr[5]=5

The array elements are

Arr[0]=1 Arr[1]=2 Arr[3]=3

Arr[4]=4 Arr[5]=5

6.Generate all the prime numbers


6.Write a C program to generate all the prime numbers between 1 and n,
where n is a value supplied by the user.

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

7.Find both the largest and smallest number


7.Write a C program to find both the largest and smallest number in a list
of integers.
Aim: to find both the largest and smallest number in a list of integers using
C programing .
Source code:

#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

i.aim: Comparison of two strings by using c program.

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();

printf(“\n enter the first string :”);

gets(str1);

printf(“\n enter the second string :”);

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;

printf(“\n the two strings are equal”);

}
}
if(len!=len2)

printf(“\n the two strings are not equal”);

if(same==0)

if(str1[i]>str2[i])

printf(“\n string1 is greater than string2”);

else if(str[i]<str2[i])

printf(“\n string 2 is greater than string1”);

getch();

return 0;

Output:

Enter the string:hello

Enter the second string:hello


The two strings are equal

ii. Concatenate of two strings

source code:

#include<stdio.h>

#include<conio.h>

int main()

char str1[100],str2[100],str3[100];

int i=0,j=0;

clrscr();

printf(“\n enter the first string:”);

gets(str1);

printf(“\n enter second string:”);

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’;

printf(“\n the concatenated string is:”);

puts(str3);

getch();

return 0;

Output:

Enter the first string: hello,

Enter the second string: how are you?

The concatenated string is :hello,how are you?

iii.Length of two strings:

source code:

#include<stdio.h>

#include<conio.h>

int main( )

char str[100],i=0,lenth;

clrscr();

printf(“\n enter the string:”);

gets(str);

while(str[i]!= ‘\0’)
i++;

length=i;

printf(“\n the length of the string is :%d”,length);

getch();

Output:

Enter the string: hello

The length of the string is: 5

10.searching of given item


10.Write C program that implements searching of given item in given list
Aim: to implement searching of given item in given list using c program.
Source code:
#include<stdio.h>
main()
{
int a[20],i,j,d,t,x,l=0,low,mid,high;
printf("\nEnter the number of elements\n");
scanf("%d",&d);
printf("Enter the numbers\n");
for(i=0;i<d;i++)
scanf("%d",&a[i]);
for(i=0;i<d;i++)
{
for(j=i+1;j<d;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
printf("\nThe sorted list :");
for(i=0;i<d;i++)
printf("%d ",a[i]);
printf("\nEnter the number to be searched\n");
scanf("%d",&x);
low=0;
high=d-1;
while(low<=high)
{
mid=(low+high)/2;
if(x<a[mid])
high=mid-1;
else if(x>a[mid])
low=mid+1;
else
{
if(x==a[mid])
{
l++;
printf("The item %d is found at location %d\n",x,mid+1);
exit(0);
}
}
}
if(l==0)
printf("Item not found\n");
}

Output:
11.Ascending order
11. Write a C program to sort a given list of integers in ascending order

Aim: to sort a given list of integers in ascending order using C program


Source code:
# include<stdio.h>
# include<conio.h>
main ( )
{
int a, b, c, d, e, Sum = 0, i;
clrscr( ) ;
printf(“ \n Enter five Numbers:”);
scanf( “ %d %d %d %d %d %d”, &a, &b, &c, &d, &e);
printf( “ \ n Numbers in Ascending Order is :”)
Sum = a + b + c + d + e;
for(i=1; i<=Sum; i++)
{
if(i = = a || i = =b || i = = c || i = = d || i = = e)
{
printf(“ %3d “,i);
}
}
getch();
}

Output: Enter five numbers: 5 8 7 4 1

Numbers in Ascending Order is : 1 4 5 7 8

You might also like