0% found this document useful (0 votes)
18 views

Final Project

Uploaded by

slideshare
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)
18 views

Final Project

Uploaded by

slideshare
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
You are on page 1/ 7

LAB 1

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.

Save the code desktop/code/LAB11/add.c

#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; }

How to use function in C program?


There are 3 types of function this are
1. Proto type or function declaration
2. Function definition
3. Function calling
It contains function name, parameter, body and return type.
Eg, void add(int a,int b);
{
//Some execute program
}

//the difference b/n scanf and gets function


When you use scanf input is collection of string means that between each string after space not
display but gets function display the whole input. With considering space. For example
#include <stdio.h>
#include <stdlib.h>
int main() {
char str[12];
printf("enter name:\n");
//scanf("%s",str); //read string excluding space
gets(str);//read string including space
printf("you have %s:\n",str);
return 0;}
4. write C code by using function and perform arithmetic operation based on user’s
choice.
#include <stdio.h>
#include <stdlib.h>
void add(int a,int b)
{
int sum=a+b;
printf("the sum of the number is %d",sum);
}
void divs(int a,int b)
{
float div=a/b;
printf("the questiont of the number is %f",div);
}
void mult(int a,int b)
{
int mult=a*b;
printf("the product of the number is %d",mult);
}
void sub(int a,int b)
{
int sub=a-b;
printf("the difference of the number is %d",sub);
}
int main()
{
int a,b,op;
printf("enter the first number\n");
scanf("%d",&a);
printf("enter the second number\n");
scanf("%d",&b);
printf("enter either 1,2,3 or4\n");

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++)
{

printf("name =%s \t\t\ age is %d \t\t cgpa is%f \n", s[j].fn,s[j].a,s[j].c);


fprintf(ptr,"name =%s \t\t\ age is %d \t\t cgpa is%f \n", s[j].fn,s[j].a,s[j].c);

}
fclose(ptr);

return 0;
}

You might also like