0% found this document useful (0 votes)
17 views56 pages

C Programming Problem Solving Notes

This document discusses problem solving techniques in programming. It covers: 1. The generations of programming languages from the first to fifth generations and their key characteristics. 2. Common problem solving steps like requirement analysis, design, implementation, testing, and maintenance. 3. Programming concepts like algorithms, flowcharts, pseudocode, and common errors like syntax, semantic, logical, and linker errors. 4. An overview of the C programming language, its history and taxonomy, basic data types, and input/output functions like scanf and printf.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views56 pages

C Programming Problem Solving Notes

This document discusses problem solving techniques in programming. It covers: 1. The generations of programming languages from the first to fifth generations and their key characteristics. 2. Common problem solving steps like requirement analysis, design, implementation, testing, and maintenance. 3. Programming concepts like algorithms, flowcharts, pseudocode, and common errors like syntax, semantic, logical, and linker errors. 4. An overview of the C programming language, its history and taxonomy, basic data types, and input/output functions like scanf and printf.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

PROBLEM SOLVING NOTES:

[Link] of Programming language


[Link] Thereja "Programming in c" (Text Book)
3.20CA2002(subject Lab code), 20ca2003(lab code)
[Link] Generation: ML(1950)
[Link] for programming
2.
[Link] generation: AL
[Link] programming language
[Link] when compared to ML
[Link] Generation: Programming language
[Link] and easy for programming
[Link] level language
[Link] and Compiler are the translators to Machine language
[Link] are portable
[Link] Generation:Very High Level Language
[Link] are non procedural
[Link] easy and useful
[Link] Generation:
[Link] visual tools to develop a program
[Link] s are widely used in Artificial intelligence
[Link] include Prolog,OPS s, Mercury
[Link]:-
[Link] Analysis
[Link] the program/software has to be built
[Link] the identified requirement
[Link] functionality, capability, performance, availability of harware
and software
[Link]
[Link] documentation in the previous phase act as an iinput to
the design phase
[Link] phase is broken down into modules
[Link] modules is specified in the form of algorithm, flowcharts or
pseudo code
[Link] phase specifies how the programs/software will be built
[Link]
[Link] algorithm are converted into program code using high
level language
[Link] the programming language
[Link] is compatible with the hardware
[Link]
[Link] the modules are tested together to ensure that all the oveerall
system works well as a whole product
[Link] integration of modules are done there may be nug this is
tested
[Link] with the large number of varied inputs known as test data
[Link] Deployment, training and support
[Link]
[Link]:-
[Link]
[Link] the value
[Link] the Value
[Link]
[Link] chart:-
[Link] with symbols(pictorial representation)
[Link] code:-
[Link] of a program
[Link] CLassroom CODE ---flbgqpm
[Link], Flow chart, Pseudo code
[Link] an algotithm to find out area of a circle?
[Link] 1:start
[Link] 2:Get value for r (input operation)
[Link] 3:Calculate Area=Pi*r*r (Processing)
[Link] 4:Print Area (Output operation/Execution)
[Link] 5:End
[Link] about Raptor tool(Download it)
[Link] 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
[Link] Statement
[Link] code:-
[Link] in books
[Link] like programming
3.
[Link] a code forcalculating the price of a product afteradding sales taxto its
original price
[Link] the price of the product
[Link] the sales tax rate
3.
[Link] of errors:-
[Link]-Time-
[Link] when program is executed
[Link] occurs when the program performs some illegal operations
like
[Link] number by 0
[Link] file that already exist
[Link] of free memory space
[Link] square or log of -ve nos.

[Link]-time Errors:-
[Link] errors:-
[Link] of c programming lang are violated
2. int a: //syntax error
[Link] errors:-
[Link] with the rules of the programming but not
meaningful to the compiler
2.a*b=c//Does not seem correct
[Link] errors:-
[Link] are unexpected and undesirable output
[Link] detected/occurs due to incorrect statements
3.c=a+b//to be
4.c=a*b//mistake
[Link] errors:-
[Link] when the linker is not able to find the function
definition
[Link]
[Link] included #include<stdio.h>
[Link]
[Link]
[Link] converts High level language into Machine level language
[Link] handling
[Link]:(Comes under Syntax Error)
Datatype variablename;
eg:
int a;
[Link] Error:-
no meaning to the compiler
eg:
a*b=c/no meaning
C=a*b/more meaning (a is operand, * is operator)
[Link] error:-
can'tbe detected by compiler
eg:
C=a+b/to be done
C=a*b/by mistake
[Link] Error:-
occur when the linker isnot able to find the function definition
eg:
Printf
not included #include<stdio.h>
[Link] and debugging approaches:-
[Link] tests- Checks the single module
[Link] test- Two units are combined and checked
[Link] Test- To test the entire system
[Link] approaches:-
[Link] force
[Link] tracking
[Link] elimination

1.C programming:-
1.C was devolpedin 1970 by Dennis ritchie
[Link] of c
[Link]-Algorithmic language
[Link]-Basic combined programming language
3.B-Ken Thompson
4.C-Traditional C
5.K&RC
[Link] C
7.C89
8.C95
9.C99
[Link] high level language
[Link] 32 keywords
[Link] is structured programming language

[Link] file(extension .c), header file(extension.h),object file([Link]),


executable file([Link])
[Link] file(string.h, stdlib.h,stdio.h,math.h,alloc.h,conio.h)

[Link] section
[Link] line comment(//), [Link] comment(/*....)
[Link] section(preprocessor directive)
ex., #include <stdio.h>
[Link] section
ex.,# define PI 3.14
[Link] declaration section
[Link] function
{
Declaration part
Executable part
}
7. cc greet.c
8. ./[Link]
9. nano display.c
[Link] (online coding resource)
[Link] set in C
[Link] in C language
[Link] and data types
[Link] Data Types in C:-
[Link]
[Link]
[Link]
[Link]
[Link] a variable:-
syntax-:- datatype variablename,
Eg. int a;, float g;
[Link] a program to assign a interger value!
[Link]- printf("control string", variablename);
[Link] data types in c:-
[Link]
[Link]
[Link]
[Link] stores characters
[Link] of data type:-
[Link]
[Link]
[Link]
[Link] format
[Link] float or double values are stored in memory?
MSD-----------------------------LSB
[Link] point must be converted binary
[Link] the converted binary to normalize form(format= [Link]
bit*2exponent)
[Link] bias to exponent
[Link] function
[Link] programming on [Link]

[Link] output statements in c:-


[Link] source of data as well as the destination of data (stream)
[Link] Stream, Binary stream
eg:- Keyboard-->
[Link]("control string",variable list); ---> syntax
[Link] test
[Link] 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);
}

[Link]()-
[Link]:-
scanf("conrtl str1,,,")
[Link] prototype of the control string can be
%[*][width][modifiers]type
[Link] to use a scanf function
[Link] 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);
}

[Link] 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

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

[Link] 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")
[Link] operator:-
&(binary AND),|(Binary OR),^(Binary XOR),`(Binary one
compliment),<<(Binary Left shift),>>(Binary right shift)

[Link] operator: -
1. =,+=,-=,*=,/=,%=,**=
[Link] operator
[Link]
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);
}

[Link] conversion and type casting: -


[Link] conversion is done implicitly
[Link] done by the programmer
3.
[Link] = int(9.5)
[Link]=(int)12.3/(int)4.2
[Link]=(int)(a+b)
a=500, b=70
[Link] a program to convert an integer into the coressponding floating number

[Link] to scopes of variables


[Link] Scope
{} local variable
[Link] scope
goto label has function scope
eg:

int main()
{
Loop:
..
..
goto Loop;
return0
}
[Link] scope
can access a variable through out the program
[Link] 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

[Link] of Storage class <storage_class_specifier><data type><variable name>


[Link]
Default storage class for variable declaration inside a block
Eg:
auto int x;
[Link]
Stored in CPU, Maximum size of the variable will be equal to the
register size
Eg;
register int x;
[Link]
Global variable,it's not initialized, will be initialized to zero
Eg:
extern int x;
[Link]
Static variables have life time over the entire program
Eg:
statics int x=10;
[Link] 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("[Link] of the String\[Link] the string\[Link] the
strings\[Link] 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",[Link]);
printf("\nEnter no. of pages ");
scanf("%d",&[Link]);
printf("\nTitle : %s",[Link]);
printf("\nNo. of pages : %d",[Link]);
}

#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",[Link]);
printf("Enter your age: ");
scanf("%d",&[Link]);
printf("Enter your Register Number: ");
scanf("%s",[Link]);
printf("Enter your Gender: ");
scanf("\n%s",[Link]);
printf("\n%s",[Link]);
printf("\n %d",[Link]);
printf("\n%s",[Link]);
printf("\n%s",[Link]);
}

Also
#include<stdio.h>
struct student
{
char name[25];
int age;
};
struct student s1={"john",25};
struct student s2={"kumar",45};
if([Link] == [Link])
{
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,[Link] 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:\\[Link]","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:\\[Link]","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