Atul Soni - C Programming to Improve Coding Skills_ Only Learning and Algorithm Based Programs With Source Code-Independently Published (2024)
Atul Soni - C Programming to Improve Coding Skills_ Only Learning and Algorithm Based Programs With Source Code-Independently Published (2024)
by Atul Soni
COPYRIGHT © 2024 BY
ATUL SONI
All rights reserved. No part of this work may be reproduced or transmitted
in any form or by any means, electronic or mechanical, including
photocopying, recording, or by any information storage or retrieval system,
without the prior written permission of the copyright owner.
Atul Soni
is an engineer whose main interests and expertise are programming
languages, algorithms, data structure, data analytics, web technologies and
android mobile application development. He writes many programming
tutorials. He has extensive experience in teaching programming languages
to engineers and software developers.
He is a programmer, writer and android app developer. He develops many
tutorial app of different programming languages like, C, C++, Java, Python,
Android, VB.Net, C#, PHP, HTML5, CSS3, SQL & PL/SQL, JavaScript,
jQuery etc. When he’s not writing or programming, he enjoys reading
books, touring by car and spending time with his family.
TABLE OF CONTENTS
Chapter-1 : C Introduction
Chapter-2 : Variables, Constants & Data Types
Chapter-3 : Operators & Expressions
Chapter-4 : Selection
Chapter-5 : Iteration / Loop
Chapter-6 : Series
Chapter-7 : Patterns
Chapter-8 : Arrays
Chapter-9 : Strings
Chapter-10 : Functions
Chapter-11 : Structure, Union & Enum
Chapter-12 : Pointers
Chapter-13 : Dynamic Memory Allocation
Chapter-14 : Preprocessors
Chapter-15 : Input/Output Formatting
Chapter-16 : File Handling
CHAPTER − 1
C - Introduction
______________ **** ______________
Program No. - 1 :
Program to display a String Literal in C using printf() function.
Code :
#include <stdio.h>
int main()
{
printf( " Let\'s learn C Programming Language." );
return 0;
}
Output :
system( "pause" );
return 0;
}
Output :
system( "pause" );
return 0;
}
Output :
system( "pause" );
}
Output :
s = n1 + n2;
system( "pause" );
return 0;
}
Output :
s = n1 + n2;
s = n1 + n2;
system( "pause" );
return 0;
}
Output :
system( "pause" );
return 0;
}
Output :
system( "pause" );
return 0;
}
Output :
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 16 :
Program to calculate Simple Interest.
Code :
#include <stdio.h>
int main( )
{
system( "cls" );
float p, r, t, si ;
si = ( p * r * t ) / 100;
system( "pause" );
return 0;
}
Output :
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 18 :
Program to calculate the Car’s average from the given total distance
travelled (in kms) and fuel consumed (in litres).
Code :
#include <stdio.h>
int main()
{
float km, ltr, avg;
system( "cls" );
avg = km / ltr;
system( "pause" );
return 0;
}
Output :
peri = 2 * ( l + w );
printf( "Perimeter of Rectangle : %f\n", peri );
area = l * w;
printf( "Area of Rectangle : %f\n", area );
system( "pause" );
return 0;
}
Output :
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 21 :
Program to calculate the Total Surface Area of Sphere.
Code :
#include <stdio.h>
int main()
{
system( "cls" );
double radius, tsa;
system( "pause" );
return 0;
}
Output :
system( "pause" );
return 0;
}
Output :
system( "pause" );
return 0;
}
Output :
system( "pause" );
return 0;
}
Output :
hra = bs * 20 / 100 ;
da = bs * 45 / 100 ;
gs = bs + hra + da ;
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 26 :
Program to explain Simple C Program Structure.
Code :
// File Inclusion Section
#include <stdio.h>
// Global Declaration & Definition Section
int x, y = 20;
void show();
// main Function
int main()
{
int a, b = 40;
system( "cls" );
x = 10;
a = 30;
system( "pause" );
return 0;
}
// Function Definition
void show()
{
printf( "x = %d, y = %d\n", x, y );
}
Output :
______________ **** ______________
CHAPTER − 2
Variables, Constants & Data Types
______________ **** ______________
Program No. - 27 :
Program to explain Variable Declaration and Assignment.
Code :
#include <stdio.h>
int main()
{
int length; // Variable Declaration.
system( "cls" );
system( "pause" );
return 0;
}
Output :
system( "pause" );
return 0;
}
Output :
system( "pause" );
return 0;
}
Output :
system( "pause" );
return 0;
}
Output :
system( "pause" );
return 0;
}
Output :
system( "pause" );
return 0;
}
Output :
system( "pause" );
return 0;
}
Output :
system( "pause" );
return 0;
}
Output :
ch = 'A';
printf( "ch = %c\n", ch );
system( "pause" );
return 0;
}
Output :
system( "pause" );
return 0;
}
Output :
system( "pause" );
return 0;
}
Output :
system( "pause" );
return 0;
}
Output :
a = p * pow( ( 1 + ( r / 100 ) ), n ) ;
ci = a − p ;
system( "pause" );
return 0;
}
Output :
s = (a + b + c) / 2;
ar = sqrt( s * (s − a) * (s − b) * (s − c) );
system( "pause" );
return 0;
}
Output :
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 43 :
Program to display the range of different character and integer data types
using pre-defined constants.
Code :
#include <stdio.h>
#include <limits.h>
int main()
{
int i;
system( "cls" );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 44 :
Program to display the upper limit of different unsigned character and
integer data types using pre-defined constants.
Code :
#include <stdio.h>
#include <limits.h>
int main()
{
int i;
system( "cls" );
system( "pause" );
return 0;
}
Output :
var1 = ( var1 * 2 ) / 3;
var2 = ( var2 * 2 ) / 3;
system( "pause" );
return 0;
}
Output :
a = 1;
b = !a; // Not
printf( "\n!a = %d\n", b );
a = 10;
b = ++a; // Increment
printf( "\n++a = %d\n", b );
a = 10;
b = −−a; // Decrement
printf( "−−a = %d\n", b );
a = 10;
b = ~a; // Complement
printf( "\n~a = %d\n", b );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 47 :
Program to use Arithmetic operators ( + − * / % ).
Code :
#include <stdio.h>
int main()
{
int a = 20, b = 3;
int sum, sub, mul, rem, idiv ;
float rdiv;
system( "cls" );
sum = a + b;
sub = a − b;
mul = a * b;
idiv = a / b;
rem = a % b;
rdiv = (float)a / (float)b;
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 48 :
Program to explain Relational Operators ( <, >, <=, >=, ==, != ).
Code :
#include <stdio.h>
int main()
{
int n;
system( "cls" );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 49 :
Program to explain the Relational operator.
( < > <= >= == != ).
Code :
#include <stdio.h>
int main()
{
int a;
system( "cls" );
a = ( 10 < 20 );
printf( "(10 < 20) = %d\n", a ) ;
a = ( 10 > 20 );
printf( "(10 > 20) = %d\n", a ) ;
a = ( 10 <= 20 );
printf( "(10 <= 20) = %d\n", a ) ;
a = ( 10 >= 20 );
printf( "(10 >= 20) = %d\n", a ) ;
a = ( 20 == 20 );
printf( "(20 == 20) = %d\n", a ) ;
a = ( 20 != 20 );
printf( "(20 != 20) = %d\n", a ) ;
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 50 :
Program to explain Logical And ( && ) operator.
Code :
#include <stdio.h>
int main()
{
system( "cls" );
system( "pause" );
return 0;
}
Output :
system( "pause" );
return 0;
}
Output :
system( "pause" );
return 0;
}
Output :
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 54 :
Program to explain the Logical OR ( || ) operator.
Code :
#include <stdio.h>
int main()
{
int a;
system( "cls" );
a = ( 2 < 1 ) || ( 4 < 3 );
printf( "(2<1) || (4<3) = %d\n", a ) ;
a = ( 2 < 1 ) || ( 4 > 3 );
printf( "(2<1) || (4>3) = %d\n", a ) ;
a = ( 2 > 1 ) || ( 4 < 3 );
printf( "(2>1) || (4<3) = %d\n", a ) ;
a = ( 2 > 1 ) || ( 4 > 3 );
printf( "(2>1) || (4>3) = %d\n", a ) ;
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 55 :
Program to explain the Logical NOT ( ! ) operator.
Code :
#include <stdio.h>
int main()
{
int a;
system( "cls" );
a = !( 2 < 1 );
printf( "!(2<1) = %d\n", a ) ;
a = !( 2 > 1 );
printf( "!(2>1) = %d\n", a ) ;
system( "pause" );
return 0;
}
Output :
system( "pause" );
return 0;
}
Output :
Program No. - 57 :
Program to use Assignment Operator ( = ).
Code :
#include <stdio.h>
int main()
{
int a, b, c, d, e, f;
system( "cls" );
a = b = c = d = e = f = 10;
system( "pause" );
return 0;
}
Output :
num −= 5;
printf( "Num −= 5 : %d\n", num );
num *= 2;
printf( "Num *= 2 : %d\n", num );
num /= 3;
printf( "Num /= 3 : %d\n", num );
num %= 3;
printf( "Num %= 3 : %d\n", num );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 59 :
Program to explain the Prefix and Postfix Increment ( ++ ) operator.
Code :
#include <stdio.h>
int main( )
{
int a = 10, b;
system( "cls" );
b = ++a;
printf( "After Prefix Increment : " );
printf( "a = %d and b = %d\n", a, b );
a = 10;
b = a++;
printf( "After Postfix Increment : " );
printf( "a = %d and b = %d\n", a, b );
system( "pause" );
return 0;
}
Output :
Program No. - 60 :
Program to explain the Prefix and Postfix Decrement (−−) operator.
Code :
#include <stdio.h>
int main( )
{
int a = 10, b;
system( "cls" );
b = −−a;
printf( "After Prefix Decrement : " );
printf( "a = %d and b = %d\n", a, b );
a = 10;
b = a−−;
printf( "After Postfix Decrement : " );
printf( "a = %d and b = %d\n", a, b );
system( "pause" );
return 0;
}
Output :
Program No. - 61 :
Program to explain the use of Prefix Increment( ++ ) Operator.
Code :
#include <stdio.h>
int main()
{
int a = 10, b = 20 ,c;
system( "cls" );
c = (++a) + (++b);
system( "pause" );
return 0;
}
Output :
c = (a++) + (b++);
system( "pause" );
return 0;
}
Output :
c = (−−a) + (−−b);
system( "pause" );
return 0;
}
Output :
c = (a−−) + (b−−);
system( "pause" );
return 0;
}
Output :
c = a & b;
printf( "a & b = %d\n", c );
c = a | b;
printf( "a | b = %d\n", c );
c = a ^ b;
printf( "a ^ b = %d\n", c );
c = ~a;
printf( "~a = %d\n", c );
c = a << 3;
printf( "a << 3 = %d\n", c );
c = b >> 2;
printf( "b >> 2 = %d\n", c );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 66 :
Program to use sizeof() operator.
Code :
#include <stdio.h>
int main()
{
int a = 100, b;
system( "cls" );
b = sizeof( a );
printf( "The size of a : %d\n", b );
b = sizeof( double );
printf( "The size of double : %d\n", b );
b = sizeof( 123L );
printf( "The size of 123L : %d\n", b );
b = sizeof( 123.45 );
printf( "The size of 123.45 : %d\n", b );
b = sizeof( 123.45f );
printf( "The size of 123.45f : %d\n", b );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 67 :
Program to explain sizeof() operator.
Code :
#include <stdio.h>
int main()
{
system( "cls" );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 68 :
Program to explain the use Comma( , ) Operator.
Code :
#include <stdio.h>
int main()
{
int a, b, c;
system( "cls" );
system( "pause" );
return 0;
}
Output :
system( "pause" );
return 0;
}
Output :
system( "pause" );
return 0;
}
Output :
system( "pause" );
return 0;
}
Output :
max = ( (a > b) ? a : b );
system( "pause" );
return 0;
}
Output :
system( "pause" );
return 0;
}
Output :
system( "pause" );
return 0;
}
Output :
x = ( a + b ) / ( c − d );
system( "pause" );
return 0;
}
Output :
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 77 :
Program to explain Automatic or Implicit Type Conversion.
Code :
#include <stdio.h>
int main()
{
int a = 10, b = 20;
float c = 10.50;
long d = 20L, e;
system( "cls" );
e = ( ( a + c ) * d ) + b;
system( "pause" );
return 0;
}
Output :
system( "pause" );
return 0;
}
Output :
a=x/y;
printf( "Value of a (without casting ) = %f\n", a ) ;
a = (float) x / y ;
printf( "Value of a (with casting ) = %f\n", a ) ;
system( "pause" );
return 0;
}
Output :
system( "pause" );
return 0;
}
Output :
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 82 :
Program to explain Type Casting / Explicit type conversion.
Code :
#include <stdio.h>
// explicit conversion
int main()
{
system( "cls" );
int i = (int)12.5; /* double −> int */
char c = (char)65; /* int −> char */
printf( "i = %d\n", i );
printf( "c = %c\n", c );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 83 :
Program to convert Total number of days into years, months, weeks and
days.
Code :
#include <stdio.h>
int main()
{
int d, y, m, w;
system( "cls" );
y = d / 365;
d = d % 365;
m = d / 30;
d = d % 30;
w = d / 7;
d = d % 7;
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 84 :
Program to find value of Hundred, Tens and Unit place of given Three Digit
Number.
Code :
#include <stdio.h>
int main()
{
int n, u, t, h;
system( "cls" );
u = n % 10;
n = n / 10;
t = n % 10;
h = n / 10;
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 85 :
Program to Swap of two numbers using Bitwise operator.
Code :
#include <stdio.h>
int main()
{
int a, b;
system( "cls" );
a = a ^ b;
b = a ^ b;
a = a ^ b;
system( "pause" );
return 0;
}
Output :
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 87 :
Program to explain If-else statement.
Code :
#include <stdio.h>
int main()
{
int n;
system( "cls" );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 88 :
Program to find maximum of three numbers using simple if statement.
Code :
#include <stdio.h>
int main()
{
int a, b, c, max = 0;
system( "cls" );
max = a;
if( b > max )
max = b;
if( c > max )
max = c;
system( "pause" );
return 0;
}
Output :
if( a > b )
max = a;
else
max = b;
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 90 :
Program to check whether the given number is Even or Odd ?
Code :
#include <stdio.h>
int main()
{
int n;
system( "cls" );
if( n % 2 == 0 )
printf( "\n%d is an Even number.\n", n );
else
printf( "\n%d is an Odd number.\n", n );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 91 :
Program to check whether the given number is Positive or Negative ?
Code :
#include <stdio.h>
int main()
{
int n;
system( "cls" );
if( n >= 0 )
printf( "\n%d is a Positive number.\n", n );
else
printf( "\n%d is a Negative number.\n", n );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 92 :
Program to check whether the given character is Alphanumeric or not.
Code :
#include <stdio.h>
#include <ctype.h>
int main()
{
int ch;
system( "cls" );
if( isalnum( ch ) )
printf( "\nIt is an alpha-numeric character.\n" );
else
printf( "\nThis is not an alpha-numeric character.\n" );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 93 :
Program to check whether the given year is Leap year or not.
Code :
#include <stdio.h>
int main()
{
int year;
system( "cls" );
if( year % 4 == 0 )
printf( "\n%d is Leap Year.\n", year );
else
printf( "\n%d is Not a Leap Year.\n", year );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 94 :
Program to find Maximum of Three Numbers using Nested If statement.
Code :
#include <stdio.h>
int main()
{
int a, b, c, g;
system( "cls" );
if( a > b )
{
if( a > c )
g = a;
else
g=c;
}
else
{
if( b > c)
g = b;
else
g = c;
}
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 95 :
Program to find Grade using Nested If statement on the basis of Range of
Marks.
Marks Grade
80 to 100 A+
70 to 80 A
60 to 70 B
40 to 60 C
0 to 40 F
Code :
#include <stdio.h>
int main()
{
int m;
system( "cls" );
if( m >= 80 )
{
printf( "\nGrade = A+ \n" );
}
else
{
if( m >= 70 )
{
printf( "\nGrade = A \n" );
}
else
{
if( m >= 60 )
{
printf( "\nGrade = B \n" );
}
else
{
if( m >= 40 )
{
printf( "\nGrade = C \n" );
}
else
{
printf( "\nGrade = F \n" );
}
}
}
}
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 96 :
Program to check whether the given number is Positive, Negative or Zero.
Code :
#include <stdio.h> >
int main()
{
int n;
system( "cls" );
if( n > 0 )
printf( "\nPositive Number.\n" );
else if( n < 0 )
printf( "\nNegative Number.\n" );
else
printf( "\nZero.\n" );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 97 :
Program to find Maximum of three numbers using Nested If.
Code :
#include <stdio.h>
int main()
{
int n1, n2, n3;
system( "cls" );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 98 :
Program to find Maximum of three numbers using Nested If.
Code :
#include <stdio.h>
int main()
{
int n1, n2, n3, n4;
system( "cls" );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 99 :
Program to find Grade using Else If ladder statement on the basis of Range
of Marks.
Marks Grade
80 to 100 A+
70 to 80 A
60 to 70 B
40 to 60 C
0 to 40 F
Code :
#include <stdio.h>
int main()
{
int m;
system( "cls" );
if( m >= 80 )
{
printf( "\nGrade = A+ \n" );
}
else if( m >= 70 )
{
printf( "\nGrade = A \n" );
}
else if( m >= 60 )
{
printf( "\nGrade = B \n" );
}
else if( m >= 40 )
{
printf( "\nGrade = C \n" );
}
else
{
printf( "\nGrade = F \n" );
}
system( "pause" );
return 0;
}
Output :
if( y % 100 == 0 )
{
if( y % 400 == 0 )
printf( "\nIt is a LEAP Year.\n" );
else
printf( "\nIt is NOT a LEAP Year.\n" );
}
else if( y % 4 == 0 )
printf( "\nIt is a LEAP Year.\n" );
else
printf( "\nIt is NOT a LEAP Year.\n" );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 101 :
Program to create Menu Based (Choice Based) Program for Temperature
Conversion.
Code :
#include <stdio.h>
int main()
{
int choice;
float temp, cnvtemp;
system( "cls" );
if( choice == 1 )
{
printf( "\nEnter temp. in Fahrenheit : " );
scanf( "%f", &temp );
system( "pause" );
return 0;
}
Output :
if( op == '+' )
{
res = a + b;
}
else if( op == '−' )
{
res = a − b;
}
else if( op == '*' )
{
res = a * b;
}
else if( op == '/' )
{
res = a / b;
}
else if( op == '%' )
{
res = a % b;
}
else
{
printf( "Invalid operator.\n" );
}
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 103 :
Program to display the name of the week day on the basis of Week Day
Number ( 1 to 7 ).
Code :
#include <stdio.h>
int main()
{
int d;
system( "cls" );
if( d == 1 )
{
printf( "\nMonday.\n" );
}
else if( d == 2 )
{
printf( "\nTuesday.\n" );
}
else if( d == 3 )
{
printf( "\nWednesday.\n" );
}
else if( d == 4 )
{
printf( "\nThursday.\n" );
}
else if( d == 5 )
{
printf( "\nFriday.\n" );
}
else if( d == 6 )
{
printf( "\nSaturday.\n" );
}
else if( d == 7 )
{
printf( "\nSunday.\n" );
}
else
{
printf( "\nInvalid Day Number.\n" );
}
system( "pause" );
return 0;
}
Output :
switch( d )
{
case 1:
printf( "\nMonday\n" );
break;
case 2:
printf( "\nTuesday\n" );
break;
case 3:
printf( "\nWednesday\n" );
break;
case 4:
printf( "\nThursday\n" );
break;
case 5:
printf( "\nFriday\n" );
break;
case 6:
printf( "\nSaturday\n" );
break;
case 7:
printf( "\nSunday\n" );
break;
default :
printf( "\nWrong Day Number.\n" );
}
system( "pause" );
return 0;
}
Output :
if( a == 1 )
{
printf( "One.\n" );
}
else if( a == 2 )
{
printf( "Two.\n" );
}
else if( a == 3 )
{
printf( "Three.\n" );
}
else
{
printf( "Other Number.\n" );
}
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 106 :
Program to use switch-case for multiple values of single variable.
Code :
#include <stdio.h>
int main()
{
int a = 3;
system( "cls" );
switch( a )
{
case 1:
printf( "One.\n" );
break;
case 2:
printf( "Two.\n" );
break;
case 3:
printf( "Three.\n" );
break;
default:
printf( "Other Number.\n" );
break;
}
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 107 :
Program to perform Arithmatic Operation using Switch-case statement.
Code :
#include <stdio.h>
int main()
{
char op;
int a, b, res;
system( "cls" );
switch( op )
{
case '+':
res = a + b;
break;
case '−':
res = a − b;
break;
case '*':
res = a * b;
break;
case '/':
res = a / b;
break;
case '%':
res = a % b;
break;
default:
printf( "\nInvalid operator.\n" );
break;
}
system( "pause" );
return 0;
}
Output :
switch( chart )
{
case 'B':
printf( "\nDrawing Bar Chart.\n" );
break;
case 'S':
printf( "\nDrawing Scatter Chart.\n" );
break;
case 'L':
printf( "\nDrawing Line Chart.\n" );
break;
case 'P':
printf( "\nDrawing Pie Chart.\n" );
break;
default:
printf( "\nWrorng Choice.\n" );
break;
}
system( "pause" );
return 0;
}
Output :
switch( a )
{
case 100:
printf( "In Outer Switch.\n" );
switch( b )
{
case 200:
printf( "In Inner Switch.\n" );
}
}
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 110 :
Program to check whether the given character is Vowel or not.
Code :
#include <stdio.h>
int main()
{
char ch;
system( "cls" );
switch( ch )
{
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U':
printf( "\n%c is a Vowel.\n", ch );
break;
default :
printf( "\n%c is not a Vowel.\n", ch );
break;
}
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 111 :
Program to check whether the given character is Vowel or not.
Code :
#include <stdio.h>
int main()
{
char ch;
system( "cls" );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 112 :
Program to test whether a particular bit in a given number is ON or OFF
using Bitwise AND operator.
Code :
#include <stdio.h>
int main()
{
int a = 45, b ;
system( "cls" ) ;
b = a & 32 ;
if( b == 0 )
printf( " and its fifth bit is OFF.\n" ) ;
else
printf( " and its fifth bit is ON.\n" ) ;
system( "pause" );
return 0;
}
Output :
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 114 :
Program to check whether the given character is Uppercase, Lowercase ,
Digits or Special Symbols.
Code :
#include <stdio.h>
int main()
{
char ch;
system( "cls" );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 115 :
Program to check whether the given character is Uppercase, Lowercase ,
Digits or Special Symbols.
Code :
#include <stdio.h>
#include <ctype.h>
int main()
{
char ch;
system( "cls" );
if( isupper( ch ) )
printf( "\nUpper Case Letter.\n" );
else if( islower( ch ) )
printf( "\nLower Case Letter.\n" );
else if( isdigit( ch ) )
printf( "\nDigit.\n" );
else
printf( "\nSpecial Symbol.\n" );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 116 :
Program to change the Case of a character ( upper to lower or lower to
upper ).
Code :
#include <stdio.h>
int main()
{
char ch;
system( "cls" );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 117 :
Program to calculate Net Price after Discount on the basis of quantity
purchase using Conditional Operator ( ?: ).
Quantity Discount (%)
> 10 5%
> 25 10 %
> 50 15 %
Code :
#include <stdio.h>
int main()
{
const float price = 250.0;
const float discount1 = 0.05;
const float discount2 = 0.1;
const float discount3 = 0.15;
float total_price = 0.0;
int quantity = 25;
system( "cls" );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 118 :
Program to find Smallest value of 4 numbers using conditional operators (
?: ).
Code :
#include <stdio.h>
int main()
{
int a ,b, c, d, small;
system( "cls" );
system( "pause" );
return 0;
}
Output :
system( "pause" );
return 0;
}
Output :
while( n != 0 )
{
printf( "Enter No.( 0 for exit) : " );
scanf( "%d", &n );
}
system( "pause" );
return 0;
}
Output :
i = 1;
while( i <= n )
{
printf( " %d", i );
i++;
}
printf( "\n" );
system( "pause" );
return 0;
}
Output :
do
{
printf( "Enter No.( 0 for exit) : " );
scanf( "%d", &n );
}while( n != 0 );
system( "pause" );
return 0;
}
Output :
i = 1;
do
{
printf( " %d", i );
i++;
}while( i <= n );
printf( "\n" );
system( "pause" );
return 0;
}
Output :
printf( "\n" );
system( "pause" );
return 0;
}
Output :
i = 1;
for( ; i<=10 ; i++ )
{
printf( "%d ", i );
}
printf( "\n" );
system( "pause" );
return 0;
}
Output :
i = 1;
for( ; i<=10 ; )
{
printf( "%d ", i );
i++;
}
printf( "\n" );
system( "pause" );
return 0;
}
Output :
i = 1;
for( ; ; )
{
if( i > 10 )
{
break;
}
printf( "%d ", i );
i++;
}
printf( "\n" );
system( "pause" );
return 0;
}
Output :
system( "pause" );
return 0;
}
Output :
printf( "\n" );
system( "pause" );
return 0;
}
Output :
f = 1;
for( i=1 ; i<=n ; i++ )
{
f = f * i;
}
system( "pause" );
return 0;
}
Output :
i = 1;
p = 1;
while( i <= n )
{
p *= x;
i++;
}
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 132 :
Program to calculate x raise to the power n using for loop.
Code :
#include <stdio.h>
int main()
{
int x, n, p, i;
system( "cls" );
p = 1;
for( i=1 ; i<=n ; i++ )
{
p *= x;
}
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 133 :
Program to Count Number of Digits in a given Number.
Code :
#include <stdio.h>
int main()
{
long int n, c;
system( "cls" );
c = 0;
while( n != 0 )
{
n = n / 10;
c = c + 1; /* c++; */
}
system( "pause" );
return 0;
}
Output :
s = 0;
while( n != 0 )
{
r = n % 10;
s = s + r;
n = n / 10;
}
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 135 :
Program to find Reverse of given Number.
Code :
#include <stdio.h>
int main()
{
long int n, r, rev;
system( "cls" );
rev = 0;
while( n != 0 )
{
r = n % 10;
rev = ( rev * 10 ) + r;
n = n / 10;
}
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 136 :
Program to chech whether the given Number is Palindrome Number or
not.
Code :
#include <stdio.h>
int main()
{
long int n, r, x, rev;
system( "cls" );
x = n;
rev = 0;
while( n != 0 )
{
r = n % 10;
rev = ( rev * 10 ) + r;
n = n / 10;
}
if( rev == x )
printf( "It is a Palindrome Number.\n" );
else
printf( "It is Not a Palindrome Number.\n" );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 137 :
Program to check whether the given number is Perfect Number or not.
(e.g. : 6 = 1 + 2 +3 )
Code :
#include <stdio.h>
int main()
{
int i, n, s;
system( "cls" );
s = 0;
for( i=1 ; i<=n/2 ; i++ )
{
if( n % i == 0 )
{
s = s + i;
}
}
if( s == n )
{
printf( "It is a PERFECT Number.\n" );
}
else
{
printf( "It is NOT a PERFECT Number.\n" );
}
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 138 :
Program to check whether the given number is Prime Number or not.
Code :
#include <stdio.h>
int main()
{
int n, i, flag;
system( "cls" );
flag = 1;
for( i=2 ; i<=n/2 ; i++ )
{
if( n % i == 0 )
{
flag = 0;
break;
}
}
if( flag == 1 ) /* if( flag ) */
printf( "%d is a Prime Number.\n", n );
else
printf( "%d is not a Prime Number.\n", n );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 139 :
Program to check whether the given three digit number is Armstrong
Number or Not.
(e.g. : 153 = 13 + 53 + 33 )
Code :
#include <stdio.h>
int main()
{
int x, n, r, s;
system( "cls" );
x = n;
s = 0;
while( n != 0 )
{
r = n % 10;
s = s + ( r * r * r );
n = n / 10;
}
if( s == x )
printf("\n%d is a Armstrong Number.\n", x );
else
printf( "\n%d is not a Armstrong Number.\n", x );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 140 :
Program to check whether the given number is Strong Number or Not.
(e.g. : 145 = 1! + 4! + 5! )
Code :
#include <stdio.h>
int main()
{
int n, x, r, s, i, f;
system( "cls" );
x = n;
s = 0;
while( n != 0 )
{
r = n % 10;
f = 1;
for( i=1 ; i<=r ; i++ )
{
f = f * i;
}
s = s + f;
n = n / 10;
}
if( s == x )
printf( "\n%d is a STRONG Number.\n", x );
else
printf( "\n%d is not a STRONG Number.\n", x );
system( "pause" );
return 0;
}
Output :
if( a< b )
s = a;
else
s = b;
lcm = ( a * b ) / hcf;
printf( "The LCM of given Numbers = %d\n", lcm );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 142 :
Program to find LCM and HCF of Two Number.
Code :
#include <stdio.h>
int main()
{
int a, b, g, hcf, i, lcm;
system( "cls" );
if( a > b )
g = a;
else
g = b;
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 143 :
Program to find HCF and LCM of two given numbers.
Code :
#include <stdio.h>
int main()
{
int x, y, a, b, hcf, lcm, t;
system( "cls" );
if( x > y )
{
a = x;
b = y;
}
else
{
a = y;
b = x;
}
while( b != 0 )
{
t = b;
b = a % b;
a = t;
}
hcf = a;
printf( "\nHCF of %d & %d : %d\n", x, y, hcf );
lcm = (x * y) / hcf;
printf( "LCM of %d & %d : %d\n", x, y, lcm );
system( "pause" );
return 0;
}
Output :
i = 0;
while( n != 0 )
{
r = n % 10;
num = num + ( r * pow( 2, i ) );
n = n / 10;
i++;
}
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 145 :
Program to convert Octal number to Decimal number.
Code :
#include <stdio.h>
#include <math.h>
int main()
{
long i, n, r, num = 0;
system( "cls" );
i = 0;
while( n != 0 )
{
r = n % 10;
num = num + ( r * pow( 8, i ) );
n = n / 10;
i++;
}
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 146 :
Program to convert Decimal number to Binary number.
Code :
#include <stdio.h>
#include <math.h>
int main()
{
long i, n, r, num = 0;
system( "cls" );
i = 0;
while( n > 0 )
{
r = n % 2;
num = num + ( r * pow( 10, i ) );
n = n / 2;
i++;
}
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 147 :
Program to convert Decimal number to Octal number.
Code :
#include <stdio.h>
#include <math.h>
int main()
{
long i, n, r, num = 0;
system( "cls" );
i = 0;
while( n != 0 )
{
r = n % 8;
num = num + ( r * pow( 10, i ) );
n = n / 8;
i++;
}
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 148 :
Program to Display Multiplication Table of given number.
Code :
#include <stdio.h>
int main()
{
int n, i, a;
system( "cls" );
printf( "\n" );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 149 :
Program to Display Multiplication Table from 1 to 10.
Code :
#include <stdio.h>
int main()
{
int i, j, a;
system( "cls" );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 150 :
Program to find Largest value in “n” number of given numbers.
Code :
#include <stdio.h>
int main()
{
int i, n, x, large = −2147483648;
system( "cls" );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 151 :
Program to find Largest value & Smallest value in “n” number of given
numbers.
Code :
#include <stdio.h>
int main()
{
int i, n, x, large = −2147483648, small = 2147483647;
system( "cls" );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 152 :
Program to display all Leap years between two given years.
Code :
#include <stdio.h>
int main()
{
int y1, y2;
system( "cls" );
while( y1 <= y2 )
{
if( (y1 % 400 == 0) || ( y1 % 100 != 0 && y1 % 4 == 0 ))
{
printf( "\nLeap Year : %d", y1 );
}
y1++;
}
printf( "\n" );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 153 :
Program to show Cube Table of 1 to 10.
Code :
#include <stdio.h>
int main()
{
int pow = 1;
int num = 1;
system( "cls" );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 154 :
Program to calculate Squares of given values.
Code :
#include <stdio.h>
int main()
{
char another = 'y' ;
int num ;
system( "cls" );
printf( "\n" );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 155 :
Program to display all Pythogorus Triplets from 2 to N.
Code :
#include <stdio.h>
int main()
{
int i, j, k;
int num;
system( "cls" );
printf( "\n" );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 156 :
Program to explain break statement.
Code :
#include <stdio.h>
int main()
{
int i;
system( "cls" );
printf( "\n" );
system( "pause" );
return 0;
}
Output :
printf( "\n" );
system( "pause" );
return 0;
}
Output :
printf( "\n" );
system( "pause" );
return 0;
}
Output :
printf( "AAA\n" );
printf( "BBB\n" );
if( a == 10 )
{
goto LBL;
}
printf( "CCC\n" );
printf( "DDD\n" );
LBL:
printf( "EEE\n" );
printf( "FFF\n" );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 160 :
Program to explain goto statement with backward jump.
Code :
#include <stdio.h>
int main()
{
int i;
system( "cls" );
i = 1;
LABEL1:
printf( " %d", i );
i++;
if( i <= 10 )
{
goto LABEL1;
}
printf( "\n" );
system( "pause" );
return 0;
}
Output :
printf( "\n" );
system( "pause" );
return 0;
}
Output :
do
{
printf( "Enter Dividend : " );
scanf( "%ld", ÷nd );
printf( "Enter Divisor : " );
scanf( "%ld", &divisor );
if( divisor == 0 )
{
printf( "\nILLEGAL Divisor.\n" );
continue;
}
printf( "\nQuotient = %d\n", ( dividend / divisor ) );
printf( "Remainder = %d\n", ( dividend % divisor ) );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 163 :
Program to use exit() function to check whether the given Number is Prime
Number or Not.
Code :
#include <stdlib.h> /* #include <process.h> */
#include <stdio.h>
int main()
{
int n, i;
system( "cls" );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 164 :
Program to display all Perfect number between 1 to 100.
Code :
#include <stdio.h>
int main()
{
int n, i, sum;
system( "cls" );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 165 :
Program to display all Prime numbers from 2 to 100 and count it.
Code :
#include <stdio.h>
#include <math.h>
int main()
{
int i, x, n, r, flag, cnt = 0;
system( "cls" );
system( "pause" );
return 0;
}
Output :
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 167 :
Program to display all Strong Numbers between 1 and 100000.
Code :
#include <stdio.h>
int main()
{
int n, r, sum, f, i, j;
system( "cls" );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 168 :
Program to display Truth Table for Boolean expression xy+z.
Code :
#include <stdio.h>
int main()
{
int x, y, z;
system( "cls" );
printf( "x\ty\tz\txy+z\n" );
printf( "___________________________" );
for( x=0 ; x<=1 ; x++ )
{
for( y=0 ; y<=1 ; y++ )
{
for( z=0 ; z<=1 ; z++ )
{
if( ((x==1) && (y==1)) || (y==1) )
printf( "\n%d\t%d\t%d\t1", x, y, z );
else
printf( "\n%d\t%d\t%d\t0", x, y, z );
}
}
}
printf( "\n" );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 169 :
Program to calculate Lucky Number from Date of Birth.
Code :
#include <stdio.h>
int main()
{
int num, sum, r;
system( "cls" );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 170 :
Program to find Prime Factors of given number.
Code :
#include <stdio.h>
int main()
{
int num, i=1, j, cnt;
system( "cls" );
printf( "\n" );
for( i=1 ; i<=n ; i++ )
{
printf( "%d ", i );
}
printf( "\n" );
system( "pause" );
return 0;
}
Output :
s = 0;
for( i=1 ; i<=n ; i++ )
{
s = s + i;
}
printf( "\nSum of Series = %d\n", s );
system( "pause" );
return 0;
}
Output :
s = 0;
for( i=1 ; i<=n ; i++ )
{
if( i == 1 )
printf( "%d", i );
else
printf( " + %d", i );
s = s + i;
}
printf( " = %d\n", s );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 174 :
Program to display first n terms of given series :
2 4 6 8 ... ... n terms
Code :
#include <stdio.h>
int main()
{
int i, n;
system( "cls" );
printf( "\n" );
for( i=2 ; i<=(2*n) ; i+=2 )
{
printf( "%d ", i );
}
printf( "\n" );
system( "pause" );
return 0;
}
Output :
printf( "\n" );
for( i=1 ; i<=(2*n) ; i++ )
{
if( i % 2 == 0 )
{
printf( "%d ", i );
}
}
printf( "\n" );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 176 :
Program to display first n terms of given series :
2 4 6 8 ... ... n terms
Code :
#include <stdio.h>
int main()
{
int i, n, t;
system( "cls" );
printf( "\n" );
for( i=1 ; i<=n ; i++ )
{
t = 2 * i;
printf( "%d ", t );
}
printf( "\n" );
system( "pause" );
return 0;
}
Output :
printf( "\n" );
for( i=1 ; i<=(2*n) ; i+=2 )
{
printf( "%d ", i );
}
printf( "\n" );
system( "pause" );
return 0;
}
Output :
printf( "\n" );
for( i=1 ; i<=(2*n) ; i++ )
{
if( i % 2 == 1 ) /* if( i % 2 != 0 ) */
{
printf( "%d ", i );
}
}
printf( "\n" );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 179 :
Program to display first n terms of given series :
1 3 5 7 ... ... n terms
Code :
#include <stdio.h>
int main()
{
int i, n, t;
system( "cls" );
printf( "\n" );
for( i=1 ; i<=n ; i++ )
{
t = 2 * i − 1;
printf( "%d ", t );
}
printf( "\n" );
system( "pause" );
return 0;
}
Output :
printf( "\n" );
for( i=1 ; i<=n ; i++ )
{
t = i * pow( (−1), i );
printf( "%d ", t );
}
printf( "\n" );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 181 :
Program to display first n terms of given series :
1 −2 3 −4 5 ... ... n terms.
Code :
#include <stdio.h>
#include <math.h>
int main()
{
int i, n, t;
system( "cls" );
printf( "\n" );
for( i=1 ; i<=n ; i++ )
{
t = i * pow( (−1), (i + 1) );
printf( "%d ", t );
}
printf( "\n" );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 182 :
Program to find the sum of all Even values & all Odd values in “n” number
of terms of natural number series.
Code :
#include <stdio.h>
int main()
{
int n, i, esum = 0, osum = 00;
system( "cls" );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 183 :
Program to display the squares of “n” number of terms of natural number
series and also find the sum.
1 4 9 16 25 36 … n terms
Code :
#include <stdio.h>
int main()
{
int i, t, s, n;
system( "cls" );
s = 0;
for( i=1 ; i<=n ; i++ )
{
t = i * i;
printf( " %d", t );
s = s + t;
}
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 184 :
Program to display “n” terms of given series.
1 2 4 8 16 32 … n terms
Code :
#include <stdio.h>
#include <math.h>
int main()
{
int i, t, n;
system( "cls" );
printf( "\n" );
system( "pause" );
return 0;
}
Output :
x = x * ( 3.14159 / 180 );
s = 0;
for( i=1 ; i<=n ; i++ )
{
f = 1;
for( j=1 ; j<=(2*i-1) ; j++ )
{
f = f * j;
}
t = ( pow( x, (2*i−1) ) * pow( −1, (i+1) ) ) / f;
s = s + t;
}
system( "pause" );
return 0;
}
Output :
x = x * ( 3.14159 / 180 );
s = 1;
for( i=1 ; i<n ; i++ )
{
f = 1;
for( j=1 ; j<=(2*i) ; j++ )
{
f = f * j;
}
t = ( pow( x, (2*i) ) * pow( −1, i ) ) / f;
s = s + t;
}
system( "pause" );
return 0;
}
Output :
s = 0;
for( i=1 ; i<=n ; i++ )
{
t = i * pow( x, i );
s = s + t;
}
printf( "\nSum of Series = %d\n", s );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 188 :
Program to find the sum first n terms of given series :
1 + 1/2 + 1/3 + 1/4 + ... ... n terms
Code :
#include <stdio.h>
int main()
{
int i, n;
float t, s;
system( "cls" );
s = 0.0;
for( i=1 ; i<=n ; i++ )
{
t = 1.0 / i;
s = s + t;
}
printf( "\nSum of Series = %f\n", s );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 189 :
Program to find the sum first n terms of given series :
1/2 + 2/3 + 3/4 + ... ... n terms
Code :
#include <stdio.h>
int main()
{
int i, n;
float t, s;
system( "cls" );
s = 0.0;
for( i=1 ; i<=n ; i++ )
{
t = (float) i / ( i + 1 );
s = s + t;
}
printf( "\nSum of Series = %f\n", s );
system( "pause" );
return 0;
}
Output :
s = 0.0;
for( i=1 ; i<=n ; i++ )
{
t = (float) i / ( ( i + 1 ) * ( i + 2 ) );
s = s + t;
}
printf( "\nSum of Series = %f\n", s );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 191 :
Program to find the sum first n terms of given series :
1/1! + 1/2! + 1/3! + ... ... n terms
Code :
#include <stdio.h>
int main()
{
int i, n, j, f;
float t, s;
system( "cls" );
s = 0.0;
for( i=1 ; i<=n ; i++ )
{
f = 1;
for( j=1 ; j<=i ; j++ )
{
f = f * j;
}
t = 1.0 / f;
s = s + t;
}
printf( "\nSum of Series = %f\n", s );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 192 :
Program to find the sum first n terms of given series :
−(2x1)/1! + (3x2)/2! − (4x3)/3! + ... ... n terms
Code :
#include <stdio.h>
#include <math.h>
int main()
{
int i, n, x, j, f;
float t, s;
system( "cls" );
s = 0.0;
for( i=1 ; i<=n ; i++ )
{
f = 1;
for( j=1 ; j<=i ; j++ )
{
f = f * j;
}
t = ( ( i+1 ) * pow( x, i ) * pow( −1, i ) ) / f;
s = s + t;
}
printf( "\nSum of Series = %f\n", s );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 193 :
Program to find the sum first n terms of given series :
1 + (1+2) + (1+2+3) + ... ... + n terms
Code :
#include <stdio.h>
int main()
{
int i, n, j, t, s;
system( "cls" );
s = 0;
for( i=1 ; i<=n ; i++ )
{
t = 0;
for( j=1 ; j<=i ; j++ )
{
t = t + j;
}
s = s + t;
}
printf( "\nSum of Series = %d\n", s );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 194 :
Program to display first n terms of Fibonacci Series.
0 1 1 2 3 5 ... ... n terms
Code :
#include <stdio.h>
int main()
{
int i, n, a, b, c;
system( "cls" );
a = 0;
b = 1;
printf( "%d %d ", a, b );
for( i=1 ; i<=(n−2) ; i++ )
{
c = a + b;
printf( "%d ", c );
a = b;
b = c;
}
printf( "\n" );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 195 :
Program to display first n terms of Lucas Series.
1 1 1 3 5 9 ... ... n terms
Code :
#include <stdio.h>
int main()
{
int i, n, a, b, c, d;
system( "cls" );
a = 1;
b = 1;
c = 1;
printf( "%d %d %d ", a, b, c );
for( i=1 ; i<=(n−3) ; i++ )
{
d = a + b + c;
printf( "%d ", d );
a = b;
b = c;
c = d;
}
printf( "\n" );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 196 :
Program to display first n terms of following Series.
1 11 111 1111 ... ... n terms
Code :
#include <stdio.h>
int main()
{
int i, n, t;
system( "cls" );
t = 0;
for( i=0 ; i<n ; i++ )
{
t = t * 10 + 1;
printf( "%d ", t );
}
printf( "\n" );
system( "pause" );
return 0;
}
Output :
t = 0;
for( i=0 ; i<n ; i++ )
{
t = t + pow( 10, i );
printf( "%d ", t );
}
printf( "\n" );
system( "pause" );
return 0;
}
Output :
printf( "\n" );
system( "pause" );
return 0;
}
Output :
sum = 0;
for( i=1 ; i<=n ; i++ )
{
t = i * pow( (−1), ( i + 1 ) ) * pow( x, i );
sum = sum + t;
}
system( “pause” );
return 0;
}
Output :
______________ **** ______________
Program No. - 200 :
Program to display first n terms of given Series.
1 4 9 16 ... ... n terms
Code :
#include <stdio.h>
int main()
{
int n, i, t;
system( "cls" );
printf( "\n" );
system( "pause" );
return 0;
}
Output :
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 202 :
Program to display the following design pattern :
1
2 2
3 3 3
4 4 4 4
Code :
#include <stdio.h>
int main()
{
int i, j, n;
system( "cls" );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 203 :
Program to display the following design pattern :
*
* *
* * *
* * * *
Code :
#include <stdio.h>
#include <conio.h>
int main()
{
int i, j, n;
system( "cls" );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 204 :
Program to display the following design pattern :
A
A B
A B C
A B C D
Code :
#include <stdio.h>
int main()
{
int i, j, n;
system( "cls" );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 205 :
Program to display the following design pattern :
a
a b
a b c
a b c d
Code :
#include <stdio.h>
int main()
{
int i, j, n;
system( "cls" );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 206 :
Program to display the following design pattern :
A
B B
C C C
D D D D
Code :
#include <stdio.h>
int main()
{
int i, j, n;
system( "cls" );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 207 :
Program to display the following design pattern :
a
b b
c c c
d d d d
Code :
#include <stdio.h>
int main()
{
int i, j, n;
system( "cls" );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 208 :
Program to display the following design pattern :
1
12
123
1 2 3 4
Code :
#include <stdio.h>
int main()
{
int i, j, k, n;
system( "cls" );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 209 :
Program to display the following design pattern :
1
121
12321
1 2 3 4 3 2 1
Code :
#include <stdio.h>
int main()
{
int i, j, k, l, n;
system( "cls" );
system( "pause" );
return 0;
}
Output :
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 211 :
Program to display the following design pattern :
1
1 1
1 1
1 1
1 1
1 1
1
Code :
#include <stdio.h>
int main()
{
int i, j, k, l, n;
system( "cls" );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 212 :
Program to display the following design pattern :
1
1 1
1 1
1 4 1
1 1
1 1
1
Code :
#include <stdio.h>
int main()
{
int i, j, k, l, n;
system( "cls" );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 213 :
Program to display the following design pattern :
1
1 1
1 1
1234321
1 1
1 1
1
Code :
#include <stdio.h>
int main()
{
int i, j, k, l, n;
system( "cls" );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 214 :
Program to display the following design pattern :
1
121
1 3 1
1 4 1
1 3 1
121
1
Code :
#include <stdio.h>
int main()
{
int i, j, k, l, n;
system( "cls" );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 215 :
Program to display the following design pattern (Floyd’s Triangle) :
1
2 3
4 5 6
7 8 9 10
Code :
#include <stdio.h>
int main()
{
int i, j, n, a;
system( "cls" );
a = 1;
for( i=1 ; i<=n ; i++ )
{
for( j=1 ; j<=i ; j++ )
{
printf( "%3d", a );
a++;
}
printf( "\n" );
}
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 216 :
Program to display the following design pattern :
1
0 0
1 1 1
0 0 0 0
Code :
#include <stdio.h>
int main()
{
int i, j, n;
system( "cls" );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 217 :
Program to display the following design pattern :
1
0 0
1 1 1
0 0 0 0
Code :
#include <stdio.h>
int main()
{
int i, j, n;
system( "cls" );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 218 :
Program to display the following design pattern :
1
1 0
1 0 1
1 0 1 0
Code :
#include <stdio.h>
int main()
{
int i, j, n;
system( "cls" );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 219 :
Program to display the following design pattern :
1
1 0
1 0 1
1 0 1 0
Code :
#include <stdio.h>
int main()
{
int i, j, n;
system( "cls" );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 220 :
Program to display the following design pattern :
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
Code :
#include <stdio.h>
int main()
{
int i, j, n;
system( "cls" );
printf( "\n" );
for( i=1 ; i<=n ; i++ )
{
for( j=1 ; j<=n ; j++ )
{
printf( " *" );
}
printf( "\n" );
}
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 221 :
Program to display the following design pattern :
* * * *
*
* *
* *
* *
* * * * *
Code :
#include <stdio.h>
int main()
{
int i, j, n;
system( "cls" );
printf( "\n" );
for( i=1 ; i<=n ; i++ )
{
for( j=1 ; j<=n ; j++ )
{
if( i == 1 || i == n || j == 1 || j == n )
printf( " *" );
else
printf( " " );
}
printf( "\n" );
}
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 222 :
Program to display the following design pattern :
* * * * *
* * * *
* * *
* * * *
* * * * *
Code :
#include <stdio.h>
int main()
{
int i, j, n;
system( "cls" );
printf( "\n" );
for( i=1 ; i<=n ; i++ )
{
for( j=1 ; j<=n ; j++ )
{
if( i == 1 || i == n || j == 1 || j == n || i == j ||
( i+j ) == (n+1) )
printf( " *" );
else
printf( " " );
}
printf( "\n" );
}
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 223 :
Program to display the following design pattern :
1
123
12345
1234567
Code :
#include <stdio.h>
int main()
{
int i, j, k, n;
system( "cls" );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 224 :
Program to display the following design pattern :
C
C O
C O M
C O M P
C O M P U
C O M P U T
C O M P U T E
C O M P U T E R
Code :
#include <stdio.h>
#include <string.h>
int main()
{
int i, j, k;
char str[25];
system( "cls" );
k = strlen( str );
for( i=0 ; i< k ; i++ )
{
for( j=0 ; j <= i ; j++ )
{
printf( " %c", str[ j ] );
}
printf( "\n" );
}
system( "pause" );
return 0;
}
Output :
l = strlen( str );
for( i=0 ; i< l ; i++ )
{
for( k=0 ; k <i ; k++ )
{
printf( " " );
}
for( j=i ; j < l ; j++ )
{
printf( " %c", str[ j ] );
}
printf( "\n" );
}
system( "pause" );
return 0;
}
Output :
system( "pause" );
return 0;
}
Output :
printf( "\n" );
for( i=1 ; i<=n ; i++ )
{
for( j=1 ; j<=n ; j++ )
{
printf( "%3d", ( i * j ) );
if( i == j )
break;
}
printf( "\n" );
}
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 229 :
Program to explain use of break statement with following pattern :
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Code :
#include <stdio.h>
int main()
{
int i, j;
system( "cls" );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 230 :
Program to explain use of break statement with following pattern :
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Code :
#include <stdio.h>
int main()
{
int i, j;
system( "cls" );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 231 :
Program to explain use of break statement with following pattern :
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
Code :
#include <stdio.h>
int main()
{
int i, j;
system( "cls" );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
CHAPTER – 8
Arrays
______________ **** ______________
Program No. - 232 :
Program to use Simple Array.
Code :
#include <stdio.h>
int main()
{
int num[5];
int i;
system( "cls" );
printf( "\n" );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 233 :
Program to explain Initialization of an Array.
Code :
#include <stdio.h>
int main()
{
int i;
int num[10] = { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 };
system( "cls" );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 234 :
Program to find Average of an Array.
Code :
#include <stdio.h>
int main()
{
float a[5];
float s, avg;
int i;
system( "cls" );
s = 0.0;
for( i=0 ; i<5 ; i++ )
{
s = s + a[ i ];
}
avg = s / 5;
printf( "\nSum : %f\n", s );
printf( "Average : %f\n", avg );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 235 :
Program to twice the value of all elements of Array.
Code :
#include <stdio.h>
int main()
{
int ar[50], n,i;
system( "cls" );
printf( "\n" );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 236 :
Program to find Maximum and Minimum of Array elements.
Code :
#include <stdio.h>
int main()
{
int i, arr[5], min, max;
system( "cls" );
min = arr[0];
max = arr[0];
for( i=1 ; i<5 ; i++ )
{
if( min > arr[ i ] )
min = arr[ i ];
if( max < arr[ i ] )
max = arr[ i ];
}
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 237 :
Program to search an element in array using Linear Search.
Code :
#include <stdio.h>
int main()
{
int num[10], i, pos = −1, value;
system( "cls" );
if( pos == −1 )
printf( "\nThe element %d not found.\n", value );
else
printf( "\nThe position of %d : %d\n", value, pos );
system( "pause" );
return 0;
}
Output :
beg = 0;
end = 10 − 1;
while( beg<= end )
{
mid = ( beg + end ) / 2;
if( value == num[mid] )
{
pos = mid + 1;
break;
}
else if( value > num[mid] )
beg = mid + 1;
else
end = mid − 1;
}
if( pos == −1 )
printf( "\nThe element %d not found.\n", value );
else
printf( "\nThe position of %d : %d\n" , value, pos );
system( "pause" );
return 0;
}
Output :
printf( "\n" );
system( "pause" );
return 0;
}
Output :
printf( "\n" );
system( "pause" );
return 0;
}
Output :
printf( "\n" );
system( "pause" );
return 0;
}
Output :
n[0] = −2147483648;
for( i=1 ; i<=5 ; i++ )
{
tmp = n[ i ];
j = i − 1;
while( tmp < n[ j ] )
{
n[ j + 1 ] = n[ j ];
j−−;
}
n[ j + 1 ] = tmp;
}
printf( "\n" );
system( "pause" );
return 0;
}
Output :
a[0] = 0;
a[1] = 1;
for( i=2 ; i<n ; i++ )
{
a[ i ] = a[ i – 1 ] + a[ i – 2 ];
}
printf( "\n" );
for( i=0 ; i<n ; i++ )
{
printf( " %d", a[ i ] );
}
printf( "\n" );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 244 :
Program to check whether the given number is Unique Number or Not.
Code :
#include <stdio.h>
int main()
{
int i, t, n, x, r, flag;
int a[10] = {0};
system( "cls" );
x = n;
while( n != 0 )
{
r = n % 10;
a[ r ]++;
n = n / 10;
}
flag = 1;
for( i=0 ; i<10 ; i++ )
{
if( a[ i ] > 1 )
{
flag = 0;
break;
}
}
if( flag ) // if( flag == 1 )
printf( "\n%d is a Unique Number.\n", x );
else
printf( "\n%d is not a Unique Number.\n", x );
system( "pause" );
return 0;
}
Output :
int main()
{
int a[5], b[5];
int i, t;
system( "cls" );
printf( "\n" );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 246 :
Program to find First Biggest and Second Biggest of array elements.
Code :
#include <stdio.h>
#include <limits.h>
int main()
{
int a[5];
int i, t, big, sbig;
system( "cls" );
big = a[0];
for( i=1 ; i<5 ; i++ )
{
if( big < a[ i ] )
big = a[ i ];
}
printf( "\n" );
system( "pause" );
return 0;
}
Output :
printf( "\n" );
system( "pause" );
return 0;
}
Output :
flag = 0;
for( i=0 ; i<n; i++ )
{
if( value == ar[ i ] )
{
for( j=i ; j<n−1 ; j++ )
{
ar[ j ] = ar[ j+1];
}
n−−;
flag = 1;
}
}
printf( "\n" );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 249 :
Program to remove all duplicate elements from array.
Code :
#include <stdio.h>
int main()
{
int ar[25], pos, i, j, k, n, flag;
system( "cls" );
flag = 0;
for( i=0 ; i<n; i++ )
{
for( j=i+1 ; j<n ; j++ )
{
if( ar[ j ] == ar[ i ] )
{
for( k=j ; k<(n−1) ; k++ )
{
ar[k] = ar[k+1];
}
n−−;
flag = 1;
}
}
}
printf( "\n" );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 250 :
Program to Delete an element at specific location in array.
Code :
#include <stdio.h>
int main()
{
int ar[25], pos, i, n;
system( "cls" );
printf( "\n" );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 251 :
Program to Input and display a Matrix ( 2-D Array ).
Code :
#include <stdio.h>
int main()
{
int a[2][3];
int i, j;
system( "cls" );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 252 :
Program to Initialize a Matrix ( 2-D Array ).
Code :
#include <stdio.h>
int main()
{
int a[2][3] = { { 10, 20, 30 },
{ 40, 50, 50 } };
int i, j;
system( "cls" );
system( "pause" );
return 0;
}
Output :
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 254 :
Program to find Transpose of given Matrix.
Code :
#include <stdio.h>
int main()
{
int a[2][3], b[3][2];
int i, j;
system( "cls" );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 255 :
Program to Transpose a Square Matrix without using Second matrix.
Code :
#include <stdio.h>
int main()
{
int a[10][10];
int n, i, j, temp;
system( "cls" );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 256 :
Program to Multiply Two Matrices.
Code :
#include <stdio.h>
int main()
{
int a[2][3], b[3][2], c[2][2];
int i, j, k;
system( "cls" );
system( "pause" );
return 0;
}
Output :
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 258 :
Program to find Row-wise Sum and Column-wise Sum and Sum of all
elements of Matrix.
Code :
#include <stdio.h>
int main()
{
int a[3][3];
int i, j, gt=0;
int rs[3] = {0}, cs[3] = {0};
system( "cls" );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 259 :
Program to check whether the Two Matrices are Equal or not.
Code :
#include <stdio.h>
int main()
{
int n1[5][5], n2[5][5];
int i, j, r, c, flag;
system( "cls" );
flag = 0;
for( i=0 ; i<r ; i++ )
{
for( j=0 ; j<c ; j++ )
{
if( n1[ i ][ j ] != n2[ i ][ j ] )
{
flag = 1;
break;
}
}
if( flag == 1 ) /* if( flag ) */
break;
}
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 260 :
Program to check whether the given matrix is Identity Matrix or Not.
Code :
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a[5][5];
int i, j, r, c, flag;
system( "cls" );
if( r != c )
{
printf("\nNot an Identity Matrix (rows!=columns).\n" );
exit(0);
}
flag = 1;
for( i=0 ; i<r ; i++ )
{
for( j=0 ; j<c ; j++ )
{
if( (i==j && a[i][j] != 1) || (i!=j && a[i][j] != 0) )
{
flag = 0;
break;
}
}
if( flag == 0 ) /* if( ! flag ) */
break;
}
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 261 :
Program to check whether the given matrix is Sparse Matrix or Not.
Code :
#include <stdio.h>
int main()
{
int a[5][5];
int i, j, r, c, zcnt=0;
system( "cls" );
if( a[ i ][ j ] == 0 )
zcnt++;
}
}
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 262 :
Program to find No. of even values and No. of odd values in given matrix.
Code :
#include <stdio.h>
int main()
{
int a[5][5];
int i, j, r, c, ecnt, ocnt;
system( "cls" );
ecnt = ocnt = 0;
for( i=0 ; i<r ; i++ )
{
for( j=0 ; j<c ; j++ )
{
if( a[ i ][ j ] % 2 == 0 )
ecnt++;
if( a[ i ][ j ] % 2 != 0 )
ocnt++;
}
}
system( "pause" );
return 0;
}
Output :
system( "pause" );
return 0;
}
Output :
system( "pause" );
return 0;
}
Output :
system( "pause" );
return 0;
}
Output :
system( "pause" );
return 0;
}
Output :
s3[0] = 'A';
s3[1] = 't';
s3[2] = 'u';
s3[3] = 'l';
s3[4] = '\0';
system( "pause" );
return 0;
}
Output :
system( "pause" );
return 0;
}
Output :
puts( s1 );
system( "pause" );
return 0;
}
Output :
system( "pause" );
return 0;
}
Output :
system( "pause" );
return 0;
}
Output :
system( "pause" );
return 0;
}
Output :
system( "pause" );
return 0;
}
Output :
if( d == 0 )
printf( "Strings are same.\n" );
else
printf( "Strings are different.\n" );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 275 :
Program to use strcmpi() function to Compare String.
Code :
#include <stdio.h>
#include <string.h>
int main()
{
char pass[20] = "INDIA", ps[20];
int ctr = 0;
system( "cls" );
if( ctr == 0 )
printf( "\nCorrect password!!!\n" );
else
printf( "\nWrong password!!!\n" );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 276 :
Program to Swap Two Strings.
Code :
#include <stdio.h>
#include <string.h>
int main()
{
char str1[25], str2[25], t[25];
system( "cls" );
strcpy( t, str1 );
strcpy( str1, str2 );
strcpy( str2, t );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 277 :
Program to explain the use of strrev() string library function .
Code :
#include <stdio.h>
#include <string.h>
int main()
{
char s1[25] = "GREEN";
system( "cls" );
strrev( s1 );
system( "pause" );
return 0;
}
Output :
strcpy( s2, s1 );
strrev( s2 );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 279 :
Program to Remove all Vowels from given string.
Code :
#include <stdio.h>
#include <string.h>
int main()
{
char s1[25] = "INDIA";
char s2[25];
int i, j;
system( "cls" );
j = 0;
for( i=0 ; s1[ i ]!='\0' ; i++ )
{
if( s1[ i ]!='A' && s1[ i ]!='a' && s1[ i ]!='E' &&
s1[ i ]!='e' && s1[ i ]!='I' && s1[ i ]!='i' &&
s1[ i ]!='O' && s1[ i ]!='o' && s1[ i ]!='U' &&
s1[ i ]!='u' )
{
s2[ j ] = s1[ i ];
j++;
}
}
s2[ j ] = '\0';
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 280 :
Program to Remove all Vowels from the given string without using second
string.
Code :
#include <stdio.h>
int main()
{
char s1[25] = "INDIA";
int i, j;
system( "cls" );
i = 0;
while( s1[ i ] != '\0' )
{
if( s1[ i ]=='A' || s1[ i ]=='a' || s1[ i ]=='E' ||
s1[ i ]=='e' || s1[ i ]=='I' || s1[ i ]=='i' ||
s1[ i ]=='O' || s1[ i ]=='o' || s1[ i ]=='U' ||
s1[ i ]=='u' )
{
for( j=i ; s1[ j ]!='\0' ; j++ )
s1[ j ] = s1[ j+1];
}
else
i++;
}
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 281 :
Program to find Length of String without using strlen() function.
Code :
#include <stdio.h>
int main()
{
char str[50];
int i, len;
system( "cls" );
len = i;
system( "pause" );
return 0;
}
Output :
system( "pause" );
return 0;
}
Output :
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 284 :
Program to Reverse a String without using String function.
Code :
#include <stdio.h>
int main()
{
char str1[25], str2[25];
int i, j, l;
system( "cls" );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 285 :
Program to check whether the Input String is Palindrome or not.
Code :
#include <stdio.h>
int main()
{
char str[26];
int i, j, len, flag;
system( "cls" );
flag = 1;
for( i=0, j=len−1 ; i<len/2 ; i++, j−− )
{
if( str[ i ] != str[ j ] )
{
flag = 0;
break;
}
}
if( flag == 1 ) /* if( flag ) */
printf( "\nIt is Palindrome.\n" );
else
printf( "\nIt is not a Palindrome.\n" );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 286 :
Program to Reverse a String without using String functions and Second
String.
Code :
#include <stdio.h>
int main()
{
int i, j, len;
char ch, str[50];
system( "cls" );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 287 :
Program to Count the Number of Vowels in the Sentence.
Code :
#include <conio.h>
int main()
{
int i, j, c;
char str[50];
system( "cls" );
c = 0;
for( i=0 ; str[ i ] != '\0' ; i++ )
{
if( ( str[ i ]=='a' ) || ( str[ i ]=='e' ) || ( str[ i ]=='i' ) ||
( str[ i ]=='o' ) || ( str[ i ]=='u' ) || ( str[ i ]=='A' ) ||
( str[ i ]=='E' ) || ( str[ i ]=='I' ) || ( str[ i ]=='O' ) ||
( str[ i ]=='U' ) )
{
c++;
}
}
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 288 :
Program to convert lowercase characters of string to uppercase characters.
Code :
#include <stdio.h>
#include <ctype.h>
int main()
{
char s1[ ] = "india";
int i;
system( "cls" );
system( "pause" );
return 0;
}
Output :
system( "pause" );
return 0;
}
Output :
c = 0;
for( i=0 ; str[ i ] != '\0' ; i++ )
{
switch( str[ i ] )
{
case 'a':
case 'A':
case 'e':
case 'E':
case 'i':
case 'I':
case 'o':
case 'O':
case 'u':
case 'U':
c++;
}
}
system( "pause" );
return 0;
}
Output :
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No.- 292 :
Program to show the use of while loop in string.
Code :
#include <stdio.h>
int main()
{
char name[ ] = "Atul Kumar Soni" ;
int i = 0 ;
system( "cls" );
printf( "\n" );
system( "pause" );
return 0;
}
Output :
printf( "\n" );
system( "pause" );
return 0;
}
Output :
len = strlen( s1 );
for( i=0 ; i<len-1 ; i++ )
{
for( j=0 ; j<len−1−i ; j++ )
{
if( s1[ j ] > s1[ j+1] )
{
ch = s1[ j ];
s1[ j ] = s1[ j+1];
s1[ j+1] = ch;
}
}
}
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 295 :
Program to check whether input character is Capital letter, Small letter,
Digit or Special Symbol.
Code :
#include <stdio.h>
#include <ctype.h>
int main()
{
char ch;
system( "cls" );
if( isupper( ch ) )
printf( "\nCharacter %c is in Upper Case.\n", ch );
else if( islower( ch ) )
printf( "\nCharacter %c is in Lower Case.\n", ch );
else if( isdigit( ch ) )
printf( "\nCharacter %c is a Digit.\n", ch );
else
printf( "\nCharacter %c is a Special Symbol.\n", ch );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 296 :
Program to count number of Capital letters, Small letters, Digits and Special
Symbols.
Code :
#include <stdio.h>
int main()
{
int i, c = 0, sm = 0, d = 0, ss = 0;
char s[50];
system( "cls" );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 297 :
Program to count number of Capital letters, Small letters, Digits and Special
Symbols.
Code :
#include <stdio.h>
int main()
{
int i, c = 0, sm = 0, d = 0, ss = 0;
char s[50];
system( "cls" );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 298 :
Program to count number of Capital letters, Small letters, Digits and Special
Symbols.
Code :
#include <stdio.h>
#include <ctype.h>
int main()
{
int i, c = 0, sm = 0, d = 0, ss = 0;
char s[50];
system( "cls" );
system( "pause" );
return 0;
}
Output :
system( "pause" );
return 0;
}
Output :
system( "pause" );
return 0;
}
Output :
i = 0;
while( num ) /* while( num != 0 ) */
{
dig = num % 10;
num_names[ i++ ] = word[ dig ];
num = num / 10;
}
printf( "\n" );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 302 :
Program to convert each digits of a number in English word.
Code :
#include <stdio.h>
int main()
{
int num, i=0, j, dig;
char *num_names[10];
system( "cls" );
i = 0;
while( num )
{
dig = num % 10;
num = num / 10;
switch( dig )
{
case 0: num_names[ i ] = "Zero"; break;
case 1: num_names[ i ] = "One"; break;
case 2: num_names[ i ] = "Two"; break;
case 3: num_names[ i ] = "Three"; break;
case 4: num_names[ i ] = "Four"; break;
case 5: num_names[ i ] = "Five"; break;
case 6: num_names[ i ] = "Six"; break;
case 7: num_names[ i ] = "Seven"; break;
case 8: num_names[ i ] = "Eight"; break;
case 9: num_names[ i ] = "Nine"; break;
}
i++;
}
printf( "\n" );
system( "pause" );
return 0;
}
Output :
system( "pause" );
return 0;
}
void show( int x ) // Function Definition
{
printf( "x = %d\n", x );
}
Output :
show( a );
system( "pause" );
return 0;
}
Output :
s1 = sum( 10, 20 );
system( "pause" );
return 0;
}
int sum( int x, int y )
{
return (x + y);
}
Output :
system( "pause" );
return 0;
}
int sum( int x, int y )
{
return (x + y);
}
Output :
Simp_Int();
system( "pause" );
return 0;
}
void Simp_Int()
{
float p, r, t, si;
printf( "Enter Principal, Rate and Time : " );
scanf( "%f %f %f", &p, &r, &t );
si = ( p * r * t ) / 100;
si1 = Simp_Int();
system( "pause" );
return 0;
}
float Simp_Int()
{
float p, r, t, si;
printf( "Enter Principal, Rate and Time : " );
scanf( "%f %f %f", &p, &r, &t );
si = ( p * r * t ) / 100;
return si;
}
Output :
______________ **** ______________
Program No. - 309 :
Program to calculate Simple Interest using Function ( Argument but No
Return Value ).
Code :
#include <stdio.h>
void Simp_Int( float, float, float );
int main()
{
float p1, r1, t1;
system( "cls" );
system( "pause" );
return 0;
}
void Simp_Int( float p, float r, float t )
{
float si;
si = ( p * r * t ) / 100;
system( "pause" );
return 0;
}
float Simp_Int( float p, float r, float t )
{
float si;
si = ( p * r * t ) / 100;
return si;
}
Output :
______________ **** ______________
Program No. - 311 :
Program to use return statement in void function to terminate the function.
Code :
#include <stdio.h>
void show();
int main()
{
system( "cls" );
printf( "Red\n" );
show();
printf( "Blue\n" );
system( "pause" );
return 0;
}
void show()
{
printf( "Green\n" );
return;
printf( "Orange\n" );
}
Output :
______________ **** ______________
Program No. - 312 :
Program to explain Call By Value.
Code :
#include <stdio.h>
void cube( int );
int main()
{
int a = 10;
system( "cls" );
cube( a );
system( "pause" );
return 0;
}
void cube( int x )
{
x = x * x * x;
printf( "x = %d\n", x );
}
Output :
cube( &a );
system( "pause" );
return 0;
}
void cube( int *pa )
{
*pa = (*pa) * (*pa) * (*pa);
printf( "*pa = %d\n", *pa );
}
Output :
swap( a, b );
system( "pause" );
return 0;
}
void swap( int x, int y )
{
int t;
printf( "Before Swapping (in swap) : x = %d, y = %d\n", x, y );
t = x;
x = y;
y = t;
system( "pause" );
return 0;
}
void swap( int *pa, int *pb )
{
int t;
printf( "Before Swapping (in swap) : *pa = %d, *pb = %d\n", *pa, *pb
);
t = *pa;
*pa = *pb;
*pb = t;
printf( "After Swapping (in swap) : *pa = %d, *pb = %d\n", *pa, *pb
);
}
Output :
______________ **** ______________
Program No. - 316 :
Program to explain Returning By Reference / Address / Pointer.
Code :
#include <stdio.h>
int* max( int*, int* );
int main()
{
int a = 10, b = 20, *p;
system( "cls" );
system( "pause" );
return 0;
}
int* max( int *pa, int *pb )
{
if( *pa > *pb )
return pa;
else
return pb;
}
Output :
display( a );
printf( "\n" );
system( "pause" );
return 0;
}
/* void display( int *x ) */
void display( int x[ ] )
{
int i;
for( i=0 ; i<5 ; i++ )
{
printf( " %d", x[ i ] ); /* *( x + i ) */
}
}
Output :
display( a );
system( "pause" );
return 0;
}
void display( int x[ ][3] )
{
int i, j;
printf( "Matrix-A : \n" );
for( i=0 ; i<2 ; i++ )
{
for( j=0 ; j<3 ; j++ )
{
printf( " %d", x[ i ][ j ] );
}
printf( "\n" );
}
}
Output :
______________ **** ______________
Program No. - 319 :
Program for passing String to function.
Code :
#include <stdio.h>
/* void display( char* ); */
void display( char[ ] );
int main()
{
char s[ ] = "ATUL";
system( "cls" );
display( s );
system( "pause" );
return 0;
}
/* void display( char *s1 ) */
void display( char s1[ ] )
{
int i;
printf( "%s\n", s1 );
for( i=0 ; s1[ i ] != '\0' ; i++ )
{
printf( "%c\n", s1[ i ] ); /* *( s1 + i ) */
}
}
Output :
______________ **** ______________
Program No. - 320 :
Program for passing Array of Strings to function.
Code :
#include <stdio.h>
void display( char[ ][10] );
int main()
{
char s[ ][10] = { "Red", "Green", "Blue" };
system( "cls" );
display( s );
printf( "\n" );
system( "pause" );
return 0;
}
void display( char s1[ ][10] )
{
int i;
printf( "Colors are :" );
for( i=0 ; i<3 ; i++ )
{
printf( " %s", s1[ i ] );
}
}
Output :
printf( "\n" );
system( "pause" );
return 0;
}
void display( int n )
{
printf( " %d", n ) ;
}
Output :
printf( "\n" );
system( "pause" );
return 0;
}
void display( int *p )
{
printf( " %d", *p ) ;
}
Output :
f = fact( n );
system( "pause" );
return 0;
}
long fact( long a )
{
long f = 1;
int i;
for( i=1 ; i<= a ; i++ )
{
f = f * i;
}
return f;
}
Output :
______________ **** ______________
Program No. - 324 :
Program to calculate Factorial using Recursion.
Code :
#include <stdio.h>
long fact( long );
int main()
{
long n, f;
system( "cls" );
f = fact( n );
system( "pause" );
return 0;
}
long fact( long a )
{
if( a == 1 )
return 1;
else
return a * fact( a − 1 );
}
Output :
______________ **** ______________
Program No. - 325 :
Program to find the sum of first n Natural number by recursive function.
Code :
#include <stdio.h>
int sum( int );
int main()
{
int num, s;
system( "cls" );
s = sum( num );
system( "pause" );
return 0;
}
int sum( int n )
{
if( n == 1 )
return 1;
else
return n + sum( n − 1 );
}
Output :
______________ **** ______________
Program No. - 326 :
Program to find nth power of x using Recursion.
Code :
#include <stdio.h>
int power( int, int );
int main()
{
int x, n, p;
system( "cls" );
p = power( x, n );
system( "pause" );
return 0;
}
int power( int x, int n )
{
if( n == 0 )
return 1;
else
return x * power( x, n − 1 );
}
Output :
______________ **** ______________
Program No. - 327 :
Program to find nth term of Fibonacci Series using Recursion.
Code :
#include <stdio.h>
int fibo( int );
int main()
{
int n, t;
system( "cls" );
t = fibo( n );
system( "pause" );
return 0;
}
int fibo( int n )
{
if( n == 0 || n == 1 )
return n;
else
return fibo( n − 1 ) + fibo( n − 2 );
}
Output :
______________ **** ______________
Program No. - 328 :
Program to find nth term of Lucas Series using Recursion.
Code :
#include <stdio.h>
int lucas( int );
int main()
{
int n, t;
system( "cls" );
t = lucas( n );
system( "pause" );
return 0;
}
int lucas( int n )
{
if( n == 0 || n == 1 || n == 2 )
return 1;
else
return lucas(n − 1) + lucas(n − 2) + lucas(n − 3);
}
Output :
______________ **** ______________
Program No. - 329 :
Program to find Length of String using function ( Passing String ).
Code :
#include <stdio.h>
int length( char s[ ] )
{
int i, l = 0;
for( i=0 ; s[ i ] != '\0' ; i++ );
l = i;
return l;
}
int main()
{
int len = 0;
char str[50];
system( "cls" );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 330 :
Program to explain Constant Argument to function.
Code :
#include <stdio.h>
void show( const int );
int main()
{
int a = 10;
system( "cls" );
show( a );
printf( "a = %d\n", a );
system( "pause" );
return 0;
}
void show( const int b )
{
/* b = 20; */ /* can't be changed. */
printf( "b = %d\n", b );
}
Output :
ar = area( radius );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 332 :
Program to Return Multiple Values from Function using Reference.
Code :
#include <stdio.h>
void AreaCircum( float, float*, float* );
int main()
{
float radius, area, circum;
system( "cls" );
system( "pause" );
return 0;
}
void AreaCircum( float r, float *par, float *pcir )
{
*par = 3.14 * r * r;
*pcir = 2 * 3.14 * r;
}
Output :
______________ **** ______________
Program No. - 333 :
Program to Return Multiple Values from Function using call by reference.
Code :
#include <stdio.h>
int main()
{
void intfrac( float, float*, float* );
system( "pause" );
return 0;
}
void intfrac( float n, float *intp, float *fracp )
{
*intp = (float)( (long)n ) ;
*fracp = n - *intp;
}
Output :
______________ **** ______________
Program No. - 334 :
Program to explain Nesting of Functions.
Code :
#include <stdio.h>
void func2();
void func1()
{
printf( "function1-1 \n" );
func2();
printf( "function1-2 \n" );
}
void func2()
{
printf( "function2-1 \n" );
}
int main()
{
system( "cls" );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 335 :
Program to explain Nested function call.
Code :
#include <stdio.h>
int main()
{
int add( int, int );
system( "pause" );
return 0;
}
int add( int a, int b )
{
return ( a + b );
}
Output :
p = fact( n ) / fact( n − r );
system( "pause" );
return 0;
}
int fact( int a )
{
int i, f=1;
for( i=1 ; i<=a ; i++ )
{
f = f * i;
}
return f;
}
Output :
______________ **** ______________
Program No. - 337 :
Program to Find the Number of Combinations without repeatation.
(i.e., nCr = n! / ( r! * (n−r)!).
Code :
#include <stdio.h>
int fact( int );
int main()
{
int n, r, p;
system( "cls" );
system( "pause" );
return 0;
}
int fact( int a )
{
int i, f=1;
for( i=1 ; i<=a ; i++ )
{
f = f * i;
}
return f;
}
Output :
______________ **** ______________
Program No. - 338 :
Program to check whether a number can be expressed as sum of two
prime numbers or not.
Code :
#include <stdio.h>
int main()
{
int n, i, flag=0;
system( "cls" );
if( flag == 0 )
printf( "%d can not be expressed as sum of two Prime
Numbers.\n", n );
system( "pause" );
return 0;
}
int isprime( int n )
{
int i, flag=1;
for( i=2 ; i<=n/2 ; i++ )
{
if( n % i == 0 )
flag = 0;
}
return flag;
}
Output :
system( "pause" );
return 0;
}
int fact( int n )
{
int i, f;
f = 1;
for( i=1 ; i<=n ; i++ )
{
f *= i;
}
return f;
}
Output :
sort_string( s1 );
sort_string( s2 );
system( "pause" );
return 0;
}
void sort_string( char s1[ ] )
{
int i, j, len;
char ch;
len = strlen( s1 );
for( i=0 ; i<len−1 ; i++ )
{
for( j=0 ; j<len−1−i ; j++ )
{
if( s1[ j ] > s1[ j+1] )
{
ch = s1[ j ];
s1[ j ] = s1[ j+1];
s1[ j+1] = ch;
}
}
}
}
Output :
system( "pause" );
return 0;
}
int is_anagram( char s1[ ], char s2[ ] )
{
char chars1[256] = {0}, chars2[256] = {0};
int i;
for( i=0 ; s1[ i ] && s2[ i ] ; i++ )
{
chars1[ i ]++;
chars2[ i ]++;
}
if( s1[ i ] || s2[ i ] )
return 0;
for( i=0 ; i<256 ; i++ )
{
if( chars1[ i ] != chars2[ i ] )
return 0;
}
return 1;
}
Output :
if( index == −1 )
printf( "\nSorry!! Element not found.\n" );
else
{
printf( "\nElement found at index : %d", index );
printf( ", Position : %d\n", ( index + 1 ) );
}
system( "pause" );
return 0;
}
int Lsearch( int ar[ ], int size, int item )
{
int i;
bubble_sort( a, 5 );
printf( "\n" );
system( "pause" );
return 0;
}
void bubble_sort( int *p, int n )
{
int i, j, t;
for( i=0 ; i<n−1 ; i++ )
{
for( j=0 ; j<n−i−1 ; j++ )
{
if( *(p+j) > *(p+j+1) ) /* if( p[ j ] > p[ j+1] ) */
{
t = *(p+j); /* t = p[ j ]; */
*(p+j) = *(p+j+1); /* p[ j ] = p[ j+1]; */
*(p+j+1) = t; /* p[ j+1] = t; */
}
}
}
}
Output :
MN = M + N;
Merge( A, M, B, N, C );
system( "pause" );
return 0;
}
void localVar()
{
int x = 20;
printf( "x in localVar() : %d\n", x );
x++;
printf( "x in localVar() : %d\n", x );
}
Output :
______________ **** ______________
Program No. - 346 :
Program to explain Global Variable.
Code :
#include <stdio.h>
void globalVar();
int x = 10;
int main()
{
system( "cls" );
system( "pause" );
return 0;
}
void globalVar()
{
x++;
printf( "x in globalVar() : %d\n", x );
}
Output :
______________ **** ______________
Program No. - 347 :
Program to explain Static Variable.
Code :
#include <stdio.h>
void staticVar();
int main()
{
system( "cls" );
int x = 10;
system( "pause" );
return 0;
}
void staticVar()
{
static int x = 20;
printf( "x in staticVar() : %d\n", x );
x++;
}
Output :
______________ **** ______________
Program No. - 348 :
Program to explain Local variables.
Code :
#include <stdio.h>
int main()
{
system( "cls" );
int a = 10, i;
printf( "%d ", a );
{
int a = 200;
for( i=0 ; i<3 ; i++ )
{
printf( "%d ", a );
}
}
printf( "%d\n", a );
system( "pause" );
return 0;
}
Output :
while( val != 0 )
{
printf( "Enter a Value ( 0 to exit) : " );
scanf( "%f", &val );
if( val == 0 )
break;
av = avg( val );
system( "pause" );
return 0;
}
float avg( float value )
{
static float sum = 0;
static int count = 0;
count++;
sum += value;
return (sum / count);
}
Output :
// Default value : 0
printf( "a = %d\n", a );
system( "pause" );
return 0;
}
Output :
// Default value : 0
printf( "a = %d\n", a );
system( "pause" );
return 0;
}
Output :
d1.feet = 12;
d1.inches = 9.5;
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 354 :
Program to explain Global Structure with Global and Local structure
variable.
Code :
#include <stdio.h>
/* Global Structure */
struct Distance
{
int feet;
float inches;
};
struct Distance d1; /* Global variable */
int main()
{
struct Distance d2; /* Local variable */
system( "cls" );
d1.feet = 12;
d1.inches = 9.5;
d2.feet = 15;
d2.inches = 3.5;
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 355 :
Program to explain Local Structure with Local structure variable.
Code :
#include <stdio.h>
int main()
{
/* Local Structure */
struct Distance
{
int feet;
float inches;
}d1;
d1.feet = 12;
d1.inches = 9.5;
d2.feet = 15;
d2.inches = 3.5;
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 356 :
Program to use Multiple Structure Variables.
Code :
struct Distance
{
int feet;
float inches;
};
int main()
{
struct Distance d1, d2;
system( "cls" );
d1.feet = 12;
d1.inches = 9.5;
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 357 :
Program to Initialize Structure Variable and expain Structure Assignment.
Code :
#include <stdio.h>
struct Distance
{
int feet;
float inches;
};
int main()
{
struct Distance d1 = { 15, 6.5 };
struct Distance d2, d3;
system( "cls" );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 358 :
Program to explain the Anonymous Structure( without tagname ).
Code :
#include <stdio.h>
// Structure without Tag name
struct
{
int feet;
float inches;
}d1, d2 = { 15, 6.5 };
int main()
{
system( "cls" );
d1.feet = 12;
d1.inches = 9.5;
system( "pause" );
return 0;
}
Output :
d1.feet = 15;
d1.inches = 6.5;
system( "pause" );
return 0;
}
Output :
r1.l = 10;
r1.b = 5;
system( "pause" );
}
Output :
system( "pause" );
}
Output :
system( "pause" );
}
Output :
system( "pause" );
}
Output :
system( "pause" );
}
Output :
system( "pause" );
}
Output :
system( "pause" );
}
Output :
system( "pause" );
}
Output :
system( "pause" );
}
Output :
system( "pause" );
}
Output :
r2 = r1;
system( "pause" );
}
Output :
system( "pause" );
return 0;
}
Output :
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 373 :
Program to explain Array within Structure.
Code :
#include <stdio.h>
struct Student
{
int rno;
char name[25];
float marks[5];
};
int main()
{
int i;
struct Student s;
system( "cls" );
system( "pause" );
return 0;
}
Output :
printf( "\n" );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 375 :
Program to pass a structure variable to function as pass by value.
Code :
#include <stdio.h>
struct rectangle
{
int l, b;
};
void show( struct rectangle );
int main()
{
system( "cls" );
struct rectangle r1 = { 20, 10 };
show( r1 );
system( "pause" );
}
void show( struct rectangle r )
{
printf( "length = %d\n", r.l );
printf( "breadth = %d\n", r.b );
}
Output :
show( &r1 );
system( "pause" );
}
void show( struct rectangle *p )
{
printf( "length = %d\n", p−>l );
printf( "breadth = %d\n", p−>b );
}
Output :
______________ **** ______________
Program No. - 377 :
Program to pass a structure variable to function as pass by address/pointer.
Code :
#include <stdio.h>
struct rectangle
{
int l, b;
};
void show( struct rectangle* );
int main()
{
system( "cls" );
struct rectangle r1 = { 20, 10 };
show( &r1 );
system( "pause" );
}
void show( struct rectangle *p )
{
printf( "length = %d\n", (*p).l );
printf( "breadth = %d\n", (*p).b );
}
Output :
init( &r1 );
system( "pause" );
}
void init( struct rectangle *p )
{
p−>l = 10;
p−>b = 5;
}
Output :
______________ **** ______________
Program No. - 379 :
Program to input structure variable using function.
Code :
#include <stdio.h>
struct rectangle
{
int l, b;
};
void init( struct rectangle* );
int main()
{
system( "cls" );
struct rectangle r1;
init( &r1 );
display( d1 );
display( d2 );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 381 :
Program to Pass Structure Variable to Function.
Code :
#include <stdio.h>
struct Distance
{
int feet;
float inches;
};
void display( struct Distance d )
{
printf( "Distance = %d\'-%f\" \n", d.feet, d.inches );
}
void add_dist( struct Distance, struct Distance );
int main()
{
struct Distance d1;
struct Distance d2;
system( "cls" );
display( d1 );
display( d2 );
add_dist( d1, d2 );
system( "pause" );
return 0;
}
void add_dist( struct Distance dd1, struct Distance dd2 )
{
struct Distance dd3;
dd3.feet = dd1.feet + dd2.feet;
dd3.inches = dd1.inches + dd2.inches;
if( dd3.inches >= 12.0 )
{
dd3.inches −= 12.0;
dd3.feet++;
}
display( dd3 );
}
Output :
display( d1 );
display( d2 );
d3 = add_dist( d1, d2 );
display( d3 );
system( "pause" );
return 0;
}
struct Distanceadd_dist( struct Distance dd1, struct Distance dd2 )
{
struct Distance dd3;
dd3.feet = dd1.feet + dd2.feet;
dd3.inches = dd1.inches + dd2.inches;
if( dd3.inches >= 12.0 )
{
dd3.inches −= 12.0;
dd3.feet++;
}
return dd3;
}
Output :
a = l * w;
int main()
{
struct Room dr;
float l1, b1, a;
system( "cls" );
dr.l.feet = 15;
dr.l.inches = 6.0;
dr.b.feet = 12;
dr.b.inches = 9.0;
a = l1 * b1;
system( "pause" );
return 0;
}
Output :
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 386 :
Program to explain String within Structure.
Code :
#include <stdio.h>
struct Book
{
char name[25];
int page;
int price;
};
int main()
{
int i;
struct Book b1[3];
system( "cls" );
for( i=0 ; i<3 ; i++ )
{
printf( "Enter Name of Book : " );
fflush( stdin );
gets( b1[ i ].name );
printf( "Enter Number of Pages : " );
scanf( "%d", &b1[ i ].page );
printf( "Enter Price of Book : " );
scanf( "%d", &b1[ i ].price );
}
system( "pause" );
return 0;
}
Output :
system( "pause" );
return 0;
}
void scale( struct Distance *p, float factor )
{
float inches = ( p−>feet * 12 + p−>inches ) * factor;
p−>feet = inches / 12;
p−>inches = inches − ( p−>feet * 12 );
}
void display( struct Distance dd )
{
printf( "%d\'-%.2f\"\n", dd.feet, dd.inches );
}
Output :
t1.a = 5;
t1.b = 10;
t1.c = 200;
system( "pause" );
}
Output :
______________ **** ______________
Program No. - 389 :
Program to explain Enum.
Code :
#include <stdio.h>
enum days_of_week { SUN, MON, TUE, WED, THU, FRI, SAT };
int main()
{
enum days_of_week day1, day2;
int diff;
system( "cls" );
day1 = MON;
day2 = THU;
system( "pause" );
return 0;
}
Output :
c1 = Red;
c2 = Green;
d = c2 − c1;
printf( "Difference = %d\n", d );
if( c1 < c2 )
printf( "RED comes before GREEN.\n" );
else
printf( "GREEN comes before RED.\n" );
system( "pause" );
return 0;
}
Output :
c1 = Red;
c2 = Blue;
d = c2 − c1;
printf( "Difference = %d\n", d );
if( c1 < c2 )
printf( "RED comes before GREEN.\n" );
else
printf( "GREEN comes before RED.\n" );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 392 :
Program to show an Enumeration type ( enum ).
Code :
#include <stdio.h>
enum Colors { Red=10, Green=20, Blue=30 };
int main()
{
enum Colors c1, c2;
int d;
system( "cls" );
c1 = Red;
c2 = Blue;
d = c2 − c1;
printf( "Difference = %d\n", d );
if( c1 < c2 )
printf( "RED comes before GREEN.\n" );
else
printf( "GREEN comes before RED.\n" );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 393 :
Program to explain Union.
Code :
#include <stdio.h>
union Abc
{
char c;
int i;
float f;
};
int main()
{
union Abc x;
system( "cls" );
x.c = 'A';
printf( "x.c = %c\n", x.c );
x.i = 100;
printf( "x.i = %d\n", x.i );
x.f = 123.45;
printf( "x.f = %f\n", x.f );
system( "pause" );
}
Output :
______________ **** ______________
Program No. - 394 :
Program to differentiate Structure and Union.
Code :
#include <stdio.h>
union ABC
{
char a;
int b;
float c;
};
struct XYZ
{
char x;
int y;
float z;
};
int main()
{
union ABC p;
struct XYZ q;
system( "cls" );
ptr = &dt;
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 396 :
Program to explain Nested Structure by employee structure.
Code :
#include <stdio.h>
struct address
{
char phone[15] ;
char city[25] ;
long pin ;
};
struct emp
{
char name[25] ;
struct addressadr ;
};
int main()
{
struct emp e = { "Harsh", "099999888888", "Bhilai", 490006 };
system( "cls" ) ;
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 397 :
Program to explain the concept of Passing Structure by Reference to
function by Book Structure example.
Code :
#include <stdio.h>
struct book
{
char name[25] ;
char author[25] ;
float price;
};
void display( struct book *b );
int main()
{
struct book b1 = { "C Programming", "Atul", 500.00 } ;
system( "cls" );
display ( &b1 ) ;
system( "pause" );
return 0;
}
void display( struct book *b )
{
printf( "Book Name: %s\n", b−>name );
printf( "Author : %s\n", b−>author );
printf( "Price : %.2f\n", b−>price ) ;
}
Output :
______________ **** ______________
Program No. - 398 :
Program to explain the use of user-defined Type Declaration ( typedef ).
Code :
#include <stdio.h>
struct book
{
char name[25];
char author[25];
float price;
};
typedef struct book BOOK;
void display( BOOK *b );
int main()
{
BOOK b1 = { "C Programming", "Atul", 500.00 } ;
system( "cls" );
display( &b1 ) ;
system( "pause" );
return 0;
}
void display( BOOK *b )
{
printf( "Book Name: %s\n", b−>name );
printf( "Author : %s\n", b−>author );
printf( "Price : %.2f\n", b−>price ) ;
}
Output :
______________ **** ______________
CHAPTER − 12
Pointers
______________ **** ______________
Program No. - 399 :
Program to explain simple use of Pointer.
Code :
#include <stdio.h>
int main()
{
int a = 10;
int * p;
system( "cls" );
p = &a;
system( "pause" );
return 0;
}
Output :
p = &a;
printf( "*p = %d\n", *p );
p = &b;
printf( "*p = %d\n", *p );
p = &c;
printf( "*p = %d\n", *p );
system( "pause" );
return 0;
}
Output :
p1 = &a;
p2 = &a;
p3 = &a;
*p2 = 50;
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 402 :
Program to explain new format spacifier %p for address of variable, i.e.
pointer.
Code :
#include <stdio.h>
int main()
{
int a = 10;
int *ptr = &a;
system( "cls" );
system( "pause" );
return 0;
}
Output :
pr = &r;
ph = &h;
pv = &v;
system( "pause" );
return 0;
}
Output :
p[0] = &a;
p[1] = &b;
p[2] = &c;
t = p[0];
p[0] = p[2];
p[2] = t;
printf( "\n" );
system( "pause" );
return 0;
}
Output :
p = &a[0]; /* p = a; */
printf( "\n" );
system( "pause" );
return 0;
}
Output :
display( a );
printf( "\n" );
system( "pause" );
return 0;
}
void display( int *p )
{
int i;
printf( "Array Elements are :" );
for( i=0 ; i<5 ; i++ )
{
printf( " %d", *( p + i ) );
}
}
Output :
display( s );
system( "pause" );
return 0;
}
void display( char *ps )
{
printf( "String : %s\n", ps );
}
Output :
square( a );
system( "pause" );
return 0;
}
void square( int x )
{
x = x * x;
printf( "x = %d\n", x );
}
Output :
square( &a );
system( "pause" );
return 0;
}
void square( int *pa )
{
*pa = *pa * *pa;
printf( "*pa = %d\n", *pa );
}
Output :
system( "pause" );
return 0;
}
int* max( int *pa, int *pb )
{
if( *pa > *pb )
return pa;
else
return pb;
}
Output :
display( a );
printf( "\n" );
system( "pause" );
return 0;
}
void display( char *p )
{
int i;
printf( "String : " );
for( i=0 ; *(p+i) != '\0' ; i++ )
{
printf( " %c", *( p + i ) );
}
}
Output :
p = &a[0][0];
system( "pause" );
return 0;
}
Output :
pf = Sub;
(*pf)( a, b ); /* Sub( a, b ); */
system( "pause" );
return 0;
}
void Add( int x, int y )
{
int s;
s = x + y;
printf( "Sum = %d\n", s );
}
void Sub( int x, int y )
{
int d;
d = x − y;
printf( "Difference = %d\n", d );
}
Output :
______________ **** ______________
Program No. - 414 :
Program to explain Chain of Pointers.
Code :
#include <stdio.h>
int main()
{
int a = 10;
int *p, **pp, ***ppp, ****pppp;
system( "cls" );
p = &a;
pp = &p;
ppp = &pp;
pppp = &ppp;
p = &r1;
p−>l = 10;
p−>b = 5;
system( "pause" );
}
Output :
p = a; /* p = &p[0]; */
printf( "Before Sorting :" );
for( i=0 ; i<5 ; i++ )
{
printf( " %d", *( p + i ) );
}
printf( "\n" );
system( "pause" );
return 0;
}
Output :
system( "pause" );
return 0;
}
Output :
system( "pause" );
return 0;
}
Output :
system( "pause" );
return 0;
}
Output :
printf( "\n" );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 421 :
Program to use malloc( ) function.
Code :
#include <stdio.h>
#include <malloc.h>
int main()
{
int *p;
int i, n, s;
system( "cls" );
if( !p )
printf( "\nUnable to allocate size. \n" );
else
{
for( i=0 ; i<n ; i++ )
{
printf( "Enter Value : " );
scanf( "%d", ( p + i ) );
}
free( p );
system( "pause" );
return 0;
}
Output :
if( ! p )
printf( "\nUnable to allocate size.\n" );
else
{
for( i=0 ; i<n ; i++ )
{
printf( "Enter Value : " );
scanf( "%d", ( p + i ) );
}
free( p );
system( "pause" );
return 0;
}
Output :
if( ! p )
printf( "\nUnable to allocate size.\n" );
else
{
for( i=0 ; i<n ; i++ )
{
printf( "Enter Value : " );
scanf( "%d", ( p + i ) );
}
x = n;
printf( "\n\nEnter New Size of Dynamic Array : " );
scanf( "%d", &n );
if( !p )
{
printf( "\nUnable to allocate size.\n" );
exit( 1 );
}
free( p );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
CHAPTER − 14
Preprocessors
______________ **** ______________
Program No. - 424 :
Program to explain Simple Macro Substitution( #define ).
Code :
#include <stdio.h>
#define PI 3.14159
int main()
{
system( "cls" );
printf( "Value of PI : %f\n", PI );
system( "pause" );
return 0;
}
Output :
a = PI * r * r;
system( "pause" );
return 0;
}
Output :
x = 10;
y = 20;
printf( "Minimum = %d\n", MIN(x, y) );
system( "pause" );
return 0;
}
Output :
v = Volume( r );
printf( "\nVolume of Sphere : %f\n", v );
system( "pause" );
return 0;
}
Output :
Code :
#include <stdio.h>
#define SWAP(a,b) {int temp; temp=a; a=b; b=temp;}
int main()
{
int x, y;
system( "cls" );
SWAP( x, y );
system( "pause" );
return 0;
}
Output :
res = add( a, b );
printf( "Sum = %d\n", res );
res = sub( a, b );
printf( "Difference = %d\n", res );
system( "pause" );
return 0;
}
Output :
s = square( n );
printf( "Square : %d\n", s );
system( "pause" );
return 0;
}
Output :
s = square( n );
printf( "Square : %d\n", s );
system( "pause" );
return 0;
}
Output :
s = square( n );
printf( "Square : %d\n", s );
system( "pause" );
return 0;
}
Output :
s = square( n );
system( "pause" );
return 0;
}
Output :
a = PI * r * r;
system( "pause" );
return 0;
}
Output ( If INDIA defined ):
Output ( If USA defined ):
a = PI * r * r;
system( "pause" );
return 0;
}
Output ( If INDIA defined ):
Output ( If USA defined ):
a = PI * r * r;
system( "pause" );
return 0;
}
Output ( If USA not defined ):
Output ( If INDIA not defined ):
s = a + b;
system( "pause" );
return 0;
}
Output :
system( "pause" );
return 0;
}
Output :
#if MAX == 1
printf( "Maximum = %d\n", (a>b?a:b) );
#else
printf( "Minimum = %d\n", (a<b?a:b) );
#endif
system( "pause" );
return 0;
}
Output :
#line 201
printf( "Line No.: %d\n", __LINE__ );
printf( "Current Date: %s\n", __DATE__ );
printf( "Current Time: %s\n", __TIME__ );
system( "pause" );
return 0;
}
Output :
message( C, C++ );
system( "pause" );
return 0;
}
Output :
system( "pause" );
return 0;
}
Output :
system( "pause" );
return 0;
}
Output :
system( "pause" );
return 0;
}
Output :
printf( "\n" );
system( "pause" );
return 0;
}
Output :
num = 100;
printf( "\nnum = %d\n", num );
system( "pause" );
return 0;
}
Output :
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 448 :
Program to use integer literal inside printf() statement.
Code :
#include <stdio.h>
int main()
{
system( "cls" );
system( "pause" );
return 0;
}
Output :
system( "pause" );
return 0;
}
Output :
c = 'A';
i = 10;
l = 100L;
ll = 1000LL;
f = 12.34f;
d = 12.34;
ld = 12.34;
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 451 :
Program to use decimal, octal and hexadecimal integer values in C
program.
Code :
#include <stdio.h>
int main()
{
int dec = 12;
int oct = 015;
int hex = 0x2F;
system( "cls" );
system( "pause" );
return 0;
}
Output :
printf( "\n" );
system( "pause" );
return 0;
}
Output :
printf( "Red\nGreen\nBlue\n" );
printf( "Red\tGreen\tBlue\n" );
system( "pause" );
return 0;
}
Output :
system( "pause" );
return 0;
}
Output :
puts( "Atul" );
puts( "Kumar" );
puts( "Soni" );
system( "pause" );
return 0;
}
Output :
puts( str );
system( "pause" );
return 0;
}
Output :
ch = getchar();
putchar( ch );
system( "pause" );
return 0;
}
Output :
system( "pause" );
return 0;
}
Output :
system( "pause" );
return 0;
}
Output :
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 461 :
Program to input two three digits integer values using scanf().
Code :
#include <stdio.h>
int main()
{
int a, b;
system( "cls" );
system( "pause" );
return 0;
}
Output :
system( "pause" );
return 0;
}
Output :
p = gets( str );
system( "pause" );
return 0;
}
Output :
system( "pause" );
return 0;
}
Output :
system( "pause" );
return 0;
}
Output :
system( "pause" );
return 0;
}
Output :
system( "pause" );
return 0;
}
Output :
system( "pause" );
return 0;
}
Output :
printf( "|%7d|\n", a );
printf( "|%−7d|\n", a );
printf( "|%07d|\n", a );
printf( "|%2d|\n", a );
printf( "\n|%10ld|\n", b );
printf( "|%−10ld|\n", b );
printf( "|%010ld|\n", b );
printf( "|%3ld|\n", b );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 470 :
Program to use printf() for integer formatting.
Code :
#include <stdio.h>
int main()
{
system( "cls" );
printf( "%4d\n", 1 );
printf( "%4d\n", 12 );
printf( "%4d\n", 123 );
printf( "%4d\n", 1234 );
printf( "%4d\n\n", 12345 );
printf( "%4d\n", -1 );
printf( "%4d\n", -12 );
printf( "%4d\n", -123 );
printf( "%4d\n", -1234 );
printf( "%4d\n", -12345 );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 471 :
Program to use different format specifiers for floating-point values.
Code :
#include <stdio.h>
int main()
{
float x = 123.45f;
system( "cls" );
system( "pause" );
return 0;
}
Output :
printf( "|%12.4f|\n", a );
printf( "|%12.2f|\n", a );
printf( "|%−12.4f|\n", a );
printf( "|%−12.2f|\n", a );
printf( "|%f|\n", a );
printf( "\n|%12.4e|\n", a );
printf( "|%12.2e|\n", a );
printf( "|%−12.4e|\n", a );
printf( "|%−12.2e|\n", a );
printf( "|%e|\n", a );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 473 :
Program to use printf() for floating-point formatting.
Code :
#include <stdio.h>
int main()
{
float a = 987.654;
system( "cls" );
system( "pause" );
return 0;
}
Output :
system( "pause" );
return 0;
}
Output :
system( "pause" );
return 0;
}
Output :
fclose( fp );
system( "pause" );
return 0;
}
Output :
fclose( fp );
system( "pause" );
return 0;
}
Output :
while( 1 )
{
printf( "Enter Number ( −1 to end ) : " );
scanf( "%d", &n );
if( n == −1 )
break;
putw( n, fp );
}
fclose( fp );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 479 :
Program to read integer values from File using getw() function.
Code :
#include <stdio.h>
int main()
{
FILE *fp;
int n;
system( "cls" );
fclose( fp );
printf( "\n" );
system( "pause" );
return 0;
}
Output :
fputs( s, fp );
fputs( "\n", fp );
fclose( fp );
system( "pause" );
return 0;
}
Output :
fclose( fp );
system( "pause" );
return 0;
}
Output :
fclose( fp );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 483 :
Program to read multiple lines of text from file using fgets() function.
Code :
#include <stdio.h>
int main()
{
FILE *fp;
char s[50];
system( "cls" );
fclose( fp );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 484 :
Program to write different types of values to File using fprintf() function.
Code :
#include <stdio.h>
int main()
{
FILE *fp;
int rno = 101;
char name[50] = "Animesh";
float marks = 92.5;
system( "cls" );
fclose( fp );
system( "pause" );
return 0;
}
Output :
fclose( fp );
system( "pause" );
return 0;
}
Output :
system( "pause" );
return 0;
}
Output :
fclose( fp ) ;
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 488 :
Program to read structure variable from File using fread() function.
Code :
#include <stdio.h>
struct student
{
int rno;
char name[50];
float marks;
};
int main()
{
FILE *fp;
struct student s;
system( "cls" );
fclose( fp ) ;
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 489 :
Program to write structure variable to File using fprintf() function.
Code :
#include <stdio.h>
struct student
{
int rno;
char name[50];
float marks;
};
int main()
{
FILE *fp;
struct student s;
system( "cls" );
fclose( fp ) ;
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 490 :
Program to read structure variable from File using fscanf() function.
Code :
#include <stdio.h>
struct student
{
int rno;
char name[50];
float marks;
};
int main()
{
FILE *fp;
struct student s;
system( "cls" );
fclose( fp ) ;
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 491 :
Program to write structure variables to File using fprintf() function.
Code :
#include <stdio.h>
struct student
{
int rno;
char name[50];
float marks;
};
int main()
{
FILE *fp;
char another = 'Y';
struct student s;
system( "cls" );
fflush( stdin );
another = getche( );
printf( "\n" );
}
fclose( fp );
system( "pause" );
return 0;
}
Output :
fclose( fp );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 493 :
Program to write structure variables to File using fwrite() function.
Code :
#include <stdio.h>
struct student
{
int rno;
char name[50];
float marks;
};
int main()
{
FILE *fp;
char another = 'Y';
struct student s;
system( "cls" );
fflush( stdin );
another = getche( );
printf( "\n" );
}
fclose( fp );
system( "pause" );
return 0;
}
Output :
fclose( fp );
system( "pause" );
return 0;
}
Output :
______________ **** ______________
Program No. - 495 :
Program to use Error Handling function fopen().
Code :
#include <stdio.h>
int main()
{
FILE *fp;
int n;
system( "cls" );
if( fp == NULL )
{
printf( "File can not open.\n" );
}
else
{
printf( "File is opened.\n" );
}
fclose( fp );
system( "pause" );
return 0;
}
Output :
system( "pause" );
return 0;
}
Output :
n = 0L;
while( feof( fp ) == 0 )
{
fseek( fp, n, 0 );
printf( "\n%3c - %2d", fgetc( fp ), ftell( fp ) );
n = n + 1L;
}
rewind( fp );
printf( "\nString : " );
ch = fgetc( fp );
while( ch != EOF )
{
printf( "%c", ch );
ch = fgetc( fp );
}
printf( "\n" );
fclose( fp );
system( "pause" );
return 0;
}
Output :
system( "pause" );
return 0;
}
Output :
if( argc != 3 )
{
puts( "Improper Number of Arguments." );
exit( 1 );
}
while( 1 )
{
ch = fgetc( fs );
if( ch == EOF )
break;
else
fputc( ch, ft ) ;
}
fclose( fs ) ;
fclose( ft ) ;
system( “pause” );
return 0;
}
Output :