C Lab Report
C Lab Report
(C Language)
i
Dedication!
ii
Contents
iii
Q. Write a program to print message programming is fun?
Ans.
#include <stdio.h>
int printMessage ()
{
printf ("Programming is fun.\n");
}
int main ()
{
printMessage ();
return 0;
}
1
}
2
#include<stdio.h>
int main()
{
int x;
printf("Enter your roll No. \n");
scanf("%i",&x);
printf("Your roll No. is %i \n",x);
}
3
Q. Write a program to enter 5 to a given number?
Ans.
#include<stdio.h>
int main()
{
int x,z;
printf("Enter a number\n");
scanf("%i",&x);
z=x+5;
printf("The sum of %i and 5 is %i\n",x,z);
return 0;
}
6
Q.Write a program to print area of rectangle and average of four sides?
Ans.
#include<stdio.h>
int main()
{
int length,width,area,avg;
length=60;
width=20;
area=length*width;
avg=(length+length+width+width)/4;
printf("The area is %i \n",area );
printf("The average is %i \n",avg);
}
Q. Write a program such that if weight is greater than 80 it will print Big
Show?
Ans.
#include<stdio.h>
int main()
{
int w;
printf("Enter your Weight \n");
scanf("%i",&w);
if (w>80)
printf("Big Show \n");
else
printf("normal weight \n");
}
9
Q. Write a program to check whether the two given inputs are divisible or
not?
Ans.
#include<stdio.h>
int main()
{
int x,y,z;
printf("Enter first number\n");
scanf("%i",&x);
printf("Enter second number\n");
scanf("%i",&y);
z=x%y;
if(z==0)
printf("%i is divisible by %i\n",x,y);
else
printf("%i is not divisible by %i\n",x,y);
10
Q.Write a program to find whether a number is even or odd?
Ans.
#include<stdio.h>
int main()
{
int x,y;
printf("Enter a number to check whether it is
even or odd\n");
scanf("%i",&x);
y=x%2;
if(y==0)
printf("%i is even",x);
else
printf("%i is odd\n",x);
11
int x,y;
printf("apna numer btao jo check krna\n");
scanf("%i",&x);
y=x%2;
if(y==0)
printf("%i twada no even ha payn !",x);
else
printf("%i odd h jnab\n",x);
12
Q.Write a program to relate two numbers?
Ans.
#include<stdio.h>
int main()
{
int x,y;
printf("Enter first number\n");
scanf("%i",&x);
printf("Enter second number\n");
scanf("%i",&y);
if(x==y)
printf("Both numbers are equal \n");
if(x>y)
printf("%i is greater than %i\n",x,y);
else
printf("%i is greater than %i\n",y,x);
}
13
Q.Write a program to print square of numbers?
Ans.
#include<stdio.h>
int main()
{
float x,z;
printf("Enter a number n\n");
scanf("%f",&x);
z=x*x;
printf("The square of the number is %f\n",z);
}
14
printf("Enter any number: ");
scanf("%d",&num);
while(num){
r=num%10;
reverse=reverse*10+r;
num=num/10;
}
15
q=x*y;
r=x-y;
t=x/y;
printf("\n");
printf("Which operation you want to perform on %i
and %i?\n",x,y);
printf("Type\n");
printf("\n");
printf("1 for Addtion\n");
printf("2 for Subtraction\n");
printf("3 for Multiplication\n");
printf("4 for Division\n");
scanf("%i",&o);
printf("\n");
printf("\n");
printf("\n");
if(o==1)
printf("The addition of %i and %i is %i:
\n",x,y,p);
if(o==2)
printf("The subtraction of %i and %i is %i:
\n",x,y,r);
if(o==3)
printf("The multiplication of %i and %i is %i:
\n",x,y,q);
if(o==4)
printf("The division of %i and %i is %i:
\n",x,y,t);
}
16
Q.Write a Program to print DMC of a Student?
Ans.
#include<stdio.h>
int main()
{
int i,j,k,l,m,n,o,p,q,r,s,total;
float f;
printf("Name Ahsan Ijaz\n");
printf("Reg no %i-EE-
%i\n",2014,422);
printf("Institute name uet lahore fsd
campus\n");
printf("Department electrical
engineeing\n");
printf("Electric circuit\n");
scanf("%i",&j);
printf("ISL&PST\n");
scanf("%i",&k);
printf("Electric Workshop\n");
scanf("%i",&l);
printf("Com. skills\n");
17
scanf("%i",&m);
printf("Autocad\n");
scanf("%i",&n);
printf("calculus\n");
scanf("%i",&o);
printf("Applied physics\n");
scanf("%i",&p);
printf("AP. Lab\n");
scanf("%i",&q);
printf("EC lab\n");
scanf("%i",&r);
printf("\n");
printf("SUBJECT Total marks
Obt marks\n");
printf("Electric circuit 100
%i\n",j);
printf("ISL&PST 100
%i\n",k);
printf("Electric Workshop 100
%i\n",l);
printf("Com. skills 100
%i\n",m);
printf("Autocad 100
%i\n",n);
printf("calculus 100
%i\n",o);
printf("Applied physics 100
%i\n",p);
printf("AP. Lab 100
%i\n",q);
printf("EC lab 100
%i\n",r);
total=j+k+l+m+n+o+p+q+r;
printf("Total is %i\n",total);
s=(total*100)/900;
printf("Percentage is %i\n",s);
printf("\n");
printf("Controler Examination signature");
18
}
19
Q. Write a program to print factorial of a given number?
Ans.
#include <stdio.h>
int main (void)
{
unsigned int j;
unsigned long int factorial (unsigned int n);
for ( j = 0; j < 11; ++j )
printf ("%2u! = %lu\n", j, factorial (j));
return 0;
}
unsigned long int factorial (unsigned int n)
{
unsigned long int result;
if ( n == 0 )
result = 1;
else
result = n * factorial (n - 1);
return result;
}
20
{
int n,c,first=0,second=1,next;
printf("Enter the number of Fibonacci series you
want=");
scanf("%i",&n);
for(c=0;c<n;c++)
{
if(c<=1)
next=c;
else
{
next=first+second;
first=second;
second=next;
}
printf("Fibonacci Number=%i\n",next);
}
}
int prime(int n)
{
int i;
for(i=2;i<n;i++)
{
if(n%i==0)
return 0;
}
return 1;
23
}
24
Q.Write a program to demonstrate functions and lists?
Ans.
#include <stdio.h>
void test (int *int_pointer)
{
*int_pointer = 100;
}
int main (void)
{
void test (int *int_pointer);
int i = 50, *p = &i;
printf ("Before the call to test i = %i\n", i);test
(p);
printf ("After the call to test i = %i\n", i);
return 0;
}
#include <stdio.h>
struct entry
{
25
int value;
struct entry *next;
};
struct entry *findEntry (struct entry *listPtr, int
match)
{
while ( listPtr != (struct entry *) 0 )
if ( listPtr->value == match )
return (listPtr);
else
listPtr = listPtr->next;
return (struct entry *) 0;
}
int main (void)
{
struct entry *findEntry (struct entry *listPtr, int
match);
struct entry n1, n2, n3;
struct entry *listPtr, *listStart = &n1;
int search;
n1.value = 100;
n1.next = &n2;
n2.value = 200;
n2.next = &n3;
n3.value = 300;
n3.next = 0;
printf ("Enter value to locate: ");
scanf ("%i", &search);
listPtr = findEntry (listStart, search);
if ( listPtr != (struct entry *) 0 )
printf ("Found %i.\n", listPtr->value);
else
printf ("Not found.\n");
return 0;
}
26
27
Q.Write a program to print tomorrows date using arrays?
Ans.
#include <stdio.h>
int main (void)
{
struct date
{
int month;
int day;
int year;
};
struct date today, tomorrow;
const int daysPerMonth[12] = { 31, 28, 31, 30, 31,
30,
31, 31, 30, 31, 30, 31 };
printf ("Enter today's date (mm dd yyyy): ");
scanf ("%i%i%i", &today.month, &today.day,
&today.year);
if ( today.day != daysPerMonth[today.month - 1] ) {
tomorrow.day = today.day + 1;
tomorrow.month = today.month;
tomorrow.year = today.year;
}
else if ( today.month == 12 ) {
tomorrow.day = 1;
tomorrow.month = 1;
tomorrow.year = today.year + 1;
}
else {
tomorrow.day = 1;
tomorrow.month = today.month + 1;
tomorrow.year = today.year;
}
printf ("Tomorrow's date is %i/%i/%.2i.\n",
tomorrow.month,
tomorrow.day, tomorrow.year % 100);
return 0;
}
28
Q. Write a program to print sum of an array( demonstrate use of functions
also)?
Ans.
#include <stdio.h>
int arraysum (int array[], int numberOfElements)
{
int i, sum=0;
for (i =0; i <numberOfElements; ++i)
sum = sum + array[i];
return sum;
}
{
int array1[5] = {15,28,37,26,10};
int numberOfElements = 5;
int i, sum =0;
int arraysum (int array[],int numberOfElements);
printf("The sum is %d\n",
arraysum(array1,numberOfElements));
29
}
30
++RatingCounters[response];
}
printf("\n\nRating Number of Responses\n");
for(i=1;i<=10;i++)
printf("%4i%14i\n",i,RatingCounters[i]);
}
31
Q. Write a program to print numbers of an array?
Ans.
#include<stdio.h>
int main(void)
{
int test[]={3,5,65,12,48};
for(int i=0;i<5;i++)
printf("%i\n",test[i]);}
32
printf ("Number to be converted? ");
scanf ("%ld", &numberToConvert);
printf ("Base? ");
scanf ("%i", &base);
// convert to the indicated base
do {
convertedNumber[index] = numberToConvert % base;
++index;
numberToConvert = numberToConvert / base;
}
while ( numberToConvert != 0 );
// display the results in reverse order
printf ("Converted number = ");
for (--index; index >= 0; --index ) {
nextDigit = convertedNumber[index];
printf ("%c", baseDigits[nextDigit]);
}
printf ("\n");
return 0;
}
33
primes[0] = 2;
primes[1] = 3;
for ( p = 5; p <= 50; p = p + 2 ) {
isPrime = true;
for ( i = 1; isPrime && p / primes[i] >= primes[i];
++i )
if ( p % primes[i] == 0 )
isPrime = false;
if ( isPrime == true ) {
primes[primeIndex] = p;
++primeIndex;
}
}
for ( i = 0; i < primeIndex; ++i )
printf ("%i ", primes[i]);
printf ("\n");
return 0;
}
34
Q.Write a program to demonstrate pointers?
Ans.
#include <stdio.h>
int main (void)
{
int count = 10, x;
int *int_pointer;
int_pointer = &count;
x = *int_pointer;
printf ("count = %i, x = %i\n", count, x);
return 0;
}
36
n1.next = &n2;
n2.next = &n3;
n3.next=&n1;
i = n3.next->value;
printf ("%i ", i);
return 0;
}
#include <stdio.h>
int main (void)
{
struct date
{
int month;
38
int day;
int year;
};
struct date today, *datePtr;
datePtr = &today;
datePtr->month = 9;
datePtr->day = 25;
datePtr->year = 2004;
printf ("Today's date is %i/%i/%.2i.\n",
datePtr->month, datePtr->day, datePtr->year % 100);
return 0;
}
39
Q. Write a structure to print todays date?
Ans.
#include <stdio.h>
int main (void)
{
struct date
{
int month;
int day;
int year;
};
struct date today;
today.month = 9;
today.day = 25;
today.year = 2004;
printf ("Today's date is %i/%i/%.2i.\n", today.month,
today.day,
today.year % 100);
return 0;
40
{
int hour,minutes,seconds;
};
int main (void)
{
struct time timeUpdate (struct time now);
struct time currentTime, nextTime;
printf ("Enter the time (hh:mm:ss): ");
scanf ("%i:%i:%i",
¤tTime.hour,¤tTime.minutes,
¤tTime.seconds);
nextTime = timeUpdate (currentTime);
printf ("Updated time is %.2i:%.2i:%.2i\n",
nextTime.hour,
nextTime.minutes, nextTime.seconds );
return 0;
}
struct time timeUpdate (struct time now)
{
++now.seconds;
if ( now.seconds == 60 ) {
now.seconds = 0;
++now.minutes;
if ( now.minutes == 60 ) {
now.minutes = 0;
++now.hour;
if ( now.hour == 24 )
now.hour = 0;
}
}
return now;}
41
42