Tuesday, 8 December 2020
Sunday, 18 October 2020
C program to read a name from keyboard and display it on the screen
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
Tuesday, 22 September 2020
C Program to swap two number using Functions and Pointer's
#include<stdio.h>
#include<conio.h>
void main()
{
void swap(int*,int*);
int a,b;
clrscr();
printf("Enter the value of a = ");
scanf("%d",&a);
printf("Enter the value of b = ");
scanf("%d",&b);
swap (&a,&b);
printf("The value of a = %d \nThe value of b = %d",a,b);
getch();
}
void swap(int *a,int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
}
C Program to find the number is a Prime Number or Not
#include<stdio.h>
#include<conio.h>
void main()
{
int num,i;
clrscr();
printf("Enter the number = ");
scanf("%d",&num);
i=2;
while (i<=num-1)
{
if (num%i==0)
{
printf("%d is not a prime number",num);
break;
}
i++;
}
if (i==num)
{
printf("%d is a Prime Number",num);
}
getch();
}
C program to find the sum of digits of any 5 digit number
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr(); //to add the digits of any five digits number
int p,q,r,s,t,u,v,w,x,y;
printf("enter any number of five digit\n for which you want to add its digits\n");
scanf("%d",&p);
q=p%10;
r=p/10;
s=r%10;
t=r/10;
u=t%10;
v=t/10;
w=v%10;
x=v/10;
y=q+s+u+w+x;
printf("%d",y);
getch();
}
C Program to find the factorial of a number using Recursion
#include <stdio.h>
long factorial(int n)
{
if (n == 0)
return 1;
else
return(n * factorial(n-1));
}
void main()
{
int number;
long fact;
printf("Enter a Number: ");
scanf("%d", &number);
fact = factorial(number);
printf("Factorial of %d is %ld\n", number, fact);
getch();
}