0% found this document useful (0 votes)
5 views53 pages

C Programming Lab Assignments Overview

The document contains C code for multiple programs and lab assignments: 1) A program that calculates time from distance and speed input by the user. 2) A program that selects a candidate if they meet age, height, and marks criteria. 3) Multiple programs that perform operations on numbers like calculating sums and checking if a number is prime. 4) Programs that output patterns like stars, numbers, and matrices using loops.

Uploaded by

Arslan Anjum
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views53 pages

C Programming Lab Assignments Overview

The document contains C code for multiple programs and lab assignments: 1) A program that calculates time from distance and speed input by the user. 2) A program that selects a candidate if they meet age, height, and marks criteria. 3) Multiple programs that perform operations on numbers like calculating sums and checking if a number is prime. 4) Programs that output patterns like stars, numbers, and matrices using loops.

Uploaded by

Arslan Anjum
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Lab Assignments

#include<stdio.h>

#include<conio.h>

main()

int s, v, t, x, z, u;

int y;

printf("Enter the distance in Km:");

scanf("%d",&s);

printf("Enter the speed in Km/h:");

scanf("%d",&v);

t=s/v;

x=s%v;

y=(60*x)/v;

z=(60*x)%v;

u=(z*60)/v;

printf("The time is: %d hours, %d minutes and %d seconds.",t, y,u);

getch();

return 0;

}
AMJAD HASSAN, SESSION (2015-17)
#include<stdio.h>

#include<conio.h>

main()

int age, marks;

float height;

printf("Enter your age:");

scanf("%d",&age);

if(age>=16 && age<=20)

printf("Enter your height.");

scanf("%f",&height);

if(height>=5.6)

printf("Enter your marks");

scanf("%d",&marks);

if(marks>=60)

printf("Congratulations, you are selected.");

AMJAD HASSAN, SESSION (2015-17)


}

else

printf("You don't meet the educational requirements.");

else

printf("You don't meet the physical requirements.");

else

printf("You don't meet the age requirements.");

getch();

return 0;

printf("Enter your height:");

scanf("%f",&height);

printf("Enter your marks:");

scanf("%d",&marks);

if(age<=16 || age>=20)

printf("Sorry, you couldn't meet the age requirements.");

if(height<5.6)

printf("Sorry, you couldn't meet the physical requirements.");

AMJAD HASSAN, SESSION (2015-17)


if(marks<60)

printf("Sorry, you couldn't meet the educational requirements.");

else

printf("Congratulations, you are selected.");

getch();

return 0;

#include<stdio.h>

#include<conio.h>

main()

int n,x,y,z,c,b;

printf("Enter the number:");

scanf("%d",&n);

x=n%10;

y=n/10;

z=y%10;

b=y/10;

c=x+z+b;

printf("The sum is: %d",c);

AMJAD HASSAN, SESSION (2015-17)


getch();

return 0;

#include<stdio.h>

#include<conio.h>

main()

int n,x,y,z,c,b;

printf("Enter the number:");

scanf("%d",&n);

x=n%10;

y=n/10;

z=y%10;

b=y/10;

c=x*x*x+z*z*z+b*b*b;

if(c==n)

printf("The number %d is armstrong number.",n);

else

printf("The number %d is not a armstrong number.",n);

getch();

AMJAD HASSAN, SESSION (2015-17)


return 0;

#include<stdio.h>

#include<conio.h>

main()

int n1,n2,x,y,i;

printf("Enter number 1:");

scanf("%d",&n1);

printf("Enter number 2:");

scanf("%d",&n2);

x= n1<n2 ? n1:n2;

y= n1>n2 ? n1:n2;

for(i=x; i<=y; i++)

if (!(i%2)==0)

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

getch();

return 0;

AMJAD HASSAN, SESSION (2015-17)


}

#include<stdio.h>

#include<conio.h>

main()

int n1,n2,x,y,i;

printf("Enter number 1:");

scanf("%d",&n1);

printf("Enter number 2:");

scanf("%d",&n2);

x= n1<n2 ? n1:n2;

y= n1>n2 ? n1:n2;

for(i=x; i<=y; i++)

if(i>45 && i<50)

continue;

if (!(i%2)==0)

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

getch();

AMJAD HASSAN, SESSION (2015-17)


return 0;

#include<stdio.h>

#include<conio.h>

main()

int n,i, fact=1;

printf("Enter a number between 1 and 15:");

scanf("%d",&n);

while(n>15)

printf("Enter a number between 1 to 15.");

scanf("%d",&n);

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

fact=fact*i;

printf("The factorial of %d is:: %d",n,fact);

getch();

AMJAD HASSAN, SESSION (2015-17)


return 0;

#include<stdio.h>

#include<conio.h>

main()

int n, i, flag=0;

printf("Enter a number:");

scanf("%d",&n);

for(i=2; i<=n/2; i++)

if(n%i==0)

flag=1;

break;

if(flag==0)

printf("The entered number is a prime number.");

else

AMJAD HASSAN, SESSION (2015-17)


printf("The entered number is not a prime number.");

getch();

return 0;

#include<stdio.h>

#include<conio.h>

int main(void)

int first_digit, second_digit;

printf("Enter two digits: ");

scanf("%1d%1d",&first_digit,&second_digit);

switch(first_digit % 10)

case 1:

printf("ten"); break;

case 2:

printf("twenty"); break;

case 3:

printf("thirty"); break;

AMJAD HASSAN, SESSION (2015-17)


case 4:

printf("forty"); break;

case 5:

printf("fifty"); break;

case 6:

printf("sixty"); break;

case 7:

printf("seventy"); break;

case 8:

printf("eighty"); break;

case 9:

printf("ninety"); break;

switch(second_digit % 10)

case 0: break;

case 1:

printf(" one"); break;

case 2:

printf(" two"); break;

AMJAD HASSAN, SESSION (2015-17)


case 3:

printf(" three"); break;

case 4:

printf(" four"); break;

case 5:

printf(" five"); break;

case 6:

printf(" six"); break;

case 7:

printf(" seven"); break;

case 8:

printf(" eight"); break;

case 9:

printf(" nine"); break;

getch();

return 0;

#include<stdio.h>

#include<conio.h>

AMJAD HASSAN, SESSION (2015-17)


int main()

int n1, n2, i, j,x,y, flag;

printf("Enter two numbers(intevals): ");

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

printf("Prime numbers between %d and %d are: ", n1, n2);

if(n2>n1)

x=n2;

else

x=n1;

if(n2<n1)

y=n2;

else

y=n1;

for(i=y+1; i<x; ++i)

flag=0;

for(j=2; j<=i/2; ++j)

if(i%j==0)

AMJAD HASSAN, SESSION (2015-17)


{

flag=1;

break;

if(flag==0)

printf("%d ",i);

getch();

return 0;

#include<stdio.h>

#include<conio.h>

int main()

int n,i, prime;

printf("Enter a number: ");

scanf("%d",&n);

if(n==2)

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

AMJAD HASSAN, SESSION (2015-17)


else

for(i=2; i<=n/2; i++)

prime=1;

if(n%i==0)

prime=0;

break;

if(prime==0)

printf("It is not a prime number.");

else

printf("It is a prime number.");

getch();

return 0;

#include<stdio.h>

AMJAD HASSAN, SESSION (2015-17)


#include<conio.h>

main()

int i,j;

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

for(j=0; j<9-2*i; j++)

printf("*");

printf("\n");

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

printf(" ");

getch();

return 0;

#include<stdio.h>

#include<conio.h>

main()

int n,i;

AMJAD HASSAN, SESSION (2015-17)


printf("Enter a number: ");

scanf("%d",&n);

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

if(n%i==0)

break;

if(i<n)

printf("The first divisible of %d is %d.",n,i);

else

printf("This is a prime number.");

getch();

return 0;

#include<stdio.h>

#include<conio.h>

main()

int n;

while(1)

AMJAD HASSAN, SESSION (2015-17)


{

printf("Enter a number: ");

scanf("%d",&n);

if(n%5==0)

break;

printf("Loop terminated.");

getch();

return 0;

#include <stdio.h>

#include <conio.h>

void main()

int i,j;

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

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

printf("%d",j);

printf("\n");

AMJAD HASSAN, SESSION (2015-17)


}

getch();

Program to print stars in the form of Diamond after taking number of


rows from user:

#include<stdio.h>

#include<conio.h>

main()

int i, j, x;

printf("Enter a number:");

scanf("%d",&x);

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

for(j=0; j<x-i; j++)

printf(" ");

for(j=0; j<2*i+1; j++)

printf("*");

printf("\n");

AMJAD HASSAN, SESSION (2015-17)


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

for(j=0; j<2*i+1; j++)

printf("*");

printf("\n");

for(j=0;j<=x-i ;j++)

printf(" ");

getch();

return 0;

#include<stdio.h>

#include<conio.h>

main()

int i, j, x, y,k;

printf("Enter a number:");

scanf("%d",&x);

y=x/2;

AMJAD HASSAN, SESSION (2015-17)


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

for(j=0; j<y-i; j++)

printf(" ");

for(j=0; j<2*i+1; j++)

printf("*");

printf("\n");

if(x%2!=0)

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

printf("*");

if(i==(x-1))

printf("\n");

for(i=y-1; i>=0; i--)

k=(y-1);

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

printf(" ");

AMJAD HASSAN, SESSION (2015-17)


for(j=0; j<2*i+1; j++)

printf("*");

printf("\n");

getch();

return 0;

To print a 3*3 matrix

#include<stdio.h>

#include<conio.h>

main()

int i,j;

int stu[3][3]={1,3,7,6,4,5,9,2,6};

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

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

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

printf("\n");

AMJAD HASSAN, SESSION (2015-17)


getch();

return 0;

#include<stdio.h>

#include<conio.h>

main()

int i;

float avg, sum=0.0;

float expenses[12]={203.4, 304, 506.8, 407, 503, 301, 123, 389, 129,
345, 567, 789};

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

sum=sum+expenses[i];

avg=sum/12;

printf("The total expenses of year is %.2f\n",sum);

printf("Average expenses per month is %.3f",avg);

getch();

return 0;

AMJAD HASSAN, SESSION (2015-17)


}

Print Even and Odd Numbers from an array


#include<stdio.h>

#include<conio.h>

main()

int i, j=0,k=0, Glo[6];

int Odd[3];

int Even[3];

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

printf("Enter the %d value of Array:",i+1);

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

printf("The values of Array are: ");

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

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

printf("\n");

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

{
AMJAD HASSAN, SESSION (2015-17)
if(Glo[i]%2==0)

Even[j]=Glo[i];

printf("The Even numbers is %d.\n",Even[j]);

j++;

else

Odd[k]=Glo[i];

printf("The Odd numbers is %d.\n",Odd[k]);

k++;

getch();

return 0;

#include<stdio.h>

#include<conio.h>

main()

int i, j=0,k=0,l=0, Glo[6];

int Odd[3];

AMJAD HASSAN, SESSION (2015-17)


int Even[3];

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

printf("Enter the %d value of Array:",i+1);

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

printf("The values of Array are: ");

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

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

printf("\n");

j=0;

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

if(Glo[i]%2==0)

Even[j]=Glo[i];

printf("The Even numbers is %d.\n",Even[j]);

j++;

else

AMJAD HASSAN, SESSION (2015-17)


{

Odd[k]=Glo[i];

printf("The Odd numbers is %d.\n",Odd[k]);

k++;

k=0;

printf("{ ");

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

printf(" %d ",Odd[k]);

printf("}\n");

printf("{ ");

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

printf(" %d ",Even[l]);

printf("}");

getch();

return 0;

AMJAD HASSAN, SESSION (2015-17)


#include<stdio.h>

#include<conio.h>

main()

int i;

int A[5]={ 1, 4, 5, 3, 9 };

int B[5];

B[1]=A[3];

B[3]=A[1];

B[4]=A[2];

B[0]=A[4];

B[2]=A[0];

printf("The Values of Array B are as:");

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

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

getch();

return 0;

#include<stdio.h>

#include<conio.h>

AMJAD HASSAN, SESSION (2015-17)


main()

int i;

int A[5]={ 8, 9, 5, 11, 20 };

int B[5];

B[0]=A[4];

B[1]=A[2];

B[2]=A[1];

B[3]=A[0];

B[4]=A[3];

printf("The Values of Array B are as:\t");

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

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

getch();

return 0;

#include<stdio.h>

#include<conio.h>

main()

AMJAD HASSAN, SESSION (2015-17)


int max=0, min,i,Sum;

int A[5]={ 23, 45, 67, 89, 90};

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

if(max<A[i])

max=A[i];

min=A[0];

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

if(min>A[i])

min=A[i];

printf("The max is %d\n",max);

printf("The min is %d\n",min);

Sum=max+min;

printf("Sum is %d.",Sum);

getch();

return 0;

#include<stdio.h>
AMJAD HASSAN, SESSION (2015-17)
#include<conio.h>

main()

int i,max;

int A[5];

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

printf("Enter the first entry of array:");

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

max=A[0];

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

if(A[i]>max)

max=A[i];

printf("Max is: %d",max);

getch();

return 0;

AMJAD HASSAN, SESSION (2015-17)


#include<stdio.h>

#include<conio.h>

main()

int i,max,min;

int A[5];

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

printf("Enter the first entry of array:");

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

max=A[0];

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

if(A[i]>max)

max=A[i];

min=A[0];

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

AMJAD HASSAN, SESSION (2015-17)


{

if(A[i]<min)

min=A[i];

A[0]=max;

A[4]=min;

printf("The values of arrays are now as:");

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

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

getch();

return 0;

#include<stdio.h>

#include<conio.h>

main()

int i;

int A[3]={ 1, 5 ,6 };

AMJAD HASSAN, SESSION (2015-17)


int B[3]={ 2, 7, 4 };

int temp[3];

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

temp[i]=A[i];

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

A[i]=B[i];

printf("A[%d]=%d\t",i,A[i]);

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

B[i]=temp[i];

printf("B[%d]=%d\t",i,B[i]);

getch();

return 0;

#include<stdio.h>

AMJAD HASSAN, SESSION (2015-17)


#include<conio.h>

#include<stdlib.h>

main()

int i, j, k, Sum=0;

int A[3][3]={ {1, 3, 6}, { 7, 9, 3 }, { 4, 2, 8} };

int B[3][3]={ {1, 4, 1}, { 7, 5, 1 }, { 9, 6, 2} };

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

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

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

Sum=Sum+(A[i][k]*B[k][j]);

printf("%d",Sum);

printf("\t");

Sum=0;

printf("\n");

AMJAD HASSAN, SESSION (2015-17)


}

getch();

return 0;

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

main()

int i, j;

int A[3][3]={ {1, 3, 6}, { 7, 9, 3 }, { 4, 2, 8} };

int B[3][3]={ {1, 4, 1}, { 7, 5, 1 }, { 9, 6, 2} };

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

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

printf("%d\t", A[i][j]+B[i][j]);

printf("\n");

AMJAD HASSAN, SESSION (2015-17)


getch();

return 0;

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

main()

int i, j, a, b;

int A[2][2]={ {2, 1}, {0, 5} };

int B[2][2]={ {3, 5}, {7, 2} };

printf("Enter the value of a:");

scanf("%d",&a);

printf("Enter the value of b:");

scanf("%d",&b);

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

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

printf("%d\t", a*(A[i][j])+b*(B[i][j]));

AMJAD HASSAN, SESSION (2015-17)


}

printf("\n");

getch();

return 0;

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

main()

int i, j;

int A[3][3]={ {2, 1, 5}, {0, 5, 7}, { 2, 5,8} };

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

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

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

printf("\n");

AMJAD HASSAN, SESSION (2015-17)


}

getch();

return 0;

#include<stdio.h>

#include<conio.h>

#include<math.h>

int main()

int a,d,n,i,tn;

int sum=0;

printf("Enter the first number of the A.P. series: ");

scanf("%d",&a);

printf("Enter the total numbers in the A.P. series: ");

scanf("%d",&n);

printf("Enter the common difference of A.P. series: ");

scanf("%d",&d);

sum = ( n*( 2 * a + ( n -1 ) * d))/ 2;

tn = a + (n-1) * d;

AMJAD HASSAN, SESSION (2015-17)


printf("Sum of the series A.P.: ");

for(i=a;i<=tn; i= i + d )

if (i != tn)

printf("%d + ",i);

else

printf("%d = %d ",i,sum);

getch();

return 0;

#include<stdio.h>

#include<conio.h>

main()

int i, j, x, d;

int A[9];

printf("Entries of arrays are:\n");

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

AMJAD HASSAN, SESSION (2015-17)


{

scanf("%d\t", &A[i]);

x=A[1]-A[0];

j=1;

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

d=0;

d=A[j]-A[i];

if(d!=x)

break;

j++;

if(x==d)

printf("Series is Arithematic.");

else

printf("Series is not arithematic.");

getch();

return 0;

AMJAD HASSAN, SESSION (2015-17)


#include<stdio.h>

#include<conio.h>

main()

int i, j, x, d, t;

int A[9];

printf("Entries of arrays are:\n");

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

scanf("%d\t", &A[i]);

x=A[1]-A[0];

j=1;

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

d=0;

d=A[j]-A[i];

if(d!=x)

break;

j++;

AMJAD HASSAN, SESSION (2015-17)


}

if(x==d)

printf("Series is Arithematic.");

t= A[0]+(65-1)*d;

printf("The 65th term is: %d", t);

else

printf("Series is not arithematic.");

getch();

return 0;

#include<stdio.h>

#include<conio.h>

main()

int i, j, x, d, t=7;

int A[5];

printf("Entries of arrays are:\n");

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

AMJAD HASSAN, SESSION (2015-17)


{

scanf("%d\t", &A[i]);

x=A[1]-A[0];

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

d=A[i+1]-A[i];

t=t++;

if(d!=x)

break;

if(x==d)

printf("Series is Arithematic.");

else

printf("Series is not arithematic.");

printf("%d", t);

getch();

return 0;

#include<conio.h>

AMJAD HASSAN, SESSION (2015-17)


#include<stdio.h>

main()

int a, d, i;

int A[9]={ 1, 3, 5, 7, 9, 10, 13, 15, 17 };

d=A[1]-A[0];

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

a=A[i+1]-A[i];

if(a!=d)

break;

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

if(d==a)

printf("This is a AM Series.");

else

printf("This is not a AM Series.");

getch();

return 0;

AMJAD HASSAN, SESSION (2015-17)


#include<stdio.h>

#include<conio.h>

main()

int i, x=0, n;

printf("Enter number of terms: ");

scanf("%d",&n);

int Fib[20];

Fib[0]=0;

Fib[1]=1;

printf("%d\t %d\t",Fib[0], Fib[1]);

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

Fib[i]=Fib[i-2]+Fib[i-1];

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

getch();

return 0;

#include<stdio.h>

AMJAD HASSAN, SESSION (2015-17)


#include<conio.h>

main()

int i, j, x, d, t, AM;

int A[9];

printf("Entries of arrays are:\n");

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

scanf("%d\t", &A[i]);

x=A[1]-A[0];

j=1;

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

d=0;

d=A[j]-A[i];

if(d!=x)

break;

j++;

AMJAD HASSAN, SESSION (2015-17)


if(x==d)

printf("Series is Arithematic.");

t= A[0]+(65-1)*d;

printf("The 65th term is: %d\n", t);

else

printf("Series is not arithematic.");

j=4;

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

AM=0;

AM=(A[i]+A[j])/2;

j++;

printf("AM is %d\n",AM);

getch();

return 0;

#include<stdio.h>

AMJAD HASSAN, SESSION (2015-17)


#include<conio.h>

main()

int i, j;

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

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

printf(" ");

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

printf("%d", j);

printf("\n");

getch();

return 0;

#include<stdio.h>

#include<conio.h>

#include<math.h>

AMJAD HASSAN, SESSION (2015-17)


main()

int i, a, n,j, k=1;

float Sum=0.0, f, x;

printf("Enter the angle in degrees: ");

scanf("%d", &a);

printf("Enter the number of terms to be calculated: ");

scanf("%d", &n);

x=a*(3.14/180);

j=1;

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

Sum=Sum+(pow(x,i)*pow(-1,j))/(k*(i-1)*i);

j++;

k=(k*(i-1)*i);

f=1+Sum;

printf("The value of Cosine %d is %0.3f.",a,f);

getch();

return 0;

AMJAD HASSAN, SESSION (2015-17)


}

#include<stdio.h>

#include<conio.h>

double fact(int n);

main()

int a, x;

printf("Enter a number:");

scanf("%d",&a);

x=fact(a);

printf("Factorial is %d.",x);

getch();

return 0;

double fact(int n)

if(n==0 || n==1)

return 1;

else

AMJAD HASSAN, SESSION (2015-17)


n*=fact(n-1);

return n;

#include<stdio.h>

#include<conio.h>

int fab(int n);

main()

int a, x, i;

printf("Enter number of terms:");

scanf("%d",&a);

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

x=fab(i);

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

getch();

return 0;

AMJAD HASSAN, SESSION (2015-17)


int fab(int n)

if(n==0 || n==1)

return n;

else

return fab(n-1)+fab(n-2);

AMJAD HASSAN, SESSION (2015-17)

You might also like