PPS File
PPS File
PPS File
2018-19
5
To demonstrate the importance of ampersand(&)operator in
scanf
Course Outcomes
The student will learn
• To formulate simple algorithms for arithmetic and logical problems.
• To translate the algorithms to programs (in C language).
• To test and execute the programs and correct syntax and logical errors.
• To implement conditional branching, iteration and recursion.
• To decompose a problem into functions and synthesize a complete
program using divide and conquer approach.
• To use arrays, pointers and structures to formulate algorithms and
programs. To apply programming to solve matrix addition and multiplication
problems and searching and sorting problems.
• To apply programming to solve simple numerical method problems,
namely rot finding of function, differentiation of function and simple
integration.
Laboratory Outcomes
• To formulate the algorithms for simple problems
• To translate given algorithms to a working and correct program
• To be able to correct syntax errors as reported by the compilers
• To be able to identify and correct logical errors encountered at run time
• To be able to write iterative as well as recursive programs
• To be able to represent data in arrays, strings and structures and manipulate them
through a program To be able to declare pointers of different types and use them
in defining selfreferential structures.
• To be able to create, read and write to and from simple text files.
Compiler: A compiler is a software program that compiles program source code files into
an executable program. It is included as part of the integrated development environment
IDE with most programming software packages.
The compiler takes source code files that are written in a high-level language, such as C,
BASIC, or Java, and compiles the code into a low-level language, such as machine code
or assembly code. This code is created for a specific processor type, such as and Intel
Pentium or PowerPC. The program can then be recognized by the processor and run from
the operating system.
List of best and free C/C++ compilers and IDEs for Programmers
For
F1 Alt+X Quit Ctrl+F9 Run
Help
Trace Toggle
F7 Ctrl+Del Clear Ctrl+F8
into breakpoint
Step Search
F8 Ctrl+L Ctrl+F5 Size/Move
over again
Previous
F9 Make Alt+F7 Alt+F3 Close
error
Next
F10 Menu Alt+F8 Alt+F5 User screen
error
Topic Previous
Ctrl+F1 Alt+F1
search topic
signed
unsigned
long
short
float Floating point number. 4 bytes +/- 3.4e +/- 38 (~7 digits)
There is no fixed number of
digits before or after the
decimal point.
double Double precision floating 8 bytes +/- 1.7e +/- 308 (~15
point number. More digits)
accurate compared to float.
long double Long double precision 8 bytes +/- 1.7e +/- 308 (~15
floating point number. digits)
bool Boolean value. It can only take one 1 byte true or false
of two values: true or false.
C Keywords
asm else new this
auto enum operator throw
bool explicit private TRUE
break export protected try
case extern public typedef
catch FALSE register typeid
char float reinterpret_cast typename
class for return union
const friend short unsigned
const_cast goto signed using
continue if sizeof virtual
default inline static void
delete int static_cast volatile
do long struct wchar_t
double mutable switch while
dynamic_cast namespace template
In addition,
the following
words are
also
reserved:
Functions Descriptions
abort stops the program
abs absolute value
acos arc cosine
asctime a textual version of the time
asin arc sine
assert stops the program if an expression isn't true
atan arc tangent
atan2 arc tangent, using signs to determine quadrants
sets a function to be called when the program
atexit
exits
atof converts a string to a double
atoi converts a string to an integer
atol converts a string to a long
bsearch perform a binary search
allocates and clears a two-dimensional chunk of
calloc
memory
ceil the smallest integer not less than a certain value
clearerr clears errors
returns the amount of time that the program has
clock
been running
cos cosine
cosh hyperbolic cosine
returns a specifically formatted version of the
ctime
time
difftime the difference between two times
div returns the quotient and remainder of a division
exit stop the program
exp returns "e" raised to a given power
fabs absolute value for floating-point numbers
fclose close a file
feof true if at the end-of-file
ferror checks for a file error
fflush writes the contents of the output buffer
fgetc get a character from a stream
fgetpos get the file position indicator
fgets get a string of characters from a stream
returns the largest integer not greater than a
floor
given value
fmod returns the remainder of a division
fopen open a file
fprintf print formatted output to a file
fputc write a character to a file
fputs write a string to a file
fread read from a file
returns previously allocated memory to the
free
operating system
freopen open an existing stream with a different name
frexp decomposes a number into scientific notation
fscanf read formatted input from a file
fseek move to a specific location in a file
fsetpos move to a specific location in a file
ftell returns the current file position indicator
fwrite write to a file
getc read a character from a file
getchar read a character from STDIN
getenv get environment information about a variable
gets read a string from STDIN
returns a pointer to the current Greenwich Mean
gmtime
Time
isalnum true if a character is alphanumeric
isalpha true if a character is alphabetic
iscntrl true if a character is a control character
isdigit true if a character is a digit
isgraph true if a character is a graphical character
islower true if a character is lowercase
isprint true if a character is a printing character
ispunct true if a character is punctuation
isspace true if a character is a space character
isupper true if a character is an uppercase character
itoa Convert a integer to a string
isxdigit true if a character is a hexadecimal character
labs absolute value for long integers
ldexp computes a number in scientific notation
returns the quotient and remainder of a division,
ldiv
in long integer form
localtime returns a pointer to the current time
log natural logarithm
log10 natural logarithm, in base 10
longjmp start execution at a certain point in the program
malloc allocates memory
searches an array for the first occurrence of a
memchr
character
memcmp compares two buffers
memcpy copies one buffer to another
memmove moves one buffer to another
memset fills a buffer with a character
mktime returns the calendar version of a given time
decomposes a number into integer and fractional
modf
parts
displays a string version of the current error to
perror
STDERR
pow returns a given number raised to another number
printf write formatted output to STDOUT
putc write a character to a stream
putchar write a character to STDOUT
puts write a string to STDOUT
qsort perform a quicksort
raise send a signal to the program
rand returns a pseudo-random number
realloc changes the size of previously allocated memory
remove erase a file
rename rename a file
move the file position indicator to the beginning
rewind
of a file
scanf read formatted input from STDIN
setbuf set the buffer for a specific stream
setjmp set execution to start at a certain point
setlocale sets the current locale
setvbuf set the buffer and size for a specific stream
signal register a function as a signal handler
sin sine
sinh hyperbolic sine
sprintf write formatted output to a buffer
sqrt square root
srand initialize the random number generator
sscanf read formatted input from a buffer
strcat concatenates two strings
finds the first occurrence of a character in a
strchr
string
strcmp compares two strings
compares two strings in accordance to the
strcoll
current locale
strcpy copies one string to another
strcspn searches one string for any characters in another
strerror returns a text version of a given error code
strftime returns individual elements of the date and time
strlen returns the length of a given string
concatenates a certain amount of characters of
strncat
two strings
compares a certain amount of characters of two
strncmp
strings
copies a certain amount of characters from one
strncpy
string to another
finds the first location of any character in one
strpbrk
string, in another string
finds the last occurrence of a character in a
strrchr
string
returns the length of a substring of characters of
strspn
a string
finds the first occurrence of a substring of
strstr
characters
strtod converts a string to a double
strtok finds the next token in a string
strtol converts a string to a long
strtoul converts a string to an unsigned long
converts a substring so that it can be used by
strxfrm
string comparison functions
system perform a system call
tan tangent
tanh hyperbolic tangent
time returns the current calendar time of the system
tmpfile return a pointer to a temporary file
tmpnam return a unique filename
tolower converts a character to lowercase
toupper converts a character to uppercase
ungetc puts a character back into a stream
va_arg use variable length parameter lists
write formatted output with variable argument
vprintf, vfprintf, and vsprintf
lists
vscanf, vfscanf, and vsscanf read formatted input with variable argument lists
Output:
#include <stdio.h>
int main()
{
int first, second, add, subtract, multiply;
float divide;
return 0;
}
Output :
Practical 4: WAP to swap two numbers with the help of third variable and without using
of third variable
Output
Output
Practical 5: WAP to find the entered number is even or odd
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
printf("Enter the value of A ");
scanf("%d",&a);
if(a%2==0)
printf("The no. is even");
else
printf("the no. is odd");
getch();
}
Output
Practical 6: WAP to find the largest among three numbers.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("enter the no.s");
scanf("%d%d%d",&a,&b,&c);
if(a>b)
{
if(a>c)
printf("a is big");
else
printf("c is big");
}
else if(b>c)
printf("b is big");
else
printf("c is big");
getch();
}
Output
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float a,b,c,r,r1,r2,d;
clrscr();
printf("\nEnter the co-efficients\n");
scanf("%f%f%f",&a,&b,&c);
d=(b*b)-(4*a*c);
printf("\nThe value of D is %f",d);
if(d<0)
printf("\nThe root are not real");
if(d>0)
{
r1=(-b+sqrt(d))/(2*a);
r2=(-b-sqrt(d))/(2*a);
printf("\nThe root one is %f",r1);
printf("\nThe root two is %f",r2);
}
if(d==0)
{
r=-b/(2*a);
printf("\nThe roots are equal\n");
printf("Root1=Root2=%f\n",r);
}
getch();
}
Output
Practical 8: WAP to find whether the entered character is upper case , lower case , digit
or special symbol.
#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
clrscr();
printf("Enter a character:");
scanf("%c",&ch);
if(ch>=65 && ch<=90)
{
printf("\n Upper case letter");
}
else if(ch>=97 && ch<=122)
{
printf("\n Lower case letter");
}
else if(ch>=48 && ch<=57)
{
printf("\n Digit");
}
else if((ch>=0 && ch<=47) || (ch>=58&& ch<=64) || (ch>=91 && ch<=96) || (ch>=123
&& ch<=127))
{
printf("\n Special symbol");
}
getch();
}
Output1:
Enter a Character: H
Upper case letter
Output2:
Enter a Character: g
Lower case letter
Output3:
Enter a Character: 8
Digit
Output4:
Enter a Character: @
Special symbol
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int n,m,sum=0,i=1,term;
clrscr();
printf("Enter the power of series ");
scanf("%d",&n);
printf("Enter the last no. of series ");
scanf("%d",&m);
while(i<=m)
{
term=pow(i,n);
sum=sum+term;
i++;
}
printf("The sum of term is %d",sum);
getch();
}
Output
Practical 10: WAP to find factorial of given number by using while loop.
#include<stdio.h>
#include <conio.h>
void main()
{
int n,f=1,i=1;
printf("Enter a number: ");
scanf("%d",&n);
while(i<=n)
{
f = f * i;
i++;
}
printf("factorial= %d",f);
getch();
Practical 11: WAP to check whether the entered number is palindrome or not by using
do-while.
#include<stdio.h>
#include<conio.h>
int palindrome(int num);
int rev=0,rem=0;
void main()
{
int num;
printf("\n Enter a number: ");
scanf("%d",&num);
if(num==palindrome(num))
{
printf("\n %d is palindrome",num);
}
else
{
printf("\n %d is not palindrome",num);
}
getch();
}
int palindrome(int num)
{
do
{
rem=num%10;
rev=rev*10+rem;
num=num/10;
}while(num>0);
return rev;
}
Output:
Practical 12: WAP to perform arithmetic operation on 1-D Array.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[11],b,i;
float sum=0,avg;
clrscr();
printf("Enter the value of i(1 to 11) ");
scanf("%d",&i);
for(b=1;b<=i;b++)
{ printf("Enter the values ");
scanf("%d",&a[i]);
sum=sum+a[i];
}
printf("The sum of the values is %f\n",sum);
avg=sum/i;
printf("The average of the values is %f",avg);
getch();
}
Output
Practical 13: WAP to perform arithmetic operation on 2-D Array.
// Matrix Multiplication
#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3],b[3][3],c[3][3]={0,0,0,0,0,0,0,0,0},i,j,k;
clrscr();
printf("Enter the value of first matrix ");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter the value of second matrix ");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
for(k=0;k<3;k++)
{
c[i][j]=a[i][k]*b[k][j]+c[i][j];
}
}
}
for(i=0;i<3;i++)
{
printf("\n");
for(j=0;j<3;j++)
{
printf("%d",c[i][j]);
printf("\t");
}
}
getch();
}
Output
Practical 14: WAP to perform string manipulation function.
function returns 0.
two.
strncat() Appends a given number of characters from one string to the end
of another.
pointer.
string as a pointer.
strstr() Searches for one string inside another string. The function
found.
strnstr() Searches for one string within the first n characters of the
second string. The function returns a pointer to the string’s
another.
#include <stdio.h>
#include <string.h>
int main()
{
char password[]="taco";
char input[15];
int match;
printf("Password: ");
scanf("%s",input);
match=strcmp(input,password);
if(match==0)
puts("Password accepted");
else
puts("Invalid password. Alert the authorities.");
return(0);
}
Output
Program 16: WAP to sort list of elements using Bubble sort.
#include <stdio.h>
int main()
scanf("%d", &n);
scanf("%d", &array[c]);
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
printf("%d\n", array[c]);
return 0;
Output of program:
#include <stdio.h>
int main()
{
int n, array[1000], c, d, t;
d--;
}
}
return 0;
}
Output of program:
int main()
{
int array[100], n, c, d, position, swap;
printf("Enter number of elements\n");
scanf("%d", &n);
return 0;
}
Output of program:
Practical 19: WAP to swap two numbers using call by value and call by reference.
//Call by Value
#include<stdio.h>
#include<conio.h>
void swap(int i,int j)
{
int temp=i;i=j;j=temp;
}
void main()
{
int a,b;
clrscr();
printf("enter the value ");
scanf("%d%d",&a,&b);
swap(a,b);
printf("The value after swap is %d\n%d",a,b);
getch();
}
Output
//Call by Reference
#include<stdio.h>
#include<conio.h>
void swap(int *i,int *j)
{
int temp=(*i);(*i)=(*j);(*j)=temp;
}
void main()
{
int a,b;
clrscr();
printf("enter the value ");
scanf("%d%d",&a,&b);
swap(&a,&b);
printf("The value after swap %d\n%d",a,b);
getch();
}
Output
Practical 20: WAP to find Fibonacci series using recursion.
#include<stdio.h>
#include<conio.h>
void main()
{
int a=0,b=1,c,i,n;
clrscr();
printf("Enter the value of N element ");
scanf("%d",&n);
if(n==0)
printf("%d\n",a);
else
printf("%d\t%d\t",a,b);
for(i=2;i<=n;i++)
{
c=a+b;
printf("%d\t",c);
a=b;
b=c;
}
getch();
}
Output
Program 21: WAP to sort list of elements using Quick sort.
#include<stdio.h>
if(first<last){
pivot=first;
i=first;
j=last;
while(i<j)
while(number[i]<=number[pivot]&&i<last)
i++;
while(number[j]>number[pivot])
j--;
if(i<j){
temp=number[i];
number[i]=number[j];
number[j]=temp;
}
temp=number[pivot];
number[pivot]=number[j];
number[j]=temp;
quicksort(number,first,j-1);
quicksort(number,j+1,last);
int main(){
scanf("%d",&count);
for(i=0;i<count;i++)
scanf("%d",&number[i]);
quicksort(number,0,count-1);
for(i=0;i<count;i++)
printf(" %d",number[i]);
return 0;
}
Output of program :
#include<stdio.h>
int main()
{
int a[30],n,i;
printf("Enter no of elements:");
scanf("%d",&n);
printf("Enter array elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
mergesort(a,0,n-1);
return 0;
}
if(i<j)
{
mid=(i+j)/2;
mergesort(a,i,mid); //left recursion
mergesort(a,mid+1,j); //right recursion
merge(a,i,mid,mid+1,j); //merging of two sorted sub-arrays
}
}
Output of program :
Practical 23: WAP to perform Array of Structure.
#include<stdio.h>
#include<conio.h>
struct student
{
long rollno;
char name[20];
float fee;
}s1;
void main()
{
clrscr();
printf("Enter Roll no: ");
scanf("%d",&s1.rollno);
printf("Enter student name: ");
scanf("%s",s1.name);
printf("Enter fee: ");
scanf("%f",&s1.fee);
printf("\nDetails are:\n\n");
printf("Roll no: %ld\nName: %s\nFee: %5.2f",s1.rollno,s1.name,s1.fee);
getch();
}
Output
Output
Practical 24: WAP to implement pointers.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3],b[3][3],c[3][3]={0,0,0,0,0,0,0,0,0},i,j,k;
int *p=a,*p1=b;
clrscr();
printf("Enter the value of first matrix ");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter the value of second matrix ");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&b[i][j]);
}
}printf("The sum is \n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=(*p)+(*p1);
printf("%d ",c[i][j]);
p++;
p1++;
}
printf("\n");
}
getch();
}
Output
Practical 25: WAP to perform file handling.
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *f;
clrscr();
f=fopen("Test.txt","w");
if(f==NULL)
{
printf("\n Sorry,File cannot be opened");
printf("\n Program Terminating...");
}
else
{
printf("\n File Opened");
fprintf(f,"%s","Test Successful !");
printf("\n Operation complete,closing the file.");
fclose(f);
}
getch();
}
Output
Output
Output
Output
2. To demonstrate the importance of ampersand(&)operator in scanf
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("Enter the value of A ");
scanf("%d",&a);
printf("Enter the value of B ");
scanf("%d",b);
printf("The entered value of A is %d\n",a);
printf("The entered value of B is %d",b);
getch();
}
Output
3. To demonstrate the work of %n
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
printf("C Assigenment%n\n",&a);
/*%n is used to count the character*/
printf("Total no. of character are %d",a);
getch();
}
Output
4. Display decimal, octal & hexadecimal no.
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
printf("Enter the value");
scanf("%d",&a);
printf("The hexidecimal no. is. %x",a);
printf("\nThe octal no. is. %o",a);
printf("\nThe decimal no. is. %d",a);
getch();
}
Output
Output
Output
Output
8. To demonstrate the working of preprocessor
#include<stdio.h>
#include<conio.h>
#define N 10
void main()
{
clrscr();
printf( "The result is %d\n",N*N);
getch();
}
Output
9. To demonstrate the use of bitfields
#include<stdio.h>
#include<conio.h>
struct
{
unsigned gender: 1;
unsigned grade: 4;
unsigned bloodgroup:3;
}s1;
void main()
{
clrscr();
printf("Size of student is %d bytes",sizeof(s1));
s1.gender=1;
s1.grade=5;
s1.bloodgroup=6;
printf("\nGener : %d\nGrade : %d\nBlood Group : %d"
,s1.gender,s1.grade,s1.bloodgroup);
printf("\nAddress of student is : %u",&s1);
getch();
}
Output
Output
Output
Output
13. Check the value is vowal or consonant
#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
clrscr();
printf("Enter a character ");
scanf("%c",&ch);
switch(ch)
{
case 'A':
case 'a':
case 'E':
case 'e':
case 'I':
case 'i':
case 'O':
case 'o':
case 'U':
case 'u':
printf("Entered character is vowal");
break;
default:
printf("Entered character is consonant");
break;
}
getch();
}
Output
14. Read N no. from user and find average
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,a;
float avg,sum=0;
clrscr();
printf("Who many no's you want to enter ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("Enter the no.");
scanf("%d",&a);
sum=sum+a;
}
printf("The sum of all no's is %f\n",sum);
avg=sum/n;
printf("The average is %f",avg);
getch();
}
Output
15. Factorial of given no.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,f;
clrscr();
printf("Enter the value ");
scanf("%d",&a);
f=a-1;
while(f>1)
{
a=a*f--;
}
printf("The factorial of given no. %d",a);
getch();
}
Output
16. Convert binary no. to decimal no.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
long int num,d,p=0,r=0;
clrscr();
printf("Enter the binary no.(0 or 1) ");
scanf("%ld",&num);
while(num>0)
{
d=num%10;
num=num/10;
r=r+d*pow(2,p);
p++;
}
printf("The decimal no. is %ld",r);
getch();
}
Output
17. Convert decmal no. to binary no.
#include<stdio.h>
#include<conio.h>
void main()
{
long unsigned int num,sum=0,i=1,x;
clrscr();
printf("Enter the value ");
scanf("%ld",&num);
while(num>0)
{
x=num%2;
sum=x*i+sum;
i=i*10;
num=num/2;
}
printf("After con %ld",sum);
getch();
}
Output
18. Reverse of no.
#include<stdio.h>
#include<conio.h>
void main()
{
long int a,b;
clrscr();
printf("Enter the no. ");
scanf("%ld",&a);
while(a>0)
{
b=a%10;
a=a/10;
printf("%ld",b);
}
getch();
}
Output
19. Check the no. is amstrong or not
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int a,b,c,num,count=0,sum=0,x;
clrscr();
printf("Enter the value ");
scanf("%d",&num);
a=b=num;
while(num>0)
{
x=num%10;
num=num/10;
count++;
}
while(b>0)
{
c=b%10;
sum=sum+pow(c,count);
b=b/10;
}
if(a==sum)
printf("Entered no. is Amstrong");
else
printf("Entered no. is not Amstrong");
getch();
}
Output
20. To find the sum of series 1+ + +……
#include <stdio.h>
#include <conio.h>
void main ()
{
float i,n;
float sum =0;
clrscr();
printf ("enter the terms = ");
scanf ("%f",&n);
for (i=1;i<=n;i++)
{
sum += 1/i;
}
printf ("sum=%f\n",sum);
getch();
}
Output
Output