Upload
vissu-sweet
View
216
Download
0
Embed Size (px)
Citation preview
7/28/2019 C Progms List
1/32
1. Write a C program to check whether number is even or odd.
Ans:
#include
main()
{
intnum;
printf("Enter a number to be checked for even/odd: ");
scanf("%d",&num);
if(num%2==0)
{
printf("The entered number is EVEN.\n");
}
else
{
printf("The entered number is ODD.\n");
}
}
2. Write a C program to illustrate the use of STATIC keyword and the scope of variables.
Ans: #include
void func()
{
static int x = 0;
// x is initialized only once across three calls of func()
printf("%d\n", x); // outputs the value of x
x = x + 1;
}
int main(int argc, char * const argv[]) {func(); // prints 0
func(); // prints 1
func(); // prints 2
return 0;
}
3. Write a C program to check whether the given no is prime number or not?
Ans:
#include
void main()
{int n,pflag=0;
printf("enter the number \n");
scanf("%d",&n);
if(2
7/28/2019 C Progms List
2/32
{
if(n%5)
{
if(n%7) //if u need to know below 100 prime numbers.
{
pflag = 1;
}
}
}
}
}
if(pflag)
printf("%d is prime number \n",n);
else
printf("%d is not a prime number \n",n);
}
//============================================================================
#include#include
void main(){int n,i=2;
clrscr();printf("Enter a number \n");scanf("%d",&n);
while(i here the we get the remainder when n is divided by each i, at any time the remainder
is 0, the program will terminate and show a message " not prime". otherwise it will execute till i reaches to
n/2. and terminate the program and shows a message "prime".
//===============================================================
4. Write a C program to print all the prime numbers for 1 to 100.?
Ans:
#include
void main()
{
int a[100],i,j=2;
7/28/2019 C Progms List
3/32
for(i=0;i
7/28/2019 C Progms List
4/32
reverse = reverse + n%10;n = n/10;
}
printf("Reverse of entered number is = %d\n", reverse);
return 0;}
7. Write a C program to print the result of student by setting the grade (take the average sixsubjects and get the percentage print the grade) ?
8. Prepare a Current bill by verifying the following hints.From 1 to 100 units the unit charges 1 rupeeFrom 101 to 200 units the unit charges 2 rupeesFrom 201 to 350 unit charges 3.50 rupees.
If u enters the units output should display the amount.
9. Write a C program if u lower case letter output should be upper case letter ?
Ans: - 0x20 for every lower case or add 0x20 for every uppercase to get lower case.
10.Write a C program to print the Pascal triangle.Ans: A Pascal's triangle is a geometric arrangement of the binomial coefficients in a triangle.
1. The rows of Pascal's triangle are conventionally enumerated starting with row 0 which is
having only the number 1.
2. For other rows, add the number directly above and to the left with the number
directly above and to the right to find the new value .
3. If either the number to the right or left is not present, substitute a zero in its place. For
example, the first number in the first row is 0 + 1 = 1, whereas the numbers 1 and 2 in the
second row are added to produce the number 3 in the third row. In this way, the rows of
the triangle go on infinitely. Here we prompt the user to enter the number of rows to
display therefore only five rows will get displayed in the Pascal's triangle.
Sample output:
Enter the no. of lines: 8
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
#include
long fact(int);
int main(){
7/28/2019 C Progms List
5/32
int line,i,j;
printf("Enter the no. of lines: ");
scanf("%d",&line);
for(i=0;i
7/28/2019 C Progms List
6/32
}
12.Write a C program to print the tables 0 to 10.?
Ans: int i = 1;
int j = 1;
int result = 0
for (i = 1; i
7/28/2019 C Progms List
7/32
#include
void main()
{
int day1,mon1,year1,day2,mon2,year2;
int ref,dd1,dd2,i;
clrscr();
printf("Enter first day, month, year");
scanf("%d%d%d",&day1,&mon1,&year1);
scanf("%d%d%d",&day2,&mon2,&year2);
ref = year1;
if(year2
7/28/2019 C Progms List
8/32
return(y);
}
16.Write a C program to find the factorial of the given number?Ans:
#include
int main(){
int i=1,f=1,num;
printf("Enter a number: ");
scanf("%d",&num);
while(i
7/28/2019 C Progms List
9/32
getch();
}
19. Write a C program to sort out in the descending order from the array of given numbers.
Ans:
#include
voidmain ()
{inti,j,a,n,number[30];
printf("Enter the value of N\n");scanf("%d", &n);
printf("Enter the numbers \n");for(i=0; i
7/28/2019 C Progms List
10/32
if( strcmp(a,b) == 0 )printf("Entered strings are equal.\n");
elseprintf("Entered strings are not equal.\n");
return 0;}
C program to compare two strings without using strcmpHere we create our own function to compare strings.
int compare(char a[], char b[]){
int c = 0;
while( a[c] == b[c] ){
if( a[c] == '\0' || b[c] == '\0' )break;
c++;}if( a[c] == '\0' && b[c] == '\0' )
return 0;else
return -1;}
C program to compare two strings using pointersIn this method we will make our own function to perform string comparison, we will use character
pointers in our function to manipulate string.
#include
int compare_string(char*, char*);
main(){
char first[100], second[100], result;
printf("Enter first string\n");gets(first);
printf("Enter second string\n");gets(second);
result = compare_string(first, second);
if ( result == 0 )printf("Both strings are same.\n");
elseprintf("Entered strings are not equal.\n");
7/28/2019 C Progms List
11/32
return 0;
}
int compare_string(char *first, char *second){while(*first==*second){
if ( *first == '\0' || *second == '\0' )break;
first++;second++;
}if( *first == '\0' && *second == '\0' )
return 0;else
return -1;}
21.Write a C program to find the string length ?
Ans: #include
#include
int string_length(char s[]);
main()
{
char s[50];
int l;
printf("Enter the string\n");
scanf("%s\n",s);
l=strlen(s);
printf("Length of string = %d\n",l);
}
int string_length(char s[])
{
int i;
i=0;
while(s[i] != '\0')
++i;
return i;
}
22.By using STRCAT, STRCOMP and STRCPY. Give some example C programs related to it. ?
23.Write a C program to arrange the characters of a string in alphabetical order?
Ans:
#include
#include
#include
int main (void)
7/28/2019 C Progms List
12/32
{
char string[128], temp;
int n, i, j;
printf("\nEnter string: ");
gets(string);
n = strlen(string);
for (i=0; i
7/28/2019 C Progms List
13/32
char rev[50];
char *sptr = str;
char *rptr = rev;
int i=-1;
printf("Enter any string : ");
scanf("%s",str);
while(*sptr){
sptr++;
i++;
}
while(i>=0){
sptr--;
*rptr = *sptr;
rptr++;--i;
}
*rptr='\0';
printf("Reverse of string is : %s",rev);
return 0;
}
25.Write a C program to reverse the words of the string ?
Ans:
#include
int string_length(char*);void reverse(char*);
main(){
char string[100];
printf("Enter a string\n");gets(string);
reverse(string);
printf("Reverse of entered string is \"%s\".\n", string);
7/28/2019 C Progms List
14/32
return 0;
}
void reverse(char *string){
int length, c;char *begin, *end, temp;
length = string_length(string);
begin = string;end = string;
for ( c = 0 ; c < ( length - 1 ) ; c++ )end++;
for ( c = 0 ; c < length/2 ; c++ ){
temp = *end;
*end = *begin;*begin = temp;
begin++;end--;
}}
int string_length(char *pointer){
int c = 0;
while( *(pointer+c) != '\0' )c++;
return c;}
C program to reverse a string using recursion#include#include
void reverse(char*,int,int);
main(){
char a[100];
gets(a);
reverse(a, 0, strlen(a)-1);
printf("%s\n",a);
7/28/2019 C Progms List
15/32
return 0;
}
void reverse(char *x, int beg, int end){
char a, b, c;
if ( beg >= end )return;
c = *(x+beg);*(x+beg) = *(x+end);*(x+end) = c;
reverse(x, ++beg, --end);}
========2=====with out string==================
/*c program for reverse all words in string*/#include
#include
#include
int main()
{
char str[100];
int i,temp;
printf("Enter any string : ");
gets(str);
for(i=0; str[i]!=NULL; i++){
if(str[i+1]==' ' || str[i+1]==NULL)
{
for(temp=i; temp>=0 && str[temp]!=' '; temp--)
printf("%c", str[temp]);
}
printf(" ");
}
getch();
return 0;}
26.Write a C program to give the occurrence of a character in a string ?
Ans: In C
const char* str; // the string we want to search
7/28/2019 C Progms List
16/32
const char c; // the character we want to search for
int num = 0; // the number of times c is in str
// loop vars
int i;
const int end = strlen(str);
for(i = 0; i < end; ++i) {
if( str[i] == c ) {
++num;
}
}
27.Write a C program to addition of matrix ?
Ans:#include
int main(){
int a[3][3],b[3][3],c[3][3],i,j;
printf("Enter the First matrix->");
for(i=0;i
7/28/2019 C Progms List
17/32
printf("\n");
for(j=0;j
7/28/2019 C Progms List
18/32
getmatrix(x);
getmatrix(y);
addition(x,y,z);
printf("\n - : Matrix 1: - \n");
display(x);
printf("\n - : Matrix 2: - \n");
display(y);
printf("\n - : Matrix Addition (Result): - \n");
display(z);
}
void getmatrix(int t[][3]){
int i,j;
for(i=0;i
7/28/2019 C Progms List
19/32
display(z);
}
void multiplication(int p[][3],int q[3][3],int r[3][3])
{
int i,j,k;
for(i=0;i
7/28/2019 C Progms List
20/32
int i,j;
printf("\n\n");
for(i=0;i
7/28/2019 C Progms List
21/32
{
m=0;
n=0;
for(i=0;i
7/28/2019 C Progms List
22/32
for(j=i+1;j
7/28/2019 C Progms List
23/32
printf("Transpose of the Matrix :\n\n");
for(i=0;i
7/28/2019 C Progms List
24/32
9. Write a c program to print the string from given character.
12. String concatenation in c without using strcat
Arithmetic operation with pointer in c programming
Rule 1:Addition arithmetic with pointersAddress + Number= Address
Address - Number= Address
Address++ = Address
Address-- = Address
++Address = Address
--Address = Address
If we will add or subtract a number from an address result
will also be an address.New address will be:
(1)What will be output of following c program?
#include
intmain(){
int*ptr=(int*)1000;ptr=ptr+1;
printf(" %u",ptr);
return 0;
}
Output: 1002
http://cquestionbank.blogspot.com/2010/06/write-c-program-to-print-string-from.htmlhttp://cquestionbank.blogspot.com/2010/06/write-c-program-to-print-string-from.htmlhttp://cquestionbank.blogspot.com/2011/09/string-concatenation-in-c-without-using.htmlhttp://cquestionbank.blogspot.com/2011/09/string-concatenation-in-c-without-using.htmlhttp://cquestionbank.blogspot.com/2011/09/string-concatenation-in-c-without-using.htmlhttp://cquestionbank.blogspot.com/2010/06/write-c-program-to-print-string-from.html7/28/2019 C Progms List
25/32
(2)What will be output of following c program?
#include
intmain(){
double*p=(double*)1000;
p=p+3;
printf(" %u",p);
return 0;
}
Output: 1024
(3)What will be output of following c program?
#include
intmain(){
floatarray[5]={1.1f,2.2f,3.3f};
float(*ptr)[5];
ptr=&array;
printf("%u",ptr);
ptr=ptr+1;
printf(" %u",ptr);
return 0;
}
Output: 1000 1020
(4)What will be output of following c program?
#include
typedefstructabc{
intfar*a;
doubleb;
unsignedcharc;
}ABC;
intmain(){ABC*ptr=(ABC*)1000;
ptr=ptr+2;
printf(" %u",ptr);
return 0;
}
7/28/2019 C Progms List
26/32
Output: 1026
(5)What will be output of following c program?
#include
typedefunionabc{
charnear*a;
longdoubled;
unsignedinti;
}ABC;
intmain(){
ABC*ptr=(ABC*)1000;
ptr=ptr-4;
printf(" %u",ptr);
return 0;
}
Output: 960
(6)What will be output of following c program?
#include
float* display(int,int);
intmax=5;
intmain(){
float*(*ptr)(int,int);
ptr=display;
(*ptr)(2,2);
printf("%u",ptr);
ptr=ptr+1;
printf(" %u",ptr);
return 0;
}
float* display(intx,inty){
floatf;
f=x+y+max;
return&f;
}
7/28/2019 C Progms List
27/32
Output: Compiler error
Rule 2: Difference arithmetic with pointers
Address - Address=Number
If you will subtract two pointers result will be a number
but number will not simple mathematical subtraction of two
addresses but it follow following rule:
If two pointers are of same type then:
Consider following example:
#include
intmain(){
int*p=(int*)1000;
int*temp;
temp=p;
p=p+2;
printf("%u %u\n",temp,p);
printf("difference= %d",p-temp);
return 0;
}
Output: 1000 1004 Difference= 2
Explanation:
Here two pointer p and temp are of same type and both are
pointing tointdata type varaible.p-temp = (1004-1000)/sizeof(int) =4/2 =2
(1)What will be output of following c program?
#include
intmain(){
float*p=(float*)1000;
7/28/2019 C Progms List
28/32
float*q=(float*)2000;
printf("Difference= %d",q-p);
return 0;
}
Output:Difference= 250
Explanation:
q-p=(2000-100)/sizeof(float) =1000/4 =250
(2)What will be output of following c program?
#include
structabc{
signedcharc;
shortinti;
longdoublel;
};
intmain(){
structabc *p,*q;
p=(structabc *)1000;
q=(structabc *)2000;
printf("Difference= %d",q-p);
return 0;
}
Output: Difference= 76
Explanation: q-p=(2000-1000)/sizeof(structabc)=
1000/(1+2+10)=1000/13 =76
//===================================================
(3)What will be output of following c program?
#include
typedefunionxxx{
charfar * c;
constvolatilei;
longintl;
}XXX;
intmain(){
XXX *p,*q;
7/28/2019 C Progms List
29/32
p=(XXX *)1000;
q=(XXX *)2000;
printf("Difference= %d",q-p);
return 0;
}
Output: Difference= 250
Explanation:
q-p=(2000-1000)/max(4,2,4) =1000/4 =250
(4)What will be output of following c program?
#include
intmain(){
constvolatilearray[4]={0};
constvolatile(*p)[4]=&array;
constvolatile(*q)[4]=&array;
p++;
q++;
printf("%u %u\n",p,q);
printf("Difference= %d",q-p);
return 0;
}
Output:1000 1016 (assume)Difference= 2
Explanation:
q-p=(1016-1000)/sizeof(constvolatile) = 16/ (2*4) =2
Rule 3:Illegal arithmetic with pointers
Address + Address=Illegal
Address * Address=Illegal
Address / Address=IllegalAddress % Address=Illegal
What will be output of following c program?
#include
intmain(){
inti=5;
7/28/2019 C Progms List
30/32
int*p=&i;
int*q=(int*)2;
printf("%d",p+q);
return 0;
}
Output:Compiler error
//========================================================
Rule 4:We can use relation operator and condition operator
between two pointers.
a. If two pointers are near pointer it will compare only
its offset address.
What will be output of following c program?
#include
intmain(){
intnear*p=(intnear*)0x0A0005555;
intnear*q=(intnear*)0x0A2115555;
if(p==q)
printf("Equql");
else
printf("Not equal");
return 0;}
Output:Equal
//=========================================================
b. If two pointers are far pointer it will compare both
offset and segment address.
What will be output of following c program?#include
intmain(){
intfar*p=(intfar*)0x0A0005555;
intfar*q=(intfar*)0x0A2115555;
if(p==q)
printf("Equql");
7/28/2019 C Progms List
31/32
else
printf("Not equal");
return 0;
}
Output:Not equal
//=========================================================
c. If two pointers are huge pointer it will first normalize
into the 20 bit actual physical address and compare to its
physical address.
What will be output of following c program?
#include
intmain(){
inthuge*p=(inthuge*)0x0A0005555;
inthuge*q=(inthuge*)0x0A2113445;
if(p==q)
printf("Equql");
else
printf("Not equal");
return 0;
}
Output:Equal
Rule 5:Bit wise arithmetic with pointers
We can perform bit wise operation between two pointers like
Address & Address=Illegal
Address | Address=Illegal
Address ^ Address=Illegal
~Address=Illegal
What will be output of following c program?
#include
intmain()
{
inti=5,j=10;
int*p=&i;
7/28/2019 C Progms List
32/32
int*q=&j;
printf("%d",p|q);
return 0;
}
Output:Compiler error
//========================================================
Rule 6:We can find size of a pointer usingsizeofoperator.
What will be output of following c program?
#include
intmain(){
intnear*far*huge* p;
printf("%d",sizeof(p));
printf(" %d",sizeof(*p));
printf(" %d",sizeof(**p));
return 0;
}
Output:4 4 2
//=====================================================================