You are on page 1of 56

PROBLEM SOLVING NOTES:

1.Generation of Programming language


2.Reema Thereja "Programming in c" (Text Book)
3.20CA2002(subject Lab code), 20ca2003(lab code)
1.First Generation: ML(1950)
1.difficult for programming
2.
2.Second generation: AL
1.symbolic programming language
2.esier when compared to ML
3.Third Generation: Programming language
1.efficient and easy for programming
2.high level language
3.Interpretor and Compiler are the translators to Machine language
4.programs are portable
4.Fourth Generation:Very High Level Language
1.they are non procedural
2.very easy and useful
5.Fifth Generation:
1.uses visual tools to develop a program
2.Sgl s are widely used in Artificial intelligence
3.SGL include Prolog,OPS s, Mercury
6.Phase:-
1.Requirement Analysis
1.Why the program/software has to be built
2.Documenting the identified requirement
3.the functionality, capability, performance, availability of harware
and software
2.Design
1.Requirement documentation in the previous phase act as an iinput to
the design phase
2.design phase is broken down into modules
3.Each modules is specified in the form of algorithm, flowcharts or
pseudo code
4.design phase specifies how the programs/software will be built
3.Implementation
1.Designed algorithm are converted into program code using high
level language
2.select the programming language
3.Software is compatible with the hardware
4.Testing
1.all the modules are tested together to ensure that all the oveerall
system works well as a whole product
2.when integration of modules are done there may be nug this is
tested
3.tested with the large number of varied inputs known as test data
5.Software Deployment, training and support
6.Maintenance
7.ALGORITHYM:-
1.Start
2.Get the value
3.Add the Value
4.Print
8.FLow chart:-
1.Diagram with symbols(pictorial representation)
9.pseudo code:-
1.logic of a program
10.Google CLassroom CODE ---flbgqpm
1.Algorithym, Flow chart, Pseudo code
2.Write an algotithm to find out area of a circle?
1.Step 1:start
2.Step 2:Get value for r (input operation)
3.Step 3:Calculate Area=Pi*r*r (Processing)
4.Step 4:Print Area (Output operation/Execution)
5.Step 5:End
3.Talked about Raptor tool(Download it)
4.Write an algorithm to print "Welcome" Message.
5.,, ,, ,, name and regno
6.,, ,, ,, Address
7.,, ,, ,, Multilply 2 numbers
8.,, ,, ,, Perform Basic arithmetic operation
9.,, ,, Calculate the distance travelled by a vechile by providing speed
and time
1.Sequential Statement
2.Pseudo code:-
1.used in books
2.looks like programming
3.
3.Write a code forcalculating the price of a product afteradding sales taxto its
original price
1.Read the price of the product
2.Read the sales tax rate
3.
4.Types of errors:-
1.Run-Time-
1.occurs when program is executed
2.error occurs when the program performs some illegal operations
like
1.divide number by 0
2.opening file that already exist
3.lack of free memory space
4.Finding square or log of -ve nos.

2.Compile-time Errors:-
1.Syntax errors:-
1.Rules of c programming lang are violated
2. int a: //syntax error
2.semantic errors:-
1.Comply with the rules of the programming but not
meaningful to the compiler
2.a*b=c//Does not seem correct
3.logical errors:-
1.errors are unexpected and undesirable output
2.not detected/occurs due to incorrect statements
3.c=a+b//to be
4.c=a*b//mistake
4.linker errors:-
1.Occur when the linker is not able to find the function
definition
2.printf
3.not included #include<stdio.h>
3.Linker
4.Logical
1.Compiler converts High level language into Machine level language
2.exceptional handling
3.Syntax:(Comes under Syntax Error)
Datatype variablename;
eg:
int a;
4.Semantic Error:-
no meaning to the compiler
eg:
a*b=c/no meaning
C=a*b/more meaning (a is operand, * is operator)
5.Logical error:-
can'tbe detected by compiler
eg:
C=a+b/to be done
C=a*b/by mistake
6.Linker Error:-
occur when the linker isnot able to find the function definition
eg:
Printf
not included #include<stdio.h>
7.Testing and debugging approaches:-
1.Unit tests- Checks the single module
2.Integration test- Two units are combined and checked
3.System Test- To test the entire system
8.Debugging approaches:-
1.brute force
2.back tracking
3.cause elimination

1.C programming:-
1.C was devolpedin 1970 by Dennis ritchie
2.Taxonomy of c
1.AlGOL-Algorithmic language
2.BCPL-Basic combined programming language
3.B-Ken Thompson
4.C-Traditional C
5.K&RC
6.ANSI C
7.C89
8.C95
9.C99
3.Cis high level language
4.has 32 keywords
5.it is structured programming language

2.Source file(extension .c), header file(extension.h),object file(extension.opj),


executable file(extension.exe)
3.Header file(string.h, stdlib.h,stdio.h,math.h,alloc.h,conio.h)

1.Documentation section
2.Comments-1.Single line comment(//), 2.Multiline comment(/*....)
3.Link section(preprocessor directive)
ex., #include <stdio.h>
4.Definition section
ex.,# define PI 3.14
5.Global declaration section
6.Main function
{
Declaration part
Executable part
}
7. cc greet.c
8. ./a.out
9. nano display.c
1.rep.lit (online coding resource)
2.Character set in C
3.Keywords in C language
4.identifiers and data types
5.Basic Data Types in C:-
1.int
2.float
3.double
4.char
6.declaring a variable:-
syntax-:- datatype variablename,
Eg. int a;, float g;
7.write a program to assign a interger value!
8.syntax- printf("control string", variablename);
1.Basic data types in c:-
1.int
2.float
3.double
4.char stores characters
2.Attributes of data type:-
1.keyword
2.size
3.range
4.contol format
3.how float or double values are stored in memory?
MSD-----------------------------LSB
1.floating point must be converted binary
2.make the converted binary to normalize form(format= 1.significant
bit*2exponent)
3.Add bias to exponent
1.scanf function
2.did programming on code.karunya.edu

1.input output statements in c:-


1.the source of data as well as the destination of data (stream)
2.Text Stream, Binary stream
eg:- Keyboard-->
3.printf("control string",variable list); ---> syntax
4.tommarow test
1.sample program:-
1.
// welcome message
#include<stdio.h>
void main()
{
printf("Welcome to C programming");
}

2.

// arithmetic operations
#include<stdio.h>
void main()
{
int a,b,a1,b1,a2,a3,b2,b3;
printf("enter the value of a and b");
scanf("%d%d,&a,&b);
printf("Add=%d",a+b);
printf("enter the value of a1 and b1");
scanf("%d%d,&a1,&b1);
printf("Sub=%d,a1-b1);0
printf("enter the value of a2 and b2");
scanf("%d%d,&a2,&b2);
printf("Multi=%d,a2*b2);
printf("enter the value of a3 and b3");
scanf("%d%d,&a3,&b3);
printf("Div=%d,a3/b3);
}

1.scanf()-
1.syntax:-
scanf("conrtl str1,,,")
2.the prototype of the control string can be
%[*][width][modifiers]type
3.rules to use a scanf function
2.Some programms
Write a program to print circumference of a circle (2*3.14*radius)
#include<stdio.h>

int main() {

int rad;
float PI = 3.14, area, ci;

printf("\nEnter radius of circle: ");


scanf("%d", &rad);

area = PI * rad * rad;


printf("\nArea of circle : %f ", area);

ci = 2 * PI * rad;
printf("\nCircumference : %f ", ci);

return (0);
}
Write a program to convert fahrenheit to degree celsius
#include<stdio.h>

void main()
{
float celsius,fahrenheit;

// Reads temperature in fahrenheit


printf("\nEnter temperature in Fahrenheit:");
scanf("%f",&fahrenheit);

// Fahrenheit to celsius conversion formula


celsius=(fahrenheit - 32)*5/9;
// Print the result
printf("\nCelsius = %.3f",celsius); //.3f means correct to 3 decimal places

Write a program to find a distance between two points


#include<stdio.h>
#include<math.h>

int main()
{
float x1, y1, x2, y2, distance;

printf("Enter point 1 (x1, y1)\n");


scanf("%f%f", &x1, &y1);

printf("Enter point 2 (x2, y2)\n");


scanf("%f%f", &x2, &y2);

distance = sqrt( (x2 - x1)*(x2 - x1) + (y2 - y1)*(y2 - y1) );

printf("Distance between (%0.2f, %0.2f) and (%0.2f, %0.2f) is %0.2f\n", x1,


y1, x2, y2, distance);
return 0;
}
write a program to print the ascii value of a character
#include <stdio.h>
void main()
{
char c='A';
printf("%d",c);
printf("\n%c",c);
}

1.Operators in C:-
1. A + B ==> where + is operator, A,B are operands
2. Arithmetic operator
+,-,*,/,% (modulus) finds remainder
3. Relational operator
<(less than),>(greater than),<=(less than equal to),>=(greater than
equal to)

4. Equality operator
==, !=, returns 1 if true, o if false
5. Logical operator
AND(&&), OR(||), NOT(!) returns results as true or false

1.Unary operator:-
they act as single operands
Unary Minus(-)
a=-(b)
increment operator (++)
Prefix operand ++a
postfix....... a++
Decrement operator (--)

2.Conditional operator orternanry operator


(exp1)? exp2 : exp3
if true and goes to 2 and 3
int a=25, b=10
(a>b)? printf("A is greater than B"): printf("B is greater than A")
3.Bitwise operator:-
&(binary AND),|(Binary OR),^(Binary XOR),`(Binary one
compliment),<<(Binary Left shift),>>(Binary right shift)

1.Assignment operator: -
1. =,+=,-=,*=,/=,%=,**=
2.Comma operator
3.Precedence
4. 3*4%5/2
12%5/2
2/2
1
Write a program to calculate the bill amount for an item given it's quantity sold,
value,discount,tax!
#include<stdio.h>
int main()
{
int q,v,d,t,Amt;
printf("Enter the quantity%d",q);
scanf("\n%d",&q);
printf("\nEnter the Value%d",v);
scanf("\n%d",&v);
printf("\nEnter the Discount ",d);
scanf("\n%d",&d);
printf("\nEnter the Tax ",t);
scanf("\n%d",&t);
Amt=q*v-d+t;
printf("\nThe net amount of the products are %d",Amt);
}

1.Type conversion and type casting: -


1.Type conversion is done implicitly
2.casting done by the programmer
3.
2.Res = int(9.5)
3.REs=(int)12.3/(int)4.2
4.res=(int)(a+b)
a=500, b=70
5.Write a program to convert an integer into the coressponding floating number

1.Introduction to scopes of variables


2.Block Scope
{} local variable
3.Function scope
goto label has function scope
eg:

int main()
{
Loop:
..
..
goto Loop;
return0
}
4.Program scope
can access a variable through out the program
5.File Scope
static
6. Storage class
Where the data is stored
how long the storage allocation continue
where the variable can be accessed
variable is automatically initialized

1. Write a program to check the given number is odd or even

1.Types of Storage class <storage_class_specifier><data type><variable name>


1.Automatic
Default storage class for variable declaration inside a block
Eg:
auto int x;
2.Register
Stored in CPU, Maximum size of the variable will be equal to the
register size
Eg;
register int x;
3.External
Global variable,it's not initialized, will be initialized to zero
Eg:
extern int x;
4.Static
Static variables have life time over the entire program
Eg:
statics int x=10;
1.Decision control statements:-
if statement: -
if(test expression)
{
sate 1:
...
sate n:
}
if else statement: -
if(test expression)
{
sate 1:
}
else
{
sate 2:
}
statement x:
if else if statement
switch statement
2. did some programming

Today we did programming but suddenly something happened to ma’am she left!

Iterative Statement:
A statement to be printed more than 1 time.
For loop
While loop
Do-while loop
Four essentials elements in loop
Loop control variable
Initialization: Initial value
Condition
Step value: Increment value
While LOOP:
Syntax:
While(condition)
{
Statement;
}
Statement x:
Ex:
#include<stdio.h>
void main ()
{int i=1;
while(i<=5)
{
printf(“\nHello”);
i++;
}
}
Do-While LOOP:
Syntax:
Statement x;
Do
{
Statement block;
} while (condition);
Statement y;
Ex:
#include<stdio.h>
Void main ()
{int i=1;
Do
{
Printf(“\nHello”);
i++;
} while(i<=5);
}
// Program to print numbers from 1 to 10 using WHILE LOOP and DO-WHILE
LOOP

WHILE LOOP DO-WHILE LOOP

#include<stdio.h> #include<stdio.h>
int main() int main()
{ {
Int i=0; Int i=0;
While(i<=10) Do
{ {
printf (“\n %d”,i); printf(“\n %d”,i);
i=i+1; i=i+1;
} } while(i<=10);
Return 0; Return 0;
} }

For loop:
Syntax
For (initialization; condition; increment/decrement/update)
{
Statement block;
}
Statement y;
Example:
#include<stdio.h>
Void main()
{
int i, n=5;
for(i=1; i<=n; i++)
{
printf(“\nhello”);
}

LAB Session:

#include <stdio.h>
#include <math.h>
int main()
{
int a,b,c,d;
float r1,r2;
printf("Enter values for a,b,c: ");
scanf("%d%d%d",&a,&b,&c);
d=(b*b) - (4*a*c);
if (d==0)
{
r1=-b/(2*a);
r2=r1;
printf("The roots are real and equal");
printf("\nThe root is %f",r2);
}
else if(d > 0)
{
r1= (-b+sqrt(d))/(2*a);
r2=(-b-sqrt(d))/(2*a);
printf("The roots are real and unequal");
printf("\nRoot 1 is%f",r1);
printf("\nRott 2 is %f",r2);
}
else
{
r1=-b/(2*a);
r2=sqrt(-d)/(2*a);
printf("The roots are imaginary");
}
}

#include<stdio.h>
#include<math.h>
void main()
{
float weight,height,metric_height,bmi;
printf("Enter your weight in kilograms and height in centimetres : " );
scanf("%f%f",&weight,&height);
metric_height=height/100;
bmi=weight/(metric_height*metric_height);
if (bmi<15)
printf("Starvation");
else if (bmi>15.0 && bmi<17.6)
printf("Anorexic");
else if (bmi>17.5 && bmi<18.6)
printf("Under weight");
else if (bmi>18.5 && bmi<25.0)
printf("Ideal");
else if (bmi>24 && bm
i<30.0)
printf("Over weight");
else if (bmi>29 && bmi<40)
printf("Obese");
else
printf("Morbidly obese");
}

Nested Loop:
#include<stdio.h>
void main()
{
int i,j;
for(i=1;i<=5;i++)
{
printf("\n");
for(j=1;j<=1;j++)
{
printf("%d",j);
}
}
}

Break and continue:


Syntax:
break;
continue;

Break:

Continue:
Goto Statement:
Used to transfer toa specific label
Label must reside in the same function
The syntax for goto statement
goto label:

Write a program to print the series 1 + ½ + 1/3


Function:
1. Pre-defined
2. User-defined
An example for User-Defined function:

Function Components:

Void main()- does not return any value


Function Declaration:

Function Definition:

Ex Program:
#include<stdio.h>
void add(int,int);
void main()
{
int a,b;
printf("\nEnter two numbers: ");
scanf("%d%d",&a,&b);
add(a,b);
printf("\nEnd of program");
}

void add(int a,int b)


{
int z;
z=a+b;
printf("%d",z);
}

Functions:
1. Pre-defined
2. User defined
Ex:
#include<stdio.h>
void greet();
void main()
{
greet();
}

void greet()
{
printf("Welcome");
}
Output:
Welcome
#include<stdio.h>
float area(float w,float l);
void main()
{
float a,w,l;
printf("Enter width: ");
scanf("%f",&w);
printf("Enter length: ");
scanf("%f",&l);
a=area(w,l);
printf("\n%f",a);
}
float area(float w,float l)
{
return w*l;
}

#include<stdio.h>
double power(double a, double b);
void main()
{
double a,b,p;
printf("Enter two numbers : ");
scanf("%lf%lf",&a,&b);
p=power(a,b);
printf("%.2lf",p);
}

double power(double x, double y)


{
double f=1;
int i;
for (i=1;1<abs(y)+1;i++)
{
f=f*y;
return f;
}
}

How to pass argument through function?

1. Call by value (no affects outside)


2. Call by reference (affects outside)

Recursion:
Infinite Execution:
void main()
{
printf(“hai\n”);
main();
}
Arrays:
#include<stdio.h>
int main()
{
int i=0,n,arr[20];
printf("\n Enter the number of elements : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);

}
printf("\n The array elements are ");
for(i=0;i<n;i++)

printf("Arr[%d]=%d\t",i,arr[i]);
}
Output:
Enter the number of elements : 1                                      
2                                                                      
                                                                       
 The array elements are Arr[0]=2 

#include<stdio.h>
int fact(int n);
void main()
{
int n,r,f1,f2,f3,ncr;
read:
printf("Enter Value for N: \nEnter Value for R: ");
scanf("%d%d",&n,&r);
if(n<r)
{
printf("\nN should be greater than R");
goto read;
}

f1=fact(n);
f2=fact(r);
f3=fact(n-r);
ncr=f1/(f2*f3);
printf("%d",ncr);
}

int fact(int n)
{
int f=1;
if(n>1)
f=n*fact(n-1);
return f;
}

Portions for 2nd Internals


#include<stdio.h>
#include<string.h>
int main()
{
char str1[20],str2[20],str3[40];
int n,choice;
printf("Enter first string:");
scanf("%s",str1);
printf("Enter Second String:");
scanf("%s",str2);
menu:
printf("1.Length of the String\n2.Copy the string\n3.Compare the
strings\n4.Join two strings(concatenate)\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Length of the string\n");
n=strlen(str1);
printf("Length of the first string is %d\n",n);
n=strlen(str2);
printf("Length of the second string is %d\n",n);
break;
case 2:
printf("%s",strcpy(str3,str1));
break;
case 3:
if (strcmp(str1,str2)==0)
{
printf("Strings are identical");
}
else
{
printf("Strings are not identical");
}
break;
case 4:
printf("%s",strcat(str1,str2));
break;
}
if(choice>=1 && choice<=6)
goto menu;
}

#include<stdio.h>
#include<string.h>
int main()
{
char str[20];
int n,i,j;
printf("ENter a string: ");
scanf("%s",str);
n=strlen(str);
i=0;
j=n-1;
while(i<=j)
{
if(str[i]!= str[j])
break;
i++;
j--;
}
if(i>j)
printf("The string is a palindrome\n");
else
printf("The string is not a palindrome\n");
}

Structure:
It can store elements of different data types.
Syntax:
struct< tagname or structure name>
{
Structure members
};

#create a structure to store customer information


struct customer
{
Char name[20];
Char idno[20];
Char add[20];
Int mobileno;
};
#create a structure to store elements of bookdetails
struct book
{
Char name[20];
Char author[20];
Int editionno[30];
Int id[20];
Int year[5];
Int ISBN[20];
Char publisher[20];
};

#create a structure variable

b1 is structure variable!
#include<stdio.h>
struct book
{
char title[20];
int pages;
}b1;
void main()
{
printf("Enter title ");
scanf("%s",b1.title);
printf("\nEnter no. of pages ");
scanf("%d",&b1.pages);
printf("\nTitle : %s",b1.title);
printf("\nNo. of pages : %d",b1.pages);
}

#include<stdio.h>
struct details
{
char name[20];
int age;
char registno[20];
char gender[10];
}b1;
int main()
{
printf("Enter your name: ");
scanf("%s",b1.name);
printf("Enter your age: ");
scanf("%d",&b1.age);
printf("Enter your Register Number: ");
scanf("%s",b1.registno);
printf("Enter your Gender: ");
scanf("\n%s",b1.gender);
printf("\n%s",b1.name);
printf("\n %d",b1.age);
printf("\n%s",b1.registno);
printf("\n%s",b1.gender);
}

Also
#include<stdio.h>
struct student
{
char name[25];
int age;
};
struct student s1={"john",25};
struct student s2={"kumar",45};
if(s1.age == s2.age)
{
printf("BOTH ARE SAME");
}
else
{
printf("NOT EQUAL");
}
write a program using struct tofind biggest among three members
#include<stdio.h>
struct big
{
int a;
int b;
int c;
}s1={50,25,20};
int main()
{
if(s1.a>s1.b && s1.a>s1.c)
{
printf("A is Big");

}
else if(s1.b>s1.a && s1.b>s1.c)
{
printf("B is Big");
}
else
{
printf("C is Big");
}
}

ARRAY OF STRUCTURES:

#Create a structure employee with members name,age,salary.Accept the input


and print the value.
Array of Structure
Union?
Type Def:
Enter the height of 3 people: 12 64 85
Average height: 53.666668

Enumerator Data types

#include<stdio.h>
int main()
{
enum type{alphabet,digit,special};
enum type c,t;
printf("Enter a Character: ");
scanf("%c",&c);
if(isalpha(c))
{
t=alphabet;
}
else if(isdigit(c))
{
t=digit;
}
else
{
t=special;
}
switch(t)
{
case alphabet:
printf("\nits an ALPHABET %c",c);
break;
case digit:
printf("\nits an DIGIT %c",c);
break;
case special:
printf("\nits an SPECIAL CHARACTER %c",c);
break;
}

Pointer:
Sum of ARRAY elements in “Array”.

For 3rd internals


recursion,arrays,strings,pointers,structures.

#include<stdio.h>
struct cust
{
int custId;
char custName[50];
long int contactNo;
char custAddress[100];
char custDob[10];
int custAge;
float custMaxamt;
};
int main()
{
int i,n;
struct cust c[50];
printf("Enter the number of records to store\n");
scanf("%d",&n);
for(i=0;i<=n-1;i++)
{
printf("Enter customer Id,customer Name, contact No, customer Address,
customer Dob,customer age, customer Maximum Amount details one by one\n");
scanf("%d%s%ld%s%s%d
%f",&c[i].custId,c[i].custName,&c[i].contactNo,c[i].custAddress,c[i].custDob,&
c[i].custAge,&c[i].custMaxamt);
}

printf("****************************CUSTOMER PROFILE
****************************\n");
printf("Customer ID\t Customer Name\t Contact No\t, Customer Address\t,
Customer DOB\t, Customer Age\t, Customer Maximum Amount\n");
for(i=0;i<=n-1;i++)
{
printf("%d\t%s\t%ld\t%s\t%s\t%d\t
%f\n",c[i].custId,c[i].custName,c[i].contactNo,c[i].custAddress,c[i].custDob,c[i].
custAge,c[i].custMaxamt);

}
printf("************************* End of Customer Profile
*************************");
}
#include<stdio.h>
int main()
{
FILE *fp;
int regno;
char name[20];
float marks;
double fees;
int n,i;
fp=fopen("E:\\a.dat","w");
printf("Enter the number of students :\n");
for(i=1;i<=n;i++)
{
printf("Enter regno :");
scanf("%d",&regno);
printf("Enter name :");
scanf("%s",name);
printf("Enter percentage of marks:");
scanf("%f",&marks);
printf("Enter the fees: :");
scanf("%lf",&fees);
fprintf(fp,"%d\t%s\t%f\t%lf\n",regno,name,marks,fees);
}
fclose(fp);
fp=fopen("E:\\a.dat","r");
printf("\nThe student details are\n");
while(fscanf(fp,"%d%s%f%lf",&regno,name,&marks,&fees)>0)
{
printf("\nRregno : %d",regno);
printf("\nName : %s",name);
printf("\nPercentage of marks : %f",marks);
printf("\nFees : %lf",fees);
printf("\n---------------------------\n");
fclose(fp);
}
}

You might also like