Assignment of C Program
Assignment of C Program
#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
#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
0 1 1 2 3
PROGRAM -3
#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
Number is prime
PROGRAM -4
#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
#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
#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
#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
Reversed Number:3607
PROGRAM – 8
# 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
#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
2
2
2
3
3
2
4
3
4
3
Matrix I
2 3
3 2
Matrix II
4 3
4 3
Resultant Matrix
6 6
7 5
Program – 10
#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
Matrix I
2 3
3 2
Matrix II
3 4
4 3
Resultant Matrix
18 17
17 18
Program – 11
#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
#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
#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
#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
# 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 4 ELEMENTS:
----------------
3
4
6
7