#### Search results for if

##### Example:[1]
``````
Write a c program function to calculate the circumference of a circle if the radius of the circle is passed to it.

``````
``````
#include <stdio.h>
#define PI 3.14
float Circum(int );
int main()
{
float r;
printf("The area of the circle is %f\n",r);
return 0;
}
float Circum(int r)
{
return (2 * PI * r);
}

``````
##### Example:[2]
``````
Write a c program function to calculate the area of a circle if the radius of the circle is passed to it.
``````
``````
#include <stdio.h>
#define PI 3.14
float Area(int );
int main()
{
float r;
printf("The area of the circle is %f\n",r);
return 0;
}
float Area(int r)
{
return (PI * r * r);
}
``````
##### Example:[3]
``````
Write C function to calculate the factorial of a number and illustrate the use of recursion.
``````
``````
#include <stdio.h>
#include <stdlib.h>
int fact(int );
int main() {
int num = 5;
int res;
res = fact(num);
printf("Factorial is %d\n",res);
return 0;
}
int fact(int x)
{
if(x > 1)
{
return x * fact(x-1);
}else
{
return 1;
}
}

``````
##### Example:[4]
``````
Write a C Program macro to check if a character is alphabetic or not.
``````
``````
#include <stdio.h>
#define ALPHA(CH) ((CH >= 'a' && CH <= 'z') || (CH >= 'A' && CH <= 'Z')?1:0)
int alp(int );
int main()
{
int al;
char a = 'a';
al = alp(a);
if(al == 1)
{
printf("the given char is alpha");
}else
{
printf("the given char is not alpha");
}
return 0;
}
int alp(int a)
{
return ALPHA(a);
}
``````
##### Example:[5]
``````
Write a C program to check whether year is leap year or not using conditional/ternary operator.
``````
``````
#include <stdio.h>
#define YEAR(X) ((X%4==0)||(X%400==0) ? 1:0)
int a = 2013;
int Leap_Year(int a);
int main()
{
int num1;
num1 = Leap_Year(a);
if (num1 == 1)
{
printf("the given year is a leap year");
}
else
{
printf("the given year is not a leap year");
}
return 0;
}
int Leap_Year(int a)
{
return YEAR(a);
}
``````
##### Example:[6]
``````
Write a c program function to find the maximum of three given integers.
``````
``````
#include <stdio.h>
unsigned int max;
unsigned int maxi(unsigned int num1,unsigned int num2,unsigned int num3);
int main()
{
unsigned int max_result;
max_result = maxi(12,24,34);
printf("The max result is %d\n",max_result);
return 0;
}
unsigned int maxi(unsigned int num1,unsigned int num2,unsigned int num3)
{
max = num1;
if(num2 > max)
{
max = num2;
}
if(num3 > max)
{
max = num3;
}
return max;
}
``````
##### Example:[7]
``````
Write a C program to find longest sequence of elements in an array.
``````
``````
#include <stdio.h>
int result = 0,f_result = 0;
unsigned int find_long(int a[]);
int main()
{
int aa[] = {0,2,3,8,9,10,11,12,18,19,20,21,22,23,24};
unsigned int lon;
lon = find_long(aa);
printf("The Long sequence is %d\n",lon);
return 0;
}
unsigned int find_long(int a[])
{
int len = 15;
unsigned int i;
for(i = 1;i < len;i++)
{
if((a[i] - a[i-1]) == 1)
{
result++;
if(result >= f_result)
{
f_result = result;
printf("f_result is %d\n",f_result);
}
}else
{
result = 0;
printf("result is %d\n",result);
}
}
return f_result+1;
}

``````
##### Example:[8]
``````
Write a program to find the number of times that a given word(i.e. a short string) occurs in a sentence (i.e. a long string!).
``````
``````
#include <stdio.h>
#include <string.h>
unsigned int search_word(char *,char *);
int main()
{
char *s_str = "the";
char *l_str = "the word searching for from these sentence is the.";
unsigned int result;
result = search_word(s_str,l_str);
printf("Small string is found %d times in long string\n",result);
return 0;
}
unsigned int search_word(char *s1,char *s2)
{
int times = 0;
int i = 0,j = 0,k = 0;
int size = strlen(s1);
printf("Size of small string \" %s \" is: %d\n",s1,size);
while(s2[j] != '\0')
{
if(j >= (size - 1))
{
if(s2[j] == s1[size-1] && s2[j-1] == s1[size-2] && s2[j-2] == s1[size-3])
{
times = times + 1;
}
}
j++;
}
return times;
}
``````
##### Example:[9]
``````
Write a c program function to accept a string and return the character written two times continuously in the string.
``````
``````
#include <stdio.h>
char res_char(char *);
int main()
{
char *str = "Hello how are you";
char result;
result = res_char(str);
printf("Got result %c\n",result);
return 0;
}
char res_char(char *f)
{
char res;
while(*f != '\0')
{
if(*f == *(f + 1))
{
res = *f;
}
f++;
}
return res;
}
``````
##### Example:[10]
``````
Write a C program function to count frequency of each character in a string and return the maximum frequency.
``````
``````
#include <stdio.h>
int freq_func(char *);
int main()
{
char *s = "Here is the string that needs to be verified for the frequency of each character";
int max_freq;
max_freq = freq_func(s);
printf("Max Frequency is:%d\n",max_freq);
return 0;
}
int freq_func(char *r)
{
int i =0,j = 0,res1 = 0,res2 = 0;
while(r[j] != '\0')
{
while(r[i] != '\0')
{
if(r[j] == r[i])
{
res1 = res1 + 1;
}
i++;
}
if(res1 > res2)
{
res2 = res1;
}
i = 0;
res1 = 0;
j++;
}
return res2;
}
``````
##### Example:[11]
``````
Write a C program function to take a string as its input and return the first word of the string passed to it.

``````
``````
#include <stdio.h>
void getfword(char *s, char f_word[]);
int main() {
char str[] = "Hello how are you";
char res[20];
getfword(str,res);
printf("Result is:%s\n", res);
}
void getfword(char *s, char f_word[])
{
int i = 0;
while(s[i] != '\0')
{
if(s[i] == ' ')
{
break;
}else
{
f_word[i] = s[i];
f_word[i + 1] = '\0';
i++;
}
}
}

``````
##### Example:[12]
``````
Write a c program function to calculate the number of words present in a string passed to it.
``````
``````
#include <stdio.h>
int NumOfWords(char *s);
int res = 1;
int main() {
char str[] = "Hello how are you";
res = NumOfWords(str);
printf("Result is:%d\n", res);
}
int NumOfWords(char *s)
{
int i = 0;
while(s[i] != '\0')
{
if(s[i] == ' ')
{
res = res + 1;
}
i++;
}
return res;
}

``````
##### Example:[13]
``````
Write a c program to accept a string as input and return the number of words present in it using pointers.

``````
``````
#include <stdio.h>
void NumOfWords(char *s, int *);
int main() {
char str[] = "Hello how are you";
int res = 1;
NumOfWords(str,&res);
printf("Result is:%d\n", res);
}
void NumOfWords(char *s, int *r)
{
int i = 0;
while(s[i] != '\0')
{
if(s[i] == ' ')
{
*r = *r + 1;
}
i++;
}
}

``````
##### Example:[14]
``````
Write a program to swap the 4bytes of an integer. If input is provided as 0x12345678 output is generated as 0x78563412.
``````
``````
#include <stdio.h>
union out
{
struct
{
unsigned char ll;
unsigned char lh;
unsigned char hl;
unsigned char hh;
}in;
unsigned int b;
};
int main()
{
union out aa;
aa.b = 0x12345678;
printf("Before swapping:%x\n", aa.b);
unsigned char ll = aa.in.ll;
unsigned char lh = aa.in.lh;
aa.in.ll = aa.in.hh;
aa.in.hh = ll;
aa.in.lh = aa.in.hl;
aa.in.hl = lh;
printf("After swapping:%x\n", aa.b);
return 0;
}
``````
##### Example:[15]
``````
Write a function to determine whether the machine is Little Endian or Big Endian.
``````
``````
#include <stdio.h>
unsigned int EndianCheck();
union endian
{
unsigned int a;
char b;
};
int main()
{
unsigned int res;
res = EndianCheck();
if(res == 0)
{
printf("Little endian\n");
}else
{
printf("Big endian\n");
}
return 0;
}
unsigned int EndianCheck()
{
union endian test;
test.a = 1;
if(test.b == 1)
{
return 0;
}else
{
return 1;
}
}
``````
##### Example:[16]
``````
Write a C program function to enter different book information(pages, author, search id) in a school library.
``````
``````
#include <stdio.h>
#define NO_BOOKS 3
struct Book
{
int pages;
char author[10];
int search_id;
};
void EnterBooks(struct Book *);
int main()
{
struct Book b[NO_BOOKS];
EnterBooks(b);
return 0;
}
void EnterBooks(struct Book *a)
{
int i;
for(i = 0;i < NO_BOOKS;i++)
{
printf("Enter %d book pages\n", i+1);
scanf("%d",&a->pages);
printf("Enter %d book author\n", i+1);
scanf("%s",&a->author);
printf("Enter %d search id\n", i+1);
scanf("%d",&a->search_id);
a++;
}
}
``````
##### Example:[17]
``````
Write a C program function to enter different book information(pages, author, search id) in a school library and display the existing books using structure pointers.

``````
``````
#include <stdio.h>
#include <stdlib.h>
#define NO_BOOKS 3
struct Book
{
int pages;
char author[10];
int search_id;
};
void EnterBooks(struct Book *);
void DisBooks(struct Book *);
int main()
{
struct Book *b;
b = (struct Book *)malloc(sizeof(struct Book)*NO_BOOKS);
if(b == NULL)
{
printf("Req memory allocation failed\n");
}else
{
EnterBooks(b);
DisBooks(b);
}
free(b);
return 0;
}
void EnterBooks(struct Book *a)
{
int i;
for(i = 0;i < NO_BOOKS;i++)
{
printf("Enter %d book pages\n", i+1);
scanf("%d",&a->pages);
printf("Enter %d book author\n", i+1);
scanf("%s",&a->author);
printf("Enter %d search id\n", i+1);
scanf("%d",&a->search_id);
a++;
}
}
void DisBooks(struct Book *a)
{
int i;
for(i = 0;i < NO_BOOKS;i++)
{
printf("Book %d pages %d\n",i+1,a->pages);
printf("Book %d author %s\n",i+1,a->author);
printf("Book %d book search id %d\n",i+1,a->search_id);
a++;
}
}

``````
##### Example:[18]
``````
Write a C program function to enter different book information(pages, author, search id) in a school library and display the existing books using structure arrays.

``````
``````
#include <stdio.h>
#define NO_BOOKS 3
struct Book
{
int pages;
char author[10];
int search_id;
};
void EnterBooks(struct Book *);
void DisplayBooks(struct Book *);
int main()
{
struct Book b[NO_BOOKS];
EnterBooks(b);
DisplayBooks(b);
return 0;
}
void EnterBooks(struct Book *a)
{
int i;
for(i = 0;i < NO_BOOKS;i++)
{
printf("Enter %d book pages\n", i+1);
scanf("%d",&a->pages);
printf("Enter %d book author\n", i+1);
scanf("%s",&a->author);
printf("Enter %d search id\n", i+1);
scanf("%d",&a->search_id);
a++;
}
}
void DisplayBooks(struct Book a[])
{
int i;
for(i = 0;i < NO_BOOKS;i++)
{
printf("Book %d pages %d\n",i+1,a[i].pages);
printf("Book %d author %s\n",i+1,a[i].author);
printf("Book %d book search id %d\n",i+1,a[i].search_id);
}
}
``````
##### Example:[19]
``````
Write a C program function to enter different book information(pages, author, search id) in a school library and search any book using search id and structure arrays.

``````
``````
#include <stdio.h>
#define NO_BOOKS 3
struct Book
{
int pages;
char author[10];
int search_id;
};
void EnterBooks(struct Book *);
struct Book *SrchBook(struct Book a[]);
int main()
{
struct Book b[NO_BOOKS];
EnterBooks(b);
struct Book *s;
s = SrchBook(b);
printf("Book pages:%d\n", s->pages);
printf("Book author:%s\n", s->author);
printf("Book search_id:%d\n", s->search_id);
return 0;
}
void EnterBooks(struct Book *a)
{
int i;
for(i = 0;i < NO_BOOKS;i++)
{
printf("Enter %d book pages\n", i+1);
scanf("%d",&a->pages);
printf("Enter %d book author\n", i+1);
scanf("%s",&a->author);
printf("Enter %d search id\n", i+1);
scanf("%d",&a->search_id);
a++;
}
}
struct Book *SrchBook(struct Book a[])
{
int i,id;
printf("Enter the book search id\n");
scanf("%d",&id);
for(i = 0;i < NO_BOOKS;i++)
{
if(id == a[i].search_id)
{
return &a[i];
}
}
if(i == NO_BOOKS)
{
printf("Invalid Search_id, try again\n");
return NULL;
}
}
``````
##### Example:[20]
``````
Write a C program function to enter different book information(pages, author, search id) in a school library and search any book using search id and structure pointers.
``````
``````
#include <stdio.h>
#include <stdlib.h>
#define NO_BOOKS 3
struct Book
{
int pages;
char author[10];
int search_id;
};
void EnterBooks(struct Book *);
struct Book *SrchBook(struct Book *);
int main()
{
struct Book *b;
b = (struct Book *)malloc(sizeof(struct Book)*NO_BOOKS);
if(b == NULL)
{
printf("Req memory allocation failed\n");
}else
{
EnterBooks(b);
struct Book *s;
s = SrchBook(b);
printf("Book pages:%d\n", s->pages);
printf("Book author:%s\n", s->author);
printf("Book search_id:%d\n", s->search_id);
}
free(b);
return 0;
}
void EnterBooks(struct Book *a)
{
int i;
for(i = 0;i < NO_BOOKS;i++)
{
printf("Enter %d book pages\n", i+1);
scanf("%d",&a->pages);
printf("Enter %d book author\n", i+1);
scanf("%s",&a->author);
printf("Enter %d search id\n", i+1);
scanf("%d",&a->search_id);
a++;
}
}
struct Book *SrchBook(struct Book *a)
{
int i,id;
printf("Enter the book search id\n");
scanf("%d",&id);
for(i = 0;i < NO_BOOKS;i++)
{
if(id == a->search_id)
{
return &a[i];
}
a++;
}
if(i == NO_BOOKS)
{
printf("Invalid Search_id, try again\n");
return NULL;
}
}
``````
##### Example:[21]
``````
Write a C program to get the status of nth bit of a number.
``````
``````
#include <stdio.h>
unsigned int CheckBit(unsigned int ,unsigned int );
int main()
{
unsigned int r = 11;
r = CheckBit(r,1);
printf("Bit %d status is %d\n", 1, r);
return 0;
}
unsigned int CheckBit(unsigned int a,unsigned int x)
{
if(((a >> x) & 1) == 1)
{
return 1;
}else
{
return 0;
}
}
``````
##### Example:[22]
``````
Write a c program function to display all node data present in a singly linked list.
``````
``````
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node *next;
};
void Traverse();
void main()
{
Traverse();
}
{
struct Node *ptr;
{
ptr = (struct Node *)malloc(sizeof(struct Node ));
if(ptr == NULL)
{
printf("Req memory allocation failed\n");
}else
{
ptr->data = val;
ptr->next = NULL;
}
}else
{
ptr = (struct Node *)malloc(sizeof(struct Node ));
if(ptr == NULL)
{
printf("Req memory allocation failed\n");
}else
{
ptr->data = val;
}
}
}
void Traverse()
{
int n = 1;
while(ptr != NULL)
{
printf("Node %d data %d\n", n,ptr->data);
ptr = ptr->next;
n++;
}
}
``````
##### Example:[23]
``````
Write a c singly linked list program to add a node at the beginning of the list.
``````
``````
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node *next;
};
void main()
{
}
{
struct Node *ptr;
{
ptr = (struct Node *)malloc(sizeof(struct Node ));
if(ptr == NULL)
{
printf("Req memory allocation failed\n");
}else
{
ptr->data = val;
ptr->next = NULL;
}
}else
{
ptr = (struct Node *)malloc(sizeof(struct Node ));
if(ptr == NULL)
{
printf("Req memory allocation failed\n");
}else
{
ptr->data = val;
}
}
}
``````
##### Example:[24]
``````
Write a c program function to add new nodes at the end of a singly linked list.
``````
``````
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node *next;
};
void Traverse();
void main()
{
Traverse();
Traverse();
}
{
{
ptr = (struct Node *)malloc(sizeof(struct Node ));
if(ptr == NULL)
{
printf("Req memory allocation failed\n");
}else
{
ptr->data = val;
ptr->next = NULL;
}
}else
{
ptr = (struct Node *)malloc(sizeof(struct Node ));
if(ptr == NULL)
{
printf("Req memory allocation failed\n");
}else
{
while(aptr != NULL)
{
bptr = aptr;
aptr = aptr->next;
}
ptr->data = val;
bptr->next = ptr;
ptr->next = NULL;
}
}
}
void Traverse()
{
int n = 1;
while(ptr != NULL)
{
printf("Node %d data %d\n", n,ptr->data);
ptr = ptr->next;
n++;
}
}
``````
##### Example:[25]
``````
Write a c program function to add new nodes in the middle of a singly linked list.
``````
``````
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node *next;
};
void Traverse();
void main()
{
Traverse();
}
{
{
ptr = (struct Node *)malloc(sizeof(struct Node ));
if(ptr == NULL)
{
printf("Req memory allocation failed\n");
}else
{
ptr->data = val;
ptr->next = NULL;
}
}else
{
ptr = (struct Node *)malloc(sizeof(struct Node ));
if(ptr == NULL)
{
printf("Req memory allocation failed\n");
}else
{
while(aptr != NULL)
{
bptr = aptr;
aptr = aptr->next;
}
ptr->data = val;
bptr->next = ptr;
ptr->next = NULL;
}
}
}
void Traverse()
{
int n = 1;
while(ptr != NULL)
{
printf("Node %d data %d\n", n,ptr->data);
ptr = ptr->next;
n++;
}
}
{
int count = 0;
{
printf("List is empty\n");
}else
{
while(aptr != NULL)
{
aptr = aptr->next;
count++;
}
printf("%d\n", count);
if(count < pos)
{
printf("We have less nodes than %d\n", pos);
}else
{
aptr = (struct Node *)malloc(sizeof(struct Node ));
if(aptr == NULL)
{
printf("Req memory allocation failed\n");
}else
{
count = 0;
while(count != pos)
{
cptr = bptr;
bptr = bptr->next;
count++;
}
aptr->data = val;
cptr->next = aptr;
aptr->next = bptr;
}
}
}
}
``````