#### Search results for scanf

##### Example:[1]

Write a c program function to compute (a+b)^2 where the value of a and b will be provided by the user.

#include <stdio.h>
int get_squreof_no(int ,int );
void main()
{
int square,int1,int2;
printf("Enter the value of int1:");
scanf("%d",&int1);
printf("Enter the value of int2:");
scanf("%d",&int2);
square = get_squreof_no(int1,int2);
printf("The square result is:%d\n", square);
}
int get_squreof_no(int a, int b)
{
int r;
r = (a*a+b*b+2*a*b);
return r;
}

##### Example:[2]

Write a c program function to calculate the sum of two integers where the two integers will be provided by the user.

#include <stdio.h>
int Sum(int a,int b);
void main()
{
int int1,int2,sum;
printf("Enter first integer\n");
scanf("%d",&int1);
printf("Enter second integer\n");
scanf("%d",&int2);
sum = Sum(int1,int2);
printf("The sum is:%d\n",sum);
}
int Sum(int a,int b)
{
return a + b;
}

##### Example:[3]

Write a c program function to create simple calculator using switch case.

#include <stdio.h>
int main()
{
char op;
float num1, num2, result=0.0f;
printf("Enter [number 1] [operator] [number 2]\n");
scanf("%f %c %f", &num1, &op, &num2);
switch(op)
{
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
result = num1 / num2;
break;
default:
printf("Unsupported Operator");
}
printf("%.2f %c %.2f = %.2f\n", num1, op, num2, result);
return 0;
}

##### Example:[4]

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:[5]

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:[6]

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:[7]

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:[8]

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