Anda di halaman 1dari 12

Program to check entered number is odd or even

#include< stdio.h >


#include< conio.h >
void main()
{
int x,y;
clrscr();
printf(" \nEnter the Number :\n");
scanf("%d",&x);
switch(x)
{
Case 0:
printf(" \n Number is even :\n");
break;
Case 1:
printf(" \n Number is odd :\n");
break;
default:
y=x%2;
switch (y)
{
Case 0:
printf(" \n Number is even :\n");
break;
default :
printf(" \n Number is odd :\n");
}
}
Getch();
}
Or
#include< stdio.h >
#include< conio.h >
#include<stdlib.h>

void main()
{
int x;
clrscr();
printf(" \nEnter the Number :\n");
scanf("%d",&x);
if(x%2==0)
goto even;
else
goto odd;
even:
printf(" \n %d is Even number :\n");
return;
odd:
printf(" \n %d is odd number:\n");
}
write a program to find out whether a given number is prime or not.
Void main()
{
int a,c=0,i,n;
printf("enter the number to be checked");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
a=n%i;
if(a=0)
{
c=c+1;
}
}
if (c=2)
{

printf("the given number is prime"); }


else
printf("the given number is not prime");
}
or
#include<stdio.h>
#include<conio.h>
void main()
{
int no,i;
clrscr();
printf(" Enter the Number U want to check:");
scanf("%d",&no);
i=2;
while(i<=no-1)
{
if(no%i==0)
{
printf(" %d is not Prime number",no );
break;
}
i=i+1;
}
if(no==i)
printf("%d is a prime Number",no);
getch();
}
Or
main()
{
int a,c,i,n;
printf("enter the number to be checked");
scanf("%d",&n);
if(i=1&&i<=n&&i++)
a=n%i;
if(a=0)
{
c=c+1;

}
}
if (c=2)
printf("the given number is prime");
else
printf("the given number is not prime");
}
Program to perform addition and subtraction of two matrices whose order
are upto 10x10
#include< stdio.h >
#include< conio.h >
void main()
{
Int i,j,row1,col1,a[10][10],b[10][10];
clrscr();
printf ("Enter the order of the matrices upto 10x10: \n\n);
scanf(%d%d,&row1,&col1);
printf ("Enter the elements of the matrix A:\\n\n);
for(i=0;i < row1;i++)
{
for(j=0;j < col1;j++)
scanf(%d,&a[i][j]);
}
printf ("Enter the elements of the matrix B:\\n\n);
for(i=0;i < row1;i++)
{
for(j=0;j < col1;j++)
scanf(%d,&b[i][j]);
}
printf ("Matrix Addition: \n\n);
for(i=0;i < row1;i++)
{
for(j=0;j < col1;j++)
printf(%6d\t,a[i][j]+b[i][j]);
printf(\n);

}
printf ("Matrix Subtraction: \n\n);
for(i=0;i < row1;i++)
{
for(j=0;j < col1;j++)
printf(%6d\t,a[i][j]-b[i][j]);
printf(\n);
}
Getch();
}

Matrix Multiplication
#include<stdio.h>
#include<conio.h>
void main()
{
int a[2][2],b[2][2],c[2][2],i,j,k;
clrscr();
printf("\n\n enter the element of first matrix\n\n");
for(i=0;i<2;i++)
for(j=0;j<2;j++)
{
printf("\na%d%d:",i,j);
scanf("%d",&a[i][j]);
}
printf("\n\n enter the element of second matrix \n\n");
for(i=0;i<2;i++)
for(j=0;j<2;j++)
{
printf("\nb%d%d:",i,j);
scanf("%d",&b[i][j]);
}
printf("\n\nmultiplication of matrix will be \n\n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{

c[i][j]=0;
for(k=0;k<2;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
printf("%d\t",c[i][j]);
}
printf("\n");
}
getch();
}
Compute factorial of any number using do while loop and also while loop
#include< stdio.h >
#include< conio.h >
void main()
{
int a,fact=1
clrscr();
printf(" \nEnter the Number :\n");
scanf("%d",&a);
do {
printf(%d*,a);
fact=fact*a;
a--;
} while(a>=1);
printf(\t=%d,fact);
printf(" \nFactorial of the Number is %d\n",fact);
}

Or

#include< stdio.h >


#include< conio.h >
void main()
{
int a,fact=1
clrscr();
printf(" \nEnter the Number :\n");
scanf("%d",&a);
while(a>=1)
{
printf(%d*,a);
fact=fact*a;
a--;
}
printf(\t=%d,fact);
printf(" \nFactorial of the Number is %d\n",fact);
}
Write a c program to find the roots of a quadratic equation?

/*--------------------------------------------------------------------------PROGRAM TO FIND THE ROOTS OF A QUADRATIC EQUATION


---------------------------------------------------------------------------*/
#include <stdio.h>
#include <math.h>
#include <conio.h>
int main()
{
int a,b,c;
float disc,deno,x1,x2;
printf("\n-------------------------------------------------------");
printf("\n\n PROGRAM TO FIND THE ROOTS OF A QUADRATIC
EQUATION ");
printf("\n\n-------------------------------------------------------");
printf("\n\n\t ENTER THE VALUES OF a,b,c...");
scanf("%d,%d,%d",&a,&b,&c);
disc = (b*b)-(4*a*c);
deno = 2*a;
if(disc > 0)
{
printf("\n\t THE ROOTS ARE REAL ROOTS");
x1 = (-b/deno)+(sqrt(disc)/deno);
x2 = (-b/deno)-(sqrt(disc)/deno);
printf("\n\n\t THE ROOTS ARE...: %f and %f\n",x1,x2);
}
else if(disc == 0)
{
printf("\n\t THE ROOTS ARE REPEATED ROOTS");
x1 = -b/deno;
printf("\n\n\t THE ROOT IS...: %f\n",x1);
}
else
printf("\n\t THE ROOTS ARE IMAGINARY ROOTS\n");
printf("\n-------------------------------------------------------");
getch();
}
This program prints the Fibonacci series
#include<stdio.h>
#include<conio.h>
void main(void)
{
int i,j,k,n;

clrscr();
i=0;
j=1;
printf("%d %d
",i,j);
for(n=0;n<=5;n++)
{
k=i+j;
i=j;
j=k;
printf("%d ",k);
}
getch();
}
Or
#include <stdio.h>
void main()
{
int OldNum, NewNum, FibNum, MaxNum;
printf("
Generate Fibonacci Numbers till what number? ");
scanf("%d", &MaxNum);
OldNum=0;
NewNum=1;
FibNum = OldNum + NewNum;
printf("%d, %d, %d, ", OldNum, NewNum, FibNum);
for(;;)
{
OldNum = NewNum;
NewNum = FibNum;
FibNum = OldNum + NewNum;
if(FibNum > MaxNum)
{
printf("
");
exit(1);
}
printf("%d, ", FibNum);
}

}
Program to Convert string to upper case and lower case
#include <stdio.h>
#include <conio.h>
void lower_to_upper();
void upper_to_lower();
void main()
{
int n;
clrscr();
printf("Please enter your choice.");
printf("(1) for upper to lower conversion.");
printf("(2) for lower to upper conversion.");
printf("CHOICE: ");
scanf("%d",&n);
switch (n)
{
case 1:
{
printf("Please enter a string in upper case.");
printf("String will be terminated if you press Ctrl-Z.");
printf("STRING: ");
upper_to_lower();
break;
}
case 2:
{
printf("Please enter a string in lower case.");
printf("String will be terminated if you press Ctrl-Z.");
printf("STRING: ");
lower_to_upper();
break;
}
default:
printf("ERROR");
}
printf("HAVE A NICE DAY! BYE.");
getch();
}

void upper_to_lower()
{
int i,j;
char c4[80],c3;
for (i=0;(c3=getchar())!=EOF;i++)
c4[i]=(c3>='A' && c3<='Z')?('a' + c3 -'A'):c3;
printf("The lower case equivalent is ");
for (j=0;j<i;j++)
putchar(c4[j]);
return;
}
void lower_to_upper()
{
int i,j;
char c2[80],c1;
for (i=0;(c1=getchar())!=EOF;i++)
c2[i]=(c1>='a' && c1<='z')?('A' + c1 -'a'):c1;
printf("The upper case equivalent is ");
for (j=0;j<i;j++)
putchar(c2[j]);
return;
}
Or
#include <ctype.h>
#include <stdio.h>
int main(void)
{
char str[80];
int i;
printf("Enter a string: ");
gets(str);
for( i = 0; str[ i ]; i++)
str[ i ] = toupper( str[ i ] );
printf("%s\n", str); /* uppercase string */
for(i = 0; str[ i ]; i++)

str[i] = tolower(str[ i ]);


printf("%s\n", str); /* lowercase string */
return 0;
}
Or

we have used the library function toupper() provided by the header file
<ctype.h>.
<ctype.h> - This header file is used for testing and converting characters.
#include <ctype.h>
#include <stdio.h>
#include <conio.h>
int main(void) {
char st[40]="hello world";
int i;
for (i = 0; st[i]; i++)
st[i] = toupper(st[i]);
printf("The uppercase of String= %s\n", st);
getch();
return 0;
}

And
#include <ctype.h>
#include <stdio.h>
#include <conio.h>
void main() {
char st[40]="HELLO WORLD";
int i;
for (i = 0; st[i]; i++)
st[i] = tolower(st[ i ]);
printf("The lowercase of String= %s\n", st);
getch();
}

Anda mungkin juga menyukai