Solution List of Programs

CCP LAB

B-1 to B-10 (Sem-I)

Session 2011-12

/*1. ************Print Hello World**********/

#include<stdio.h>

void main()

{

printf(“Hello”);

}

/*2(i).*************Addition of two numbers***********/

#include<stdio.h>

void main()

{

Int a=5,b=10,c;

C=a+b;

printf(“%d”,c);

}

/*2(ii).*************Addition of two numbers by user***********/

#include<stdio.h>

void main()

{

int a,b,c;

printf(“enter the two numbers”);

scanf(“%d%d”,&a,&b);

C=a+b;

printf(“%d”,c);

}

/*3(i).*************swap two numbers using third variable***********/

#include<stdio.h>

void main()

{

int a=5,b=10,c;

c=a;

a=b;

b=c;

printf(“%d%d”,a,b);

}

/*3(ii).*************swap two numbers without using third variable***********/

#include<stdio.h>

void main()

{

int a=5,b=10;

a=a+b;

a=a-b;

b=a-b;

printf(“%d%d”,a,b);

}

/*4.*************Avg Max & min of ten numbers***********/

/*wap to printf average,maximum,minimum of 10 numbers*/

#include<stdio.h>

#include<conio.h>

void main()

{

int a=10,b=20,c=30,d=40,e=50,f=60,g=70,h=80,i=90,j=100,avg,max,min;

clrscr();

avg=a+b+c+d+e+f+g+h+i+j;

printf("avg=%d",avg/10);

/****************maximum********************

if(a>b & a>c & a>d & a>e & a>f & a>g & a>h & a>i & a>j)

{

printf("max no. is a ");

}

else

{

if(b>a & b>c & b>d & b>e & b>f & b>g & b>h & b>i & b>j)

printf("max no. is b ");

else

{

if(c>a & c>b & c>d & c>e & c>f & c>g & c>h & c>i & c>j)

{

printf("max no. is c");

printf("c");}

else

{

if(d>a & d>b & d>c & d>e & d>f & d>g & d>h & d>i & d>j)

printf("max no. is d ");

else

{

if(e>a & e>b & e>c & e>d & e>f & e>g & e>h & e>i & e>j)

{

printf("max no. is e ");

}

else

{

if(f>a & f>b & f>c & f>d & f>e & f>g & f>h & f>i & f>j)

printf("max no. is f ");

}

if(g>a & g>b & g>c & g>d & g>e & g>f & g>h & g>i & g>j)

{

printf("max no. is g ");

}

else

{

if(h>a & h>b & h>c & h>d & h>e & h>f & h>g & h>i & h>j)

printf("max no. is h ");

}

if(i>a & i>b & i>c & i>d & i>e & i>f & i>g & i>h & i>j)

{

printf("max no. is i ");

}

else

{

printf("max no. is j ");

}}}} }

//*************minimum********************

if(a<b & a<c & a<d & a<e & a<f & a<g & a<h & a<i & a<j)

{

printf("min no. is a ");

}

else

{

if(b<a & b<c & b<d & b<e & b<f & b<g & b<h & b<i & b<j)

printf("min no. is b ");

else

{

if(c<a & c<b & c<d & c<e & c<f & c<g & c<h & c<i & c<j)

{

printf("min no. is c");

}

else

{

if(d<a & d<b & d<c & d<e & d<f & d<g & d<h & d<i & d<j)

printf("min no. is d ");

else

{

if(e<a & e<b & e<c & e<d & e<f & e<g & e<h & e<i & e<j)

{

printf("min no. is e ");

}

else

{

if(f<a & f<b & f<c & f<d & f<e & f<g & f<h & f<i & f<j)

{

printf("min no. is f ");

}

if(g<a & g<b & g<c & g<d & g<e & g<f & g<h & g<i & g<j)

{

printf("min no. is g ");

}

else

{

if(h<a & h<b & h<c & h<d & h<e & h<f & h<g & h<i & h<j)

{

printf("min no. is h ");

}

if(i<a & i<b & i<c & i<d & i<e & i<f & i<g & i<h & i<j)

{

printf("min no. is i ");

}

else

{

if(j<a & j<b & j<c & j<d & j<e & j<f & j<g & j<h & j<i)

printf("min no. is j ");

}}}} }

getch();

}

/*5.*************Avg of ten numbers***********/

#include<stdio.h>

#include<conio.h>

void main()

{

int a,b,c,d,e,f,g,h,i,j,avg;

clrscr();

printf("enter first numbers");

scanf("%d",&a);

printf("enter second numbers");

scanf("%d",&b);

printf("enter third numbers");

scanf("%d",&c);

printf("enter fourth numbers");

scanf("%d",&d);

printf("enter fifth numbers");

scanf("%d",&e);

printf("enter six numbers");

scanf("%d",&f);

printf("enter seventh numbers");

scanf("%d",&g);

printf("enter eighth numbers");

scanf("%d",&h);

printf("enter nineth numbers");

scanf("%d",&i);

printf("enter tenth numbers");

scanf("%d",&j);

avg=a+b+c+d+e+f+g+h+i+j;

printf("avg=%d",avg/10);

getch();

}

/*6.*************Division based on the percentage enter by the user***********/

#include<stdio.h>

#include<conio.h>

void main()

{

int a;

printf(“enter percentage of the student”);

scanf(“%d”,&a);

if(a>=75)

{

printf(“Distinction”);

}

else

{

if(a>=60)

{

printf(“First Division”);

}

else

{

if(a>=40)

{

printf(“Second Division”);

}

else

{

printf(“Fail”);

}}}}

getch();

}

/*7.*************number is even or odd***********/

#include<stdio.h>

#include<conio.h>

void main()

{

int num;

printf(“enter number”);

scanf(“%d”,&num);
if (num % 2 == 0)
{
printf(“It's even”);
}
else
{
printf(“ It's odd”);
}

getch();

}

/*8.*************Greatest of three numbers***********/

#include<stdio.h>

#include<conio.h>

void main()

{

int a,b,c;

printf(“Enter First number”);

scanf(“%d”,&a);

printf(“Enter Second number”);

scanf(“%d”&b)

printf(“Enter third number”);

scanf(“%d”,&c);

if(a>b & a>c)

printf(“The greatest number is : %d”,a);

if(b>a & b>c)

printf(“The greatest number is : %d”,b);

else

{

printf(“The greatest number is : %d”,c);

}

getch();

}

/*9.**********************leap year***************/

#include<stdio.h>

#include<conio.h>

void main()

{

int year;

printf("Enter the year: ");

scanf("%d",&year);

if(year%400 ==0 || (year%100 != 0 & year%4 == 0))

{

printf("Year %d is a leap year",year);

}

else

{

printf("Year %d is not a leap year",year);

}

getch();

}

/*10.***********program evaluate add,sub,mul,div based on user choice*******/

#include<stdio.h>

#include<conio.h>

void main()

{

int a,b,c,ch;

clrscr();

printf("enter two numbers");

scanf("%d%d",&a,&b);

printf("Press 1 for addition,2 for subtraction , 3 for multiplication,4 for division,5 for exit");

scanf("%d",&ch);

switch(ch)

{

case 1:c=a+b;

break;

case 2:c=a-b;

break;

case 3:c=a*b;

break;

case 4:c=a/b;

break;

case 5:exit(1);

default:

printf("wrong choice");

exit(1);

}

printf("ans is = %d",c);

getch();

}

/*11.***********Prime number****************/

#include<stdio.h>

#include<conio.h>

void main()

{

int num,i,count=0;

printf("Enter a number: ");

scanf("%d",&num);

for(i=2;i<=num/2;i++){

if(num%i==0){

count++;

break;

}

}

if(count==0 & num!= 1)

printf("%d is a prime number",num);

else

printf("%d is not a prime number",num);

getch();

}

/*12.*******Fibonacci series******/

#include<stdio.h>

#include<conio.h>

void main()

{

int a=0,b=1,c=0;

clrscr();

printf("%d%d",a,b);

while(c<=13)

{

a=c;

c=b;

b=a;

c=c+b;

printf("%d",c);

}

getch();

}

/*13.*******SUM OF SERIES********************/

#include<stdio.h>

#include<conio.h>

void main()

{

int a=14,b=0,c=20;

clrscr();

printf("%d",a);

printf("\n");

while(c<=94)

{

c=20;

b=a;

c=c+b;

a=c;

printf("%d",c);

printf("\n");

}

getch();

}

/*14.************* pyramid***************/

#include<stdio.h>

#include<conio.h>

void main()

{

int i,j;

clrscr();

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

{

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

{

printf("*");

}

printf("\n");

}

getch();

}

/*15(i).****************factorial without recursion******************/

#include<stdio.h>

#include<conio.h>

void main()

{

int a,fact;

clrscr();

printf(“enter number”);

scanf(“%d”,&a);

fact=facto(a);

printf(“factorial value =%d”,fact);

getch();

}

facto(int x)

{

int f=1,i;

for(i=x;i>=1;i--)

{

f=f*I;

}

return(f);

}

/*15(ii).*************factorial with recursion******************/

#include<stdio.h>

#include<conio.h>

void main()

{

int a,fact;

printf(“enter number”);

scanf(“%d”,&a);

fact=rec(a);

printf(“factorial value =%d”,fact);

getch();

}

rec(int x)

{

int f;

if(x==1)

return(1);

else

f=x*rec(x-1);

return(f);

}

/*16. Sum of digits**********************/

#include<stdio.h>

#include<conio.h>

void main()

{

int a,b,c=0;

clrscr();

printf("enter number");

scanf("%d",&a);

while(a>=1)

{

b=a%10;

c=c+b;

a=a/10;

}

printf("the sum of digit is : %d",c);

getch();

}

/*17.***********transpose of matrix****************/

#include<stdio.h>

#include<conio.h>

void main()

{

int a[3][3],i,j;

clrscr();

printf("enter 3X3 matrix");

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

{

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

{

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

}

}

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

{

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

{

printf("%d",a[j][i]);

}

printf("\n");

}

getch();

}

/*18.***********multiplication of matrix****************/

#include<stdio.h>

#include<conio.h>

void main()

{

int a[3][3],b[3][3],c[3][3],i,j,k;

clrscr();

printf("enter 3X3 first matrix");

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

{

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

{

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

}

}

printf("enter 3X3 second matrix");

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

{

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

{

scanf("%d",&b[i][j]);}}

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]+=a[i][k]*b[k][j];

}

}

printf("first matrix");

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

{

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

{

printf("%d",a[i][j]);

}

printf("\n");

}

printf("second matrix");

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

{

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

{

printf("%d",b[i][j]);

}

printf("\n");

}

printf("multiplication of matrix");

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

{

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

{

printf("%d",c[i][j]);

}

printf("\n");

}

getch();

}

/*19.************ using functions *********************/

//a.1.STRLEN

#include<stdio.h>

#include<conio.h>

void main()

{

char *a=(”Hindustan”);

int l;

l=strlen(a);

pritnf(“the length of string=%s is =%d”,a,l);

getch();

}

//a.2.STRLEN by function

#include<stdio.h>

#include<conio.h>

void main()

{

char *a=("Hindustan");

clrscr();

int l;

l=len(a);

printf("the length of string=%s is =%d",a,l);

getch();

}

len(char *x)

{

int j=0;

while(*x !='\0')

{

j++;

x++;

}

return(j);

}

//b.1.STRCPY

#include<stdio.h>

#include<conio.h>

void main()

{

char *a=("Hindustan");

char *b[20];

clrscr();

strcpy(b,a);

printf("first string is =%s",a);

printf("second string is=%s",b);

getch();

}

//b.2.STRCPY

#include<stdio.h>

#include<conio.h>

void main()

{

char *a=("Hindustan");

char *b[20];

clrscr();

cpyt(a,b);

printf("first string is =%s",a);

printf("second string is=%s",b);

getch();

}

cpyt(char *x,char *y)

{

while(*x !='\0')

{

*y=*x;

x++;

y++;

}

*y='\0';

}

//c.1.STRCAT

#include<stdio.h>

#include<conio.h>

void main()

{

char *a=("Hindustan");

char *b=("college");

clrscr();

strcat(b,a);

printf("first string is =%s",a);

printf("second string is=%s",b);

getch();

}

//D.1.STRREV

#include<stdio.h>

#include<conio.h>

#include <string.h>

void main()

{

char str[] = "Good Morning";

clrscr();

printf("\nThe original string: %s", str);

printf("\nreverse string: %s", strrev(str));

getch();

}

//E.1.Palindrome

#include<stdio.h>

#include<conio.h>

#include<string.h>
#define size 26
void main()
{
char a[size];
char b[size];
clrscr();
printf("\n Enter String:= ");
gets(a);
strcpy(b,strupr(a));
strrev(b);
if(strcmp(a,b)==0)
printf("\n Entered string %s is palindrome",a);
else
printf("\n Entered string %s is not palindrome",a);
getch();
}
//E.2.Palindrome by function

#include<stdio.h>

#include<conio.h>

#include<stdio.h>

void main(){

char str[100],rev[100];

int i,j;

clrscr();

printf("\nEnter a string: ");

scanf("%s",str);

for(i=strlen(str)-1,j=0;i>=0;i--,j++)

rev[j]=str[i];

rev[j]='\0';

if(strcmp(rev,str))

printf("The string is not a palindrome");

else

printf("The string is a palindrome");

getch();

}

/*20(i).***************sorting a list****************/

#include<stdio.h>

#include<conio.h>

void main()

{

int a[5],i,j,k,t;

clrscr();

printf("enter the elements");

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

{

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

}

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

{

for(k=0;k<5-j;k++)

{

if(a[k]>a[k+1])

{

t=a[k+1];

a[k+1]=a[k];

a[k]=t;

}}}

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

{

printf("\n%d",a[i]);

}

getch();

}

/*20(ii).***************search from a list****************/

#include<stdio.h>

#include<conio.h>

void main()

{

int a[10],i,j,k,t;

clrscr();

printf("enter the elements");

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

{

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

}

printf("enter element to search");

scanf("%d",&k);

for(j=0;j<=9;i++)

if(k==a[i])

{

printf("the search element is found:");

break;

}

else

{

printf("element is not found");

}

getch();

}