0% found this document useful (0 votes)
63 views23 pages

Assignment of C Program

The document contains 15 programs written in C programming language. Program 1 converts days into months and days. Program 2 prints the Fibonacci series for a given number of elements. Program 3 checks if a number is prime. The remaining programs demonstrate other basic programming concepts like palindrome checking, factorial, digit sum, number reversal, sorting, matrix addition, matrix multiplication, finding the greatest of three numbers, reversing a string, string concatenation, structures, and counting even and odd numbers in an array.

Uploaded by

Sahil Bhai
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)
63 views23 pages

Assignment of C Program

The document contains 15 programs written in C programming language. Program 1 converts days into months and days. Program 2 prints the Fibonacci series for a given number of elements. Program 3 checks if a number is prime. The remaining programs demonstrate other basic programming concepts like palindrome checking, factorial, digit sum, number reversal, sorting, matrix addition, matrix multiplication, finding the greatest of three numbers, reversing a string, string concatenation, structures, and counting even and odd numbers in an array.

Uploaded by

Sahil Bhai
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/ 23

PROGRAM– 1

/*Write a C program to convert a given number of days into


months and days*/

#include<stdio.h>
#include<conic.h>
Void main()
{
Int months ,days;
Printf(“Enter the days”);
Scanf(“%d”,&days);
Months= days/30;
Days=days%30;
Printf(“months=%d days=%d”,months,days);
Getch();
}

OUTPUT

Enter the days


30
Months=1 days=0
PROGRAM – 2

/*Write a C program to print Fibonacci series*/

#include<stdio.h>
#include<conio.h>
void main()
{
int n1=0,n2=1,n3,i,number;
printf("Enter the number of elements:");
scanf("%d",&number);
printf("\n%d %d",n1,n2);//printing 0 and 1
for(i=2;i<number;++i)//loop starts from 2 because 0 and 1
are already printed
{
n3=n1+n2;
printf(" %d",n3);
n1=n2;
n2=n3;
}
getch();
clrscr();
}

OUTPUT

Enter the number of elements:5

0 1 1 2 3
PROGRAM -3

/*Write a C program to check prime number.*/

#include<stdio.h>
void main()
{
int n,i,m=0,flag=0;
printf("Enter the number to check prime:");
scanf("%d",&n);
m=n/2;
for(i=2;i<=m;i++)
{
if(n%i==0)
{
printf("Number is not prime");
flag=1;
break;
}

OUTPUT

Enter the number to check prime:73

Number is prime
PROGRAM -4

/* WRITE A c PROGRAM TO CHECK PALINDROME NUMBER.*/

#include<stdio.h>
#include<conio.h>
void main()
{
int n,r,sum=0,temp;
printf("enter the number=");
scanf("%d",&n);
temp=n;
while(n>0)
{
r=n%10;
sum=(sum*10)+r;
n=n/10;
}
if(temp==sum)
printf("palindrome number ");
else
printf("not palindrome");
getch();
clrscr();
}
}
if(flag==0)
printf("Number is prime");
}

OUTPUT

enter the number= 7


not palindrome
number is prime
PROGRAM – 5

/*WRITE A C PROGRAM TO PRINT FACTORIAL OF A NUMBER*/

#include<stdio.h>
#include<conio.h>
void main()
{
int i,fact=1,number;
printf("Enter a number: ");
scanf("%d",&number);
for(i=1;i<=number;i++){
fact=fact*i;
}
printf("Factorial of %d is: %d",number,fact);
getch();
clrscr();
}

OUTPUT

Enter a number: 6
Factorial of 6 is: 720
PROGRAM -6

/*WRITE A C PROGRAM TO PRINT SUM OF DIGITS*/

#include<stdio.h>
#include<conio.h>
void main()
{
int n,sum=0,m;
printf("Enter a number:");
scanf("%d",&n);
while(n>0)
{
m=n%10;
sum=sum+m;
n=n/10;
}
printf("Sum is=%d",sum);
getch();
clrscr();
}

OUTPUT

Enter a number:66
Sum is=12
PROGRAM – 7

/* WRITE A C PROGRAM TO REVERSE GIVEN NUMBER*/

#include<stdio.h>
#include<conio.h>
void main()
{
int n, reverse=0, rem;
printf("Enter a number: ");
scanf("%d", &n);
while(n!=0)
{
rem=n%10;
reverse=reverse*10+rem;
n/=10;
}
printf("Reversed Number: %d",reverse);
getch();
clrscr();
}

OUTPUT

Enter a number: 7063

Reversed Number:3607
PROGRAM – 8

/* WRITE A C PROGRAM TO SHOT THE NUMBER IN ASCENDING


ORDER USING BUBBLE SORT METHOD*/

# include<stdio.h>
# include<conio.h>
void main()
{
int i,n,j,t,a[10];
clrscr();
printf("\n ENTER THE NUMBER OF ELEMENTS: ");
scanf("%d",&n);
printf("\n ENTER THE NUMBERS");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
printf("\n SORTED ORDER");
for(i=0;i<n;i++)
printf("%d",a[i]);
getch();
}
OUTPUT

ENTER THE NUMBER OF ELEMENTS: 5


ENTER THE NUMBERS
5
4
3
2
1
SORTED ORDER 12345
PROGRAM -9

/*WRITE A C PROGRAM TO ADD A MATRIC*/

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,m,n,a[3][3],b[3][3],c[3][3];
clrscr();
printf("\n\t\tMatrix Addition");
printf("\n\tEnter The No.Of Rows & Columns");
scanf("%d%d",&m,&n);
printf("\nEnter the elements of Matrix I");
for(i=1;i<=m;i++)
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
printf("\nEnter the elements of Matrix II");
for(i=1;i<=m;i++)
for(j=1;j<=n;j++)
scanf("%d",&b[i][j]);
printf("\nMatrix I\n\n");
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
printf("\t%d",a[i][j]);
printf("\n");
}
printf("\nMatrix II\n\n");
for(i=1;i<=m;i++)

{
for(j=1;j<=n;j++)
printf("\t%d",b[i][j]);
printf("\n");
}
printf("\nResultant Matrix\n\n");
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
c[i][j]=a[i][j]+b[i][j];
printf("\t%d",c[i][j]);
}
printf("\n");
}
getch();
}

OUTPUT

Enter The No.Of Rows & Columns:

2
2

Enter the elements of Matrix I:

2
3
3
2

Enter the elements of Matrix II:

4
3
4
3
Matrix I
2 3
3 2

Matrix II
4 3
4 3

Resultant Matrix
6 6
7 5
Program – 10

/*WRITE A C PROGRAM FOR MULTIPLICATION OF MATRICES*/

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k,m,n,o,p,a[3][3],b[3][3],c[3][3];
clrscr();
printf("\n\t\tMatrix Multiplication");
printf("\n\tEnter The No.Of Rows & Columns of Matrix I");
scanf("%d%d",&m,&n);
printf("\n\tEnter the no. of rows & columns of matrix
II");
scanf("%d%d",&o,&p);
if(n==o)
{
printf("\nEnter the elements of Matrix 1");
for(i=1;i<=m;i++)
for(j=1;j<=n;j++)
scanf("%d",&a[i][j]);
printf("\nEnter the elements of Matrix 2");

for(i=1;i<=o;i++)
for(j=1;j<=p;j++)
scanf("%d",&b[i][j]);
printf("\nMatrix I\n\n");

for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
printf("\t%d",a[i][j]);
printf("\n");
}
printf("\nMatrix II\n\n");
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
printf("\t%d",b[i][j]);
printf("\n");
}
printf("\nResultant Matrix\n\n");
for(i=1;i<=m;i++)
{
for(j=1;j<=p;j++)
{
c[i][j]=0;
for(k=1;k<=n;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
printf("\t%d",c[i][j]);
}
printf("\n");
}
}
else
{
printf("\n\tMatrix multiplication is not possible");
}
getch();
}

OUTPUT

Enter The No.Of Rows & Columns of Matrix I : 2


2
Enter The No.Of Rows & Columns of Matrix II : 2
2

Enter the elements of Matrix 1:


2
3
3
2
Enter the elements of Matrix 2:
3
4
4
3

Matrix I

2 3
3 2
Matrix II

3 4
4 3

Resultant Matrix

18 17
17 18
Program – 11

/*WRITE A PROGRAM TO FIND GREATEST OF THREE NUMBER*/

#include<stdio.h>
#include<conio.h>
void main( )
{
int a,b,c;
clrscr();
printf("Enter three values\n");
scanf("%d%d%d",&a,&b,&c);
printf("\nThe largest value is:");
if(a>b)
{
if(a>c)
printf("%d\n",a);
else
printf("%d\n",c);
}
else
{
if(c>b)
printf("%d\n",c);
else
printf("%d\n",b);
}
getch();
}
OUTPUT
Enter three value
333
344
888
The largest value is :888
Program – 12

/*WRITE A C PROGRAM TO REVERSE A STRING*/

#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
char x[20];
clrscr();
printf("Enter the string:");
gets(x);
printf("The reversed string is : %s",strrev(x));
getch();
}

OUTPUT
Enter the string: 7095
The reversed string is :5907
Program – 13

/* Write a C Program to concatenate a string using string


function*/

#include<stdio.h>
#include<string.h>
void main()
{
char a[10],b[10];
printf("Enter a string:");
gets(a);
printf("Enter another string:");
gets(b);
printf("The concatenated string is: %s ",strcat(a,b));
getch();
}

OUTPUT
Enter a string : dijen
Enter another string :dra
The concatenated string is:dijendra
Program – 14

/*Write a C Program to ILLUSTRATE structure*/

#include <stdio.h>
#include<conio.h>
struct student
{
char name[50];
int roll;
float marks;
}
void main()
{
struct student s;
clrscr();
printf("Enter The Information of Students :\n\n");
printf("Enter Name : ");
scanf("%s",s.name);
printf("Enter Roll No. : ");
scanf("%d",&s.roll);
printf("Enter marks : ");
scanf("%f",&s.marks);
printf("\nDisplaying Information\n");
printf("Name: %s\n",s.name);
printf("Roll: %d\n",s.roll);
printf("Marks: %.2f\n",s.marks);
getch();
}

OUTPUT

Enter The Information of Students


Enter Name: Dijendra
Enter Roll No:10 B21BCA
Enter marks:99
Program – 15

/* Write a C Program to count even and odd numbers using


One-Dimensional Array */

# include<stdio.h>
# include<conio.h>
void main()
{
int a[20],n;
int i,odd_count=0,even_count=0;
clrscr();
printf("\n ENTER THE NUMBER OF ELEMENTS:");
printf("\n -----------------------------\n");
scanf("\n %d",&n);
printf("\n ENTER %d ELEMENTS:",n);
printf("\n ------------------\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n;i++)
{
if((a[i] % 2)== 0)
even_count++;
else
odd_count++;
}
printf("\n NUMBER OF EVEN ELEMENTS = %d",even_count);
printf("\n NUMBER OF ODD ELEMENTS = %d",odd_count);
getch();
clrscr();
}
OUTPUT

ENTER THE NUMBER OF ELEMENTS: 4


-----------------------------

ENTER 4 ELEMENTS:
----------------
3
4
6
7

NUMBER OF EVEN ELEMENTS:2

NUMBER OF ODD ELEMENTS: 2

You might also like