0% found this document useful (0 votes)
26 views31 pages

Lab External Programs

Uploaded by

gakoy96423
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)
26 views31 pages

Lab External Programs

Uploaded by

gakoy96423
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/ 31

*print a message if n1 is factor of n2:-

#include<stdio.h>

#include<conio.h>

void main()

int n1,n2;

printf("enter n1,n2\n");

scanf("%d%d",&n1,&n2);

if(n2%n1==0)

printf("%d is factor of %d\n",n1,n2);

else

printf("it is not factor\n");

Output:

Enter n1,n2

15 45

15 is a factor of 45
Program:

#include<stdio.h>

#include<conio.h>

void main()

char ch='a';

if(!ch);// if condition is terminated so it cannot be checked, so it prints rest of


the lines

printf("one\n");

printf("two\n");

printf("three\n");

printf("four\n");

Output:

One

Two

Three

four
*print the result of student:-

#include<stdio.h>

main( )

int m1,m2,m3;

printf(“enter m1,m2,m3\n”);

scanf(“%d%d%d”,&m1,&m2,&m3);

if(m1>=40 &&m2>=40 &&m3>=40)

printf(“student is pass\n”);

else

Printf(“student is fail\n”);

Output:

Enter m1,m2,m3

M1=97 m2=92 m3=95

Student is pass
Program:

#include<stdio.h>

#include<conio.h>

void main()

char ch;

printf("enter value of n\n");

scanf("%c",&ch);

switch(ch)

case 'a':

printf("vowel\n");

break;

case 'e':

printf("vowel\n");

break;

case 'i':

printf("vowel\n");

break;

case 'o':

printf("vowel\n");

break;

case 'u':

printf("vowel\n");
break;

default:

printf("not vowel\n");

break;

getch();

clrscr();

Output:

Enter ch

Vowel

*write a program to print 1 to n

main( )

Int i=1,n;

Printf(“enter n\n”);

Scanf(“%d”,&n);

While(i<=n)

Printf(“%d\t”,i);

i=i++;

Printf(“while loop is tested\n”);


}

Output:

Enter n

12345

While loop is tested.

*write a program to find factorial of given number:-

#include<stdio.h>

void main()

int n,fact=1;

printf("enter number\n");

scanf("%d",&n);

while(n>0)

fact=fact*n;

n--;

printf("factorial=%d\n",fact);

Output:

Enter number:

Factorial =24
//FACTORS OF GIVEN NUMBER

#include<stdio.h>

void main()

int n,i=1;

printf("enter number\n");

scanf("%d",&n);

while(i<=n)

if(n%i==0)

printf("factor is %d\n",i);

i++;

Output:

enter number

100

factors are

5
10

20

25

50

100

*To print the number in reverse order:-

//PRINT NUMBER IN REVERSE ORDER

//REVERSE OF NUMBER

#include<conio.h>

#include<stdio.h>

void main()

int n,rev;

rev=0;

printf("enter no\n");

scanf("%d",&n);

while(n>0)

int r=n%10;

rev=rev*10+r;

n=n/10;

printf("REVERSE OF NUMBER IS %d\n",rev);

getch();
clrscr();

Output:

Enter no

356

Reverse number is 653.

FIBONACCI SERIES

#include<stdio.h>

void main()

int a,b,n,c,i=1;

a=0,b=1;

printf("how many fibonacci series want\n");

scanf("%d",&n);

printf("%d",a);

while(i<=n)

c=a+b;

printf("%d",c);

a=b;

b=c;

i++;

}
}

Output:

How many Fibonacci series want

01235813

GCD,LCM OF TWO NUMBERS

#include<stdio.h>

void main()

int i,x,y,j,gcd,lcm;

printf("enter x,y\n");

scanf("%d%d", &x,&y);

i=1,j=(x<y)?x:y;

while(i<=j)

if(x%i==0 && y%i==0)

gcd=i;

i=i+1;

printf("the LCD is %d\n",gcd);

lcm=x*y/gcd;

printf("LCM is %d\n",lcm);

getch();

clrscr();
}

Output:

Enter x,y

6 15

The LCD is 3

LCM is 30

check whether given number is prime or not

#include<stdio.h>

#include<conio.h>

void main()

int n,i,count;

printf("enter n");

scanf("%d",&n);

i=1,count=0;

while(i<=n)

if(n%i==0)

count=count+1;

i++;

if(count==2)
printf("%d is prime\n"); //what is the problem in this line

else

printf("%d is not prime\n",n);

getch();

clrscr();

Output:

Enter n

13

13 is prime

check whether given number is armstrong or not

//sum of powers of digits == given number

//ex:153

#include<stdio.h>

void main()

int n,rem,sum,x;

printf("enter number\n");

scanf("%d",&n);

x=n,sum=0;

while(n>0)

rem=n%10;

printf("remainder=%d\n",rem);
sum=sum+(rem*rem*rem);

printf("sum=%d\n",sum);

n=n/10;

printf("remaining number=%d\n",n);

printf("total sum of all digits=%d\n",sum);

printf("given number =%d\n",x);

if(sum==x)

printf("given number is armstrong number\n");

Output:

Enter n

153

The given number is Armstrong.

check whether given number is Strong or not

//sum of factorial of digits of a number == original number

//ex:145

#include<stdio.h>

void main()

int n,sum,r,t,i,fact;

printf("enter n\n");

scanf("%d",&n);

t=n;
sum=0;

while(t>0)

i=1,fact=1;

r=t%10;

printf("digit=%d\n",r);

while(i<=r)

fact=fact*i;

printf("factorial=%d\n",fact);

i++;

sum=sum+fact;

printf("sum of factorial=%d\n",sum);

t=t/10;

printf("remaining number=%d\n",t);

if(sum==n)

printf("%d is strong number\n",n);

else

printf("%d is not strong number\n",n);

Output:
Enter n

123

123 is not strong

PERFECT NUMBER

//sum of divisors of given number

#include<stdio.h>

void main()

int n,i,sum;

printf("enter number\n");

scanf("%d",&n);

i=1,sum=0;

while(i<=n)

if(n%i==0)

sum=sum+i;

i++;

if(n==sum)

printf("%d is perfect number\n",n);

else

printf("%d is not perfect number\n",n);


}

Output:

Enter n

6 is perfect no

sum of divisors of given number

#include<stdio.h>

void main()

int n,i,sum;

//printf("enter number\n");

n=6;

//scanf("%d",&n);

i=1,sum=0;

while(i<=n)

if(n%i==0)

sum=sum+i;

i++;

if(n==sum)

printf("%d is perfect number\n",n);


else

printf("%d is not perfect number\n",n);

WAP TO PRINT NATURAL NUMBERS

#include<stdio.h>

void main()

for(int i=1;i<10;i++)

printf("%d\n",i);

getch();

clrscr();

Output:

123456789

*write a program to print from n to 1:-

#include<stdio.h>

void main()

int n,i;

printf("enter n\n");

scanf("%d",&n);

for(i=n;i>0;i--)
{

printf("%d\t",i);

Output:

Enter n

4321

Nested loop:-

#include<stdio.h>

void main()

int i,j;

for(i=1;i<=3;i++)

for(j=0;j<=3;j++)

printf("%d\t",j);

Output:

10123

20123
30123

Sum of n numbers:-

//SUM OF ENTERED NUMBERS

#include<stdio.h>

void main()

int n,i,x,sum=0;

printf("enter n\n");

scanf("%d",&n);

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

printf("enter x\n");

scanf("%d",&x);

sum=sum+x;

printf("sum of n nos%d\n",sum);

Output:

Enter n

Enter x
1

Enter x

Enter x
3

Enter x

Sum of n nos 10.

Program:

#include<stdio.h>

void main()

int i,j,n;

printf("enter number of elements\n");

scanf("%d",&n);

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

for(j=1;j<=n;j++)

printf("*\t");

printf("\n");

Output:

Enter no of elements

*
**

***

****

*Product of 3 numbers using function

#include<stdio.h>

main( )

int pro(int,int,int);

int a=10,b=5,c=20,product;

product=pro(a,b,c);

printf(“product=%d\n”,product);

int pro(int x,int y,int z)

int p;

p=x*y*z;

return p;

*write a function to return sum of three numbers:-

main()

int fn(int x,int y,int z);

int a=10,b=20,c=30;

int d;
d=fn(a,b,c);

printf(“sum is %d\n”,d);

int fn(int a,int b,int c)

int sum;

sum=a+b+c;

return sum;

Output:

Sum is 60.

*write a function to return area, circumference of a circle

#define pi 3.14

main( )

float circum(float);

float area(float);

float rad;

printf(“enter radius\n”);

scanf(“%f”,&rad);

printf(“area is :%f\n”,circum(rad));

float area(float r)

return(pi*r*r);
}

float circum(float r)

return(2*pi*r);

Area&circumference of a rectangle:

main()

float circum(float,float);

float area(float,float);

float leng,breadth;

printf(“enter length,breadth\n”);

printf(“area is:%f\n”,area(length,breadth));

printf(“circumference is %f\n”,circum(leng,breadth));

float area(float l,float b)

return(l*b);

float circum(float l,float b)

return(2*(l+b));

}
GCD of no using function:-

main()

int x,y,t;

printf(“enter values of x,y\n”);

scanf(“%D%D”,&x,&y\n”);

while(1)

if(x>y)

x%y,t;

if(t==0)

Printf(“GCD is %d\n”,y);

exit(0);

else

x=y;

y=t;

else if(x<y)

{
y%x=t;

if(t==0)

printf(“GCD is %d\n”,x);

exit(0);

else

y=x;

x=t;

else

printf(“GCD is 1 \n”);

exit(0);

WAP To print the elements of the array:

#include<stdio.h>

void main()

int a[5];

for(int i=0;i<5;i++)
{

printf("enter value of element for %d",i);

scanf("%d",&a[i]);

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

printf("given value of element %d is %d\n",i,a[i]);

\\WAP COUNT NUMBER OF ELEMENTS IN ARRAY


#include<stdio.h>
#include<conio.h>
void main()
{
int a[5],i,cnt=0;
a[0]=1,a[1]=2,a[2]=NULL,a[3]=NULL,a[4]=5;
for(i=0;i<5;i++)
{
if(a[i]!=NULL)
cnt++;
}
printf("count=%d\n",cnt);
getch();
clrscr();
}

WAP SUM, AVERAGE OF ARRAY OF ELEMENTS

#include<stdio.h>

void main()

int n,a[5],sum=0,avg=0;

printf("how many elements\n");


scanf("%d",&n);

for(int i=0;i<5;i++)

printf("enter value of element for %d",i);

scanf("%d",&a[i]);

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

printf("given value of element %d is %d\n",i,a[i]);

sum+=a[i];

avg=sum/n;

printf("sum=%d\n",sum);

printf("avg=%d\n",avg);

getch();

clrscr();

Output:

How many elements

Enter value of element

2.5

3.7

10.6
1

Sum=17.80

Average=4.45

//WAP TO PRINT TWO DIMENSIONAL ARRAY

#include<stdio.h>

void main()

int x[3][4]={{1,2,3,4},{5,6,7,8},{9,10,11,12}};

int i,j;

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

for(j=0;j<4;j++)

printf("%d\t",x[i][j]);

printf("\n");

getch();

clrscr();

WAP Program On strings:

#include<stdio.h>

#include<string.h>
void main()

char s1[]="abcd";

char s2[]="abcd";

int i;

i=strcmp(s1,s2);

if(i==0)

printf("strings are equal");

else if(i>1)

printf("string1 > string2");

else

printf("string1 < string2");

getch();

clrscr();

UPPER AND LOWER OF STRING WITHOUT USING STRLEN

#include<stdio.h>

#include<conio.h>

#include<string.h>

#include<ctype.h>

void main()

char c1='a',c2='b';

printf("%d \n",isupper(c1));
printf("%d \n",islower(c1));

printf("%d \n",isalpha(c1));

printf("%d \n",isspace(c1));

printf("%s \n",tolower(c1));

printf("%s \n",toupper(c1));

getch();

clrscr();

Output
0
512
1024
0
a
A

PROGORAM ON FILES

#include<stdio.h>
int main( ){
FILE *fp;
int i;
//writing
fp = fopen ("num.txt", "w");
for (i =1; i<= 10; i++){
putw (i, fp);
}
fclose (fp);
//reading
fp =fopen ("num.txt", "r");
printf ("file content is");
for (i =1; i<= 10; i++){
i= getw(fp);
printf ("%d",i);
printf("");
}
fclose (fp);
return 0;
}

//copying data from one file to another file


#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<stdio.h>
void main()
{
FILE *f1,*f2;
char ch;
f1=fopen("file1","r"); //source file
f2=fopen("file2","a+"); //a,w : if file is there it opens and
write/appedn.
//if file is not there then it creates before writing data.
if(f1==NULL)
printf("WE cannot open source file ");
else if(f2==NULL)
printf("we cannot open target file\n");
else
{
printf("file1 is opened\n");
//reading data from file1
while((ch=fgetc(f1))!=EOF)
{
fputc(ch,f2);
}
printf("data is copied successfully\n");
fclose(f1);
fclose(f2);
//fcloseall();
}
getch();
clrscr();
}

You might also like