Showing posts with label C. Show all posts
Showing posts with label C. Show all posts

Sunday, 18 October 2020

C program to read a name from keyboard and display it on the screen

October 18, 2020 Posted by Knowledge Bite , No comments

 Hello Friends

Today i will tell you about the String's in the 'C' Language. String's are basically Character Array. 

Examples of of string constants

char name[]={'K','N','O','W','L','E','D','G','E','\0'};

char name[]="KNOWLEDGE";

It is a string constant of one dimensional array of characters which is terminated by null ('\0');

here '\0' is called the null character in it \n indicates that what it follows is something special. In string's null character is most important because it is the only one which know where the string ends. Null character i.e. '\0' need not to be entered in the declaration 'C' automatically insert it at the end of the string's so we can declare them as in the above 2nd example

Each character in the string which is stored in an array occupy one byte of memory.

let's try one example of printing the string

//Program to print the string

#include<stdio.h>

#include<conio.h>

    {

        char name[]="KNOWLEDGE BITE";        

        int t=0;

        clrscr();

            while (t<=13)

                {    

                    printf("%c",name[t]);

                    t++;

                }

    getch();    

    }


Output will be:

KNOWLEDGE BITE

NOW LET'S TRY ANOTHER C PROGRAM TO READ A NAME FROM KEYBOARD AND DISPLAY IT ON THE SCREEN.

#include<conio.h>

#include<stdio.h>

void main()

    {

        char name[30];

        clrscr();

        printf(" Hi! What is your Full Name. \n User Enter Name from Keyboard ");

        scanf(%[^\n]s",name);

        printf("O Hi! My Full Name is %s ",name);

        getch();

    }

Output of the given program is

Hi! What is your Name.

User Enter Name from Keyboard #Knowledge_Bite

O Hi! My Full Name is #Knowledge_Bite


Sunday, 1 November 2015

Practical File Of C

November 01, 2015 Posted by Knowledge Bite , , No comments
INDEX
Sr.No.
Title
Signature
Date
1.
WAP TO PRINT A MESSAGE ON THE SCREEN


2.
WAP TO ADD TWO NUMBERS 


3.
WAP TO SUBTRACT  TWO NUMBERS 


4.
WAP TO MULTIPLY TWO NUMBERS 


5.
WAP TO DIVIDE TWO NUMBERS 


6.
WAP TO CALCULATE MODULUS OF TWO NUMBERS 


7.
WAP TO calculate area of circle


8.
WAP USING DO WHILE LOOP


9.
WAP USING WHILE LOOP


10.
WAP USING FOR LOOP


11.
WAP TO PRINT SUM OF FIRST 10 NATURAL NUMBERS


12.
WAP TO FIND GREATER OF TWO NUMBERS


13.
PROGRAM FOR SWITCH STATEMENT


14.
WAP TO DISPLAY AVERAGE OF N NUMBERSUSING ARRAY


15.
WAP FOR  CALL BY VALUE  USING  FUNCTION


16.
WAP FOR  CALL BY REFERENCE  USING  FUNCTION


17.
WAP FOR STRING CONCATENATION


18.
WAP FOR  COPY ONE STRING INTO ANOTHER


19.
WAP FOR STRING COMPARISION


20.
WAP TO CALCULATE LENGTH OF A STRING


21.
WAP TO PRINT FIRST 10 NATURAL NUMBERS USING 1D ARRAY


22.
WAP TO PRINT  ELEMENTS OF 2D ARRAY


23.
WAP TO IMPLEMENT  STRUCTURE


24.
WAP TO IMPLEMENT  STRUCTURE  POINTER


25.
WAP TO IMPLEMENT UNION IN C


26.
WAP TO COUNT CHARS, SPACES, TABS AND NEWLINES IN A FILE 


27.
WAP TO COPY ONE FILE INTO ANOTHER FILE








PROGRAM 1
AIM: WAP TO PRINT A MESSAGE ON THE SCREEN
 #include<stdio.h>
#include<conio.h>
void main()
{
printf("Welcome BCA 1st year students”);
getch();
}

Output:
Welcome BCA 1st year students

PROGRAM 2
AIM: WAP TO ADD TWO NUMBERS 
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("Enter the value of a and b \n");
scanf("%d%d",&a,&b);
c=a+b;
printf("SUM = %d”,c);
getch();
}

Output:
Enter the value of a and b
10
20
SUM = 30

PROGRAM 3
AIM: WAP TO SUBTRACT  TWO NUMBERS 
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("Enter the value of a and b \n");
scanf("%d%d",&a,&b);
c=a-b;
printf("SUBTRACTION = %d”,c);
getch();
}

Output:
Enter the value of a and b
20
10
SUBTRACTION = 10

PROGRAM 4
AIM: WAP TO MULTIPLY TWO NUMBERS 
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("Enter the value of a and b \n");
scanf("%d%d",&a,&b);
c=a*b;
printf("MULTIPLICATION = %d”,c);
getch();
}

Output:
Enter the value of a and b
10
20
MULTIPLICATION = 200

PROGRAM 5
AIM: WAP TO DIVIDE TWO NUMBERS 
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("Enter the value of a and b \n");
scanf("%d%d",&a,&b);
c=a/b;
printf("DIVISION = %d”,c);
getch();
}

Output:
Enter the value of a and b
20
10
DIVISION = 2

PROGRAM 6
AIM: WAP TO CALCULATE MODULUS OF TWO NUMBERS 
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("Enter the value of a and b \n");
scanf("%d%d",&a,&b);
c=a%b;
printf("MODULUS = %d”,c);
getch();
}

Output:
Enter the value of a and b
15
10
MODULUS = 5

PROGRAM 7
AIM: WAP TO CALCULATE AREA OF CIRCLE
#include<stdio.h>
#include<conio.h>
void main()
{
int r;
float a=3.14,area;
clrscr();
printf("to calculate area of circle \n");
printf("enter the value of r \n");
scanf("%d",&r);
area=a*r*r;
printf("area of the circle is %f,area);
getch();
}

Output:
to calculate area of circle
enter the value of r
2
area of the circle is 12.56






PROGRAM 8
AIM:WAP USING DO WHILE LOOP

#include<stdio.h>
#include<conio.h>
void main()
{
int i=1;
do
{
printf("i=%d",i);
i++;
}
while(i<=5);
getch();
}

OUTPUT:
i=1i=2i=3i=4i=5



PROGRAM 9
AIM:WAP USING WHILE LOOP

#include<stdio.h>
#include<conio.h>
void main()
{
int i=1;
while(i<=5)
{
printf("i=%d",i);
i++;
}
getch();
}

OUTPUT:
i=1i=2i=3i=4i=5

PROGRAM 10
AIM:WAP USING FOR LOOP

#include<stdio.h>
#include<conio.h>
void main()
{
int i;
for(i=1;i<=5;i++)
{
printf("i=%d",i);
}
getch();
}

OUTPUT:
i=1i=2i=3i=4i=5

PROGRAM 11
AIM : WAP TO PRINT SUM OF FIRST 10 NATURAL NUMBERS
#include<stdio.h>
#include<conio.h>
void main()
{
int i,sum=0;
clrscr();
for(i=1;i<=10;i++)
{
sum=sum+i;
}
printf("sum=%d",sum);
getch();
}

OUTPUT
sum=55

PROGRAM 12
AIM : WAP TO FIND GREATER OF TWO NUMBERS
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,g;
clrscr();
printf("to find greater of two numbers");
printf("enter the value of a and b\n");
scanf("%d%d\n",&a,&b);
if(a>b)
printf("a is greater than b\n");
else
printf("b is greater than a\n");
getch();
}

Output    to find greater of two numbers enter the value of a and b
45
80
b is greater than a





PROGRAM  13
AIM: PROGRAM FOR SWITCH STATEMENT
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,ch;
float d;
printf("choices are\n");
printf("\n 1. ADD");
printf ("\n 2. SUB");
printf("\n 3. DIV");
printf("\n 4. MUL");
printf("\n 5. Mod");
printf("enter the value of a and b");
scanf("%d%d",&a,&b);
printf("enter the choice");
scanf("%d",&ch);
switch(ch)
{
case 1:
c=a+b;
printf("c=%d",c);
break;
case 2:
c=a-b;
printf("c=%d",c);
break;
case 3:
c=a/b;
printf("c=%d",c);
break;
case 4:
c=a*b;
printf("c=%d",c);
break;
case 5:
c=a%b;
printf("c=%d",c);
break;
default:
printf("wrong choice entered\n");
}
getch();
}

Output   choices are
 1. ADD
 2. SUB
 3. DIV
 4. MUL
 5. mod enter the value of a and b 40
30
enter the choice 4
c=1200

PROGRAM 14
AIM: WAP TO DISPLAY AVERAGE OF N NUMBERSUSING ARRAY
#include<stdio.h>
#include<conio.h>
void main()
{

float data[5];
float total;
float average;
data[0]=34.0;
data[1]=27.0;
data[2]=45.0;
data[3]=82.0;
data[4]=22.0;
total=data[0]+data[1]+data[2]+data[3]+data[4];
average=total/5.0;
printf("%f%f\n",total,average);
getch();
}


OUTPUT  210.00000042.00000








PROGRAM  15
AIM: WAP FOR  CALL BY VALUE  USING  FUNCTION
#include<stdio.h>
#include<conio.h>
int sum (int,int);
void main()
{
int a=10,b=20,c;
c=sum(a,b);
printf("c=%d",c);
getch();
}
int sum(int x,int y)
{
int z;
z=x+y;
//printf("z=%d",z);
return(z);
}

Output    c=30



PROGRAM  16
AIM: WAP FOR  CALL BY REFERENCE  USING  FUNCTION
#include<stdio.h>
#include<conio.h>
void sum (int *,int *);
void main()
{
int a=10,b=20;
sum(&a,&b);
getch();
}
void sum(int *x,int *y)
{
int z;
z=*x+*y;
printf("z=%d",z);
//return(Z);
}

Output  z=30

PROGRAM  17
AIM: WAP FOR STRING CONCATENATION
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[20],b[10];
clrscr();
puts("enter the first string");
gets(a);
puts("enter the second string");
gets(b);
strcat(a,b);
puts("after concatenation of string");
puts(a);
getch();
}

OUTPUT  
enter the first string
abc
enter the second string
xyz
after concatenation of string
abcxyz


PROGRAM 18
AIM: WAP FOR  COPY ONE STRING INTO ANOTHER
#include<stdio.h>
#include<conio.h>

#include<string.h>
void main()
{
char a[10],b[10];
clrscr();
puts("enter the string a");
gets(a);
strcpy(b,a);
puts("After copying string b=");
puts(b);
getch();
}

OUTPUT
enter the string a
abc
After copying string b=
Abc





PROGRAM 19
AIM: WAP FOR STRING COMPARISION
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[10],b[10];
int i;
clrscr();
puts("enter the string a");
gets(a);
puts("enter the string b");
gets(b);
i=strcmp(a,b);
if(i==0)
puts("both string are equal");
else if(i>0)
{
puts("string a is greater than string b");
}
else if (i<0)
{
puts("string b is greater than string a");
}
getch();
}
OUTPUT
enter the string a
abc
enter the string b
xyz
string b is greater than string a
PROGRAM 20
AIM : WAP TO CALCULATE LENGTH OF A STRING
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[20];
int l;
clrscr();
puts("enter any string:");
gets(a);
l=strlen(a);
printf("total number of character=%d",l);
getch();


OUTPUT
enter any string:
abcxyz
total number of character=6


PROGRAM  21
AIM : WAP TO PRINT FIRST 10 NATURAL NUMBERS USING 1D ARRAY
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10];
int i;
clrscr();
printf("Enter array elements\n");
for(i=0;i<9;i++)
{
scanf(“%d”,&a[i]);
}
printf("The array elements are : \n");
for(i=0;i<9;i++)
{
printf(“%d  ”,a[i]);
}
getch();
}

OUTPUT
The array elements are :
1 2 3 4 5 6 7 8 9 10





PROGRAM 22
AIM : WAP TO PRINT  ELEMENTS OF 2D ARRAY
#include<stdio.h>
#include<conio.h>
void main()
{
int a[2][2];
int i,j;
clrscr();
printf("Enter the array elements \n");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
scanf("%d",&a[i][j]);
}
printf("The array elements are : \n");

for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
printf("%d \t",a[i][j]);
}
printf("\n");
}
getch();
}





OUTPUT
Enter the array elements
1 2
3 4
The array elements are :
1 2
3 4
 PROGRAM 23
AIM : WAP TO IMPLEMENT  STRUCTURE
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct student
{
char name[20];
int age,rollno;
}s1;
void main()
{
clrscr();
strcpy(s1.name,"ram");
s1. age=18;
s1.rollno=281;
printf("details of student s1:\n");
printf("student name=%s\n age=%d\n rollno=%d\n",s1.name,s1.age,s1.rollno);
getch();
}

Output 
details of student s1:
student name=ram
 age=18
 rollno=281



PROGRAM 24
AIM : WAP TO IMPLEMENT  STRUCTURE  POINTER
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
struct student
{
char name[20];
int age,rollno;
}s1;
clrscr();
strcpy(s1.name,"ram");
s1. age=18;
s1.rollno=281;
struct student *st;
st=&s1;
printf("details of student s1:\n");
printf("student name=%s\n age=%d\n rollno=%d\n",st->name,st->age,st->rollno);
getch();
}

Output 
details of student s1:
student name=ram
 age=18
 rollno=281


PROGRAM 25
AIM:  WAP TO IMPLEMENT UNION IN C
# include<stdio.h>
# include<conio.h>
void  main()
{
union a
{
int  I;
char ch[2];
};
union a key;
key.i=5;
printf(“key.i = %d \n”, key.i);
printf(“key.ch[0] = %d \n”, key.ch[0]);
printf(“key.i = %d \n”, key.ch[1]);
getch();

OUTPUT
Key.i=5
Key.ch[0]=0
Key.ch[1]=2

PROGRAM 26
AIM:  WAP TO COUNT CHARS, SPACES, TABS AND NEWLINES IN A FILE 
#include<stdio.h>
int main()
{
 FILE *fp;
 char ch;
 int nol=0, not=0, nob=0, noc=0;
 fp=fopen("pr1.c","r");
 while(1)
{
 ch=fgetc(fp);
 if(ch==EOF)
 break;
 noc++;
 if(ch=='')
  nob++;
 if(ch=='\n')
  nol++;
 if(ch=='\t')
  not++;
}
fclose(fp);
printf("number of characters = %d\n", noc);
printf("number of blanks = %d\n",nob);
printf("number of tabs = %d\n",not);
printf("number of lines = %d\n",nol);
}

OUTPUT
number of characters = 15
number of blanks = 5
number of tabs = 2
number of lines = 2


PROGRAM 27
AIM:  WAP TO COPY ONE FILE INTO ANOTHER FILE
#include<stdio.h>
#include<stdlib.h>
int main()
{
 FILE *fs, *ft ;
 Char ch;
fs=fopen (“pr1.c”,”r”);
If (fs==NULL)
{
puts (“cannot open source file”);
exit(1);
}
ft=fopen (“pr2.c”,”w”);
if(ft==NULL)
{
puts (“cannot open target file”);
fclose(fs);
exit(2);
 }
While(1)
{
Ch=fgetc(fs);
If(ch==EOF)
break;
else
fputc(ch,ft);
}
fclose(fs);
fclose(ft);
return 0;
}