Sei sulla pagina 1di 45

Introduction to Programming

(C Language)

University of Engineering and Technology


Lahore

i
Dedication!

I would like to dedicate this compilation to my loving brother Muhammad Zain.


I am thankful to him for his support and assistance in my well being and success
which stands worth significance in my life.

Coded By: Ahsan Ijaz & Daniyal Ahmad

Compiled By: Ahsan Ijaz

ii
Contents

TITLE Page No.

BASICS OF C LANGUAGE --1--


LOOPS,FUNCTIONS,ARRAYS 20,25,28
POINTERS,STRUCUTERS -35,40-

Note: errors and omissions are expected

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;
}

Q.Write a program to print entered marks?


Ans.
#include<stdio.h>
int main()
{
float x;
printf("Enter Your Marks\n");
scanf("%f",&x);
printf("Your Marks Are %f\n",x);

1
}

Q.Write a program to print half of a given number?


Ans.
#include<stdio.h>
int main()
{
float x,z;
printf("Enter a number n\n");
scanf("%f",&x);
z=x/2;
printf("The result of n/2 is %f\n",z);
}

Q. Write a program to print the entered roll no?


Ans.

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);
}

Q.Write a program to sum 1 to a given number?


Ans.
#include<stdio.h>
int main()
{
int x,z;
printf("Enter a number\n");
scanf("%i",&x);
z=x+1;
printf("The sum of %i and 1 is %i\n",x,z);
}

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;
}

Q. Write a program to enter two numbers and show their sum?


Ans.
4
#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;
printf("The sum of %i and %i is %i\n",x,y,z);
}

Q. Write a program to print subtraction of two numbers?


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);
5
z=x-y;
printf("The subtraction of %i and %i is
%i\n",x,y,z);
}

Q.Write a program to print area of square and average of four sides?


Ans.
#include<stdio.h>
int main()
{
int l,a,avg;
l=20;
a=l*l;
avg=(l+l+l+l)/4;
printf("The area is %i \n",a);
printf("The average is %i \n",avg);
}

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 to print first 8th triangular numbers?


Ans.
#include<stdio.h>
int main()
{
int x;
x=1+2+3+4+5+6+7+8;
7
printf("The eight triangular number is %i\n",x);
}

Q. Write a program to perform different arithmetic functions on given two


numbers?
Ans.
#include<stdio.h>
int main()
{
float x,y,a,s,m,d;
printf("Enter first number\n");
scanf("%f",&x);
printf("Enter second number\n");
scanf("%f",&y);
a=x+y;
s=x-y;
m=x*y;
d=x/y;
printf("The sum of %f and %f is %f\n",x,y,a);
printf("The subtraction of %f and %f is
%f\n",x,y,s);
8
printf("The multiplication of %f and %f is
%f\n",x,y,m);
printf("The division of %f and %f is
%f\n",x,y,d);
}

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);

Q.Write a program to check whether a number is even or odd in funky


style?
Ans.
#include<stdio.h>
int main()
{

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);

Q.Write a program to swap two numbers without using temporary


variables?
Ans.
#include<stdio.h>
int main()
{
int x,y;
printf("Enter 1st No. \n");
scanf("%i",&x);
printf("Enter 2nd No. \n");
scanf("%i",&y);
y=x+y;
x=y-x;
y=y-x;
printf("New value of 1st No %i\n",x);
printf("New value of 2nd No %i\n",y);
}

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);
}

Q.Write a program to print reverse of a given number?


Ans.
#include<stdio.h>
int main(){
int num,r,reverse=0;

14
printf("Enter any number: ");
scanf("%d",&num);

while(num){
r=num%10;
reverse=reverse*10+r;
num=num/10;
}

printf("Reverse of number: %d",reverse);


return 0;

Q. Write a program to input two numbers and then perform indicated


arithmetic function on it?
Ans.
#include<stdio.h>
int main()
{
int x,y,p,q,r,t,o;
printf("Enter first number\n");
scanf("%i",&x);
printf("Enter second number\n");
scanf("%i",&y);
p=x+y;

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;
}

Q. Write a program to print required number of Fibonacci numbers?


Ans.
#include<stdio.h>
int main(void)

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);
}
}

Q.Write a program to print required triangular numbers five times?


Ans.
#include<stdio.h>
21
int tri(int n)
{
int i, trinum=0;
for(i=1;i<=n;i++)
trinum+=i;
return trinum;
}
int main(void)
{
int num, TriNum, counter, x;
for(counter=1;counter<=5;counter++)
{
printf("What Triangular Number do you want=");
scanf("%i",&num);
x=tri(num);
printf("Triangular number of %i is %i\n\n",num,
x);
}
}

Q. Write a program to print whether given number is prime or not?


Ans.
22
#include<conio.h>
int prime(int);
int main()
{
int n,p;
printf("Enter a number : ");
scanf("%d",&n);
p=prime(n);
if(p==1)
printf("%d is prime\n",n);
else
printf("%d is not prime\n",n);
}

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;
}

Q.Write a program to demonstrate calling functions in c & cpp?


Ans.

#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 main (void)

{
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
}

Q. Write a program to input and print responses for different ratings?


Ans.
#include<stdio.h>
int main(void)
{
int RatingCounters[11], i, j, response;
for(i=1;i<=10;i++)
RatingCounters[i]=0;
printf("How many responses do you want to
enter=");
scanf("%i",&j);
printf("Enter you responses\n");
for(i=1;i<=j;i++)
{
scanf("%i",&response);
if(j==999)
printf("Last response has been entered");
if(response<1||response>10)
printf("Bad Response: %i\n",response);
else

30
++RatingCounters[response];
}
printf("\n\nRating Number of Responses\n");
for(i=1;i<=10;i++)
printf("%4i%14i\n",i,RatingCounters[i]);
}

Q.Write a program to print sum of ten numbers using array?


Ans.
#include<stdio.h>
int main(void)
{
float num[10], x;
printf("Enter any ten numbers\n");
for(int i=0;i<=10;i++)
scanf("%f",&num[i]);
x=(num[0]+num[1]+num[2]+num[3]+num[4]+num[5]+num[
6]+num[7]+num[8]+num[9])/10;
printf("Average of ten numbers=%f",x);
}

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]);}

Q. Write a program to convert a number to indicated base?


Ans.
#include <stdio.h>
int main (void)
{
const char baseDigits[16] = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
int convertedNumber[64];
long int numberToConvert;
int nextDigit, base, index = 0;
// get the number and the base

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;
}

Q.Write a program to print prime numbers from 1 to to 50?


Ans.
#include <stdio.h>
#include <stdbool.h>
int main (void)
{
int p, i, primes[50], primeIndex = 2;
bool isPrime;

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;
}

Q.Write a program to demonstrate pointers and expressions?


Ans.
#include <stdio.h>
int main (void)
{
int i1, i2;
int *p1, *p2;
i1 = 5;
p1 = &i1;
35
i2 = *p1 / 2 + 10;
p2 = p1;
printf ("i1 = %i, i2 = %i, *p1 = %i, *p2 = %i\n",
i1, i2, *p1, *p2);
return 0;
}

Q.Write a program to demonstrate linked lists?


Ans.
#include <stdio.h>
int main (void)
{
struct entry
{
int value;
struct entry *next;
};
struct entry n1, n2, n3;
int i;
n1.value = 100;
n2.value = 200;
n3.value = 300;

36
n1.next = &n2;
n2.next = &n3;
n3.next=&n1;
i = n3.next->value;
printf ("%i ", i);
return 0;
}

Q.Write a program to traverse a given linked list?


Ans.
Traversing linked list
#include <stdio.h>
int main (void)
{
struct entry
{
int value;
struct entry *next;
};
struct entry n1, n2, n3;
struct entry *list_pointer = &n1;
n1.value = 100;
37
n1.next = &n2;
n2.value = 200;
n2.next = &n3;
n3.value = 300;
n3.next = (struct entry *) 0; // Mark list end
with null pointer
while ( list_pointer != (struct entry *) 0 ) {
printf ("%i\n", list_pointer->value);
list_pointer = list_pointer->next;
}
return 0;
}

Q.Write a program to demonstrate pointers and structures?


Ans.

#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;

Q.Write a program to print updated time using structures?


Ans.
#include <stdio.h>
struct time

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",
&currentTime.hour,&currentTime.minutes,
&currentTime.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

Potrebbero piacerti anche