Final Project
Final Project
Sample C program
The smallest individual unit in a C program is known as C token a program can
also be called as collection of various token.
1. Software tool: either code block or Dev++ or Notepad++…select the familiar
one and implement the following C code.
For example, the given C code is implemented on code block.
2. when you first open code block you get this form
#include <stdio.h> //perprocessor,include the compiler, standard
input output
#include <stdlib.h>
int main() //main function excution can be start from
this
{
printf("Hello world!\n"); //printf for output,
return 0;
}
3. when you declare variable in c program by using integer, character, string,
float and others are similar to c++.
4. In c program when you accept input from user you can use “&”and use the
key word “scanf”.“& “symbol is indicate the address of the variable
5. when you display output to in c program you can use special place holder for:-
Int=%d
Char=%c
String=%s
Float=%f
Etc…
6. The program is executing either directly by clicking build and run button or
using command prompt.
1 //write a C code that displays the sum of two numbers.
#include <stdio.h>
#include <stdlib.h>
int main()
{
float a,b,c;
printf("enter the first number\n");
scanf("%f",&a);
printf("enter the second number\n");
scanf("%f",&b); //use f when the input data type is float
c=a+b;
printf("the sum of %f and %f is %f",a,b,c);
return 0;
}
2. write a C code that displays the absolute value of the accepted integer.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a,b;
printf("enter the number\n");
scanf("%d",&a);
if(a<0)
{
b=a*(-1);
printf("the absolute value of %d is %d",a,b);
}
else {
printf("the absolute value of %d is %d",a,a);
}
return 0;
}
3. write a C code that displays A1-Z26
#include <stdio.h>
#include <stdlib.h>
int main(){
char i;
int j=1;
for(i='A';i<='Z';i++) {
printf("%C%d\t",i,j);
j++; }
return 0; }
scanf("%d",&op);
switch(op)
{
case 1:
add(a,b);
break;
case 2:
divs(a,b);
break;
case 3:
mult(a,b);
break;
case 4:
sub(a,b);
default:
printf("some error occur");
}
return 0;
}
How to read and write file in C program
5. to write some file when you execute programs on code block input this program shows how
to “struct” derived data type is used.
Create file called “array.txt” by the programmer.
#include <stdio.h>
#include <stdlib.h>
int main ()
{
FILE *ptr;
ptr=fopen("array.txt","w");
struct student {
char fn[12];
int a;
float c;
}s[4];
int i,j;
for( i=0 ;i<=3;i++)
{
printf("enter your name ,age and cgpa\n");
scanf("%s%d%f",&s[i].fn,&s[i].a,&s[i].c);
}
for( j=0 ;j<=3;j++)
{
}
fclose(ptr);
return 0;
}