Write An Algorithm and Draw Corresponding Flowchart To Calculate The Factorial of A Given Number
Write An Algorithm and Draw Corresponding Flowchart To Calculate The Factorial of A Given Number
Write An Algorithm and Draw Corresponding Flowchart To Calculate The Factorial of A Given Number
#include<stdio.h>
void printFibonacci(int);
int main(){
int k,n;
long int i=0,j=1,f;
return 0;
}
if(n>0){
sum = first + second;
first = second;
second = sum;
printf("%ld ",sum);
printFibonacci(n-1);
}
}
Using file handling create a file insert some character and count down
// C Program to count
// the Number of Characters in a Text File
#include <stdio.h>
#define MAX_FILE_NAME 100
int main()
{
FILE* fp;
char filename[MAX_FILE_NAME];
return 0;
}
using pointers concept reverse a given string
#include <stdio.h>
#include <string.h>
// Function to reverse the string
// using pointers
void reverseString(char* str)
{
int l, i;
char *begin_ptr, *end_ptr, ch;
// Get the length of the string
l = strlen(str);
// Set the begin_ptr and end_ptr
// initially to start of string
begin_ptr = str;
end_ptr = str;
// Move the end_ptr to the last character
for (i = 0; i < l - 1; i++)
end_ptr++;
// Swap the char from start and end
// index using begin_ptr and end_ptr
for (i = 0; i < l / 2; i++) {
// swap character
ch = *end_ptr;
*end_ptr = *begin_ptr;
*begin_ptr = ch;
return 0;
write programe to find string length without using strlen function
/* C Program to find the length of a String without
* using any standard library function
*/
#include <stdio.h>
int main()
{
/* Here we are taking a char array of size
* 100 which means this array can hold a string
* of 100 chars. You can change this as per requirement
*/
char str[100],i;
printf("Enter a string: \n");
scanf("%s",str);
return 0;
}
write a programe using c to calculate the net salary if the basic,TA,DA ALLOWANCEs and deductions are
given using structures concept
#include
#include
/* structure to store employee salary details */
struct employee {
int empId;
char name[32];
int basic, hra, da, ma;
int pf, insurance;
float gross, net;
};
printf(“\nDeductions: \n”);
printf(“Provident fund: %d\n”, e1.pf);
printf(“Insurance: %d\n”, e1.insurance);
printf(“\nNet Salary: %.2f Rupees\n\n”, e1.net);
return;
}
int main() {
int i, ch, num, flag, empID;
struct employee *e1;
/* get the number of employees from the user */
printf(“Enter the number of employees:”);
scanf(“%d”, &num);
/* dynamically allocate memory to store employee salary details */
e1 = (struct employee *)malloc(sizeof(struct employee) * num);
/* get the employee salary details from the customer */
printf(“Enter your input for every employee:\n”);
for (i = 0; i < num; i++) {
printf(“Employee ID:”);
scanf(“%d”, &(e1[i].empId));
getchar();
printf(“Employee Name:”);
fgets(e1[i].name, 32, stdin);
e1[i].name[strlen(e1[i].name) – 1] = ‘\0’;
printf(“Basic Salary, HRA:”);
scanf(“%d%d”, &(e1[i].basic), &(e1[i].hra));
printf(“DA, Medical Allowance:”);
what is the use of continue statement explain with example
The continue statement in C programming works somewhat like the break statement.
Instead of forcing termination, it forces the next iteration of the loop to take place,
skipping any code in between.
For the for loop, continue statement causes the conditional test and increment
portions of the loop to execute. For
the while and do...while loops, continue statement causes the program control to
pass to the conditional tests.
#include <stdio.h>
int main () {
/* do loop execution */
do {
if( a == 15) {
/* skip the iteration */
a = a + 1;
continue;
}
} while( a < 20 );
return 0;
}
Explain any four string functions with example of each
strlen()
strlen() function returns the length of the string. strlen() function returns integer value.
Example:
char *str = “Learn C Online”;
int strLength;
strLength = strlen(str); //strLength contains the length of the string i.e. 14
strcpy()
strcpy() function is used to copy one string to another. The Destination_String
should be a variable and Source_String can either be a string constant or a
variable.
Syntax:
strcpy(Destination_String,Source_String);
Example:
char *Destination_String;
strcpy(Destination_String,Source_String);
printf(“%s”, Destination_String);
strncpy()
strncpy() is used to copy only the left most n characters from source to
destination. The Destination_String should be a variable and Source_String
can either be a string constant or a variable.
Syntax:
strncpy(Destination_String, Source_String,no_of_characters);
strcat()
int main()
{
void print(); //function declaration
print(); //function call
cout<<"\no parameter and no return type \n";
print();
return 0;
}
int main()
{
int num1, num2;
printf("\nEnter The First Number:\t");
scanf("%d", &num1);
printf("\nEnter The Second Number:\t");
scanf("%d", &num2);
swap(num1, num2);
printf("\nOld Values\n");
printf("\nFirst Number = %d\nSecond Number = %d\n", num1, num2);
printf("\n");
return 0;
}
int main()
{
int A[SIZE][SIZE]; // Matrix 1
int B[SIZE][SIZE]; // Matrix 2
int C[SIZE][SIZE]; // Resultant matrix
int row, col, i, sum;
/* Input elements in first matrix from user */
printf("Enter elements in matrix A of size %dx%d: \n", SIZE, SIZE);
for(row=0; row<SIZE; row++)
{
for(col=0; col<SIZE; col++)
{
scanf("%d", &A[row][col]);
}
}
/* Input elements in second matrix from user */
printf("\nEnter elements in matrix B of size %dx%d: \n", SIZE, SIZE);
for(row=0; row<SIZE; row++)
{
for(col=0; col<SIZE; col++)
{
scanf("%d", &B[row][col]);
}
}
/*
* Multiply both matrices A*B
*/
for(row=0; row<SIZE; row++)
{
for(col=0; col<SIZE; col++)
{
sum = 0;
/*
* Multiply row of first matrix to column of second matrix
* and store sum of product of elements in sum.
*/
for(i=0; i<SIZE; i++)
{
sum += A[row][i] * B[i][col];
}
C[row][col] = sum;
}
}
define a macro to find maximum among of 3 given numbers using #ifdef #else
#ifdef
The syntax is as follows:
#ifdef IDENTIFIER_NAME
{
statements;
}
This will accept a name as an argument, and returns true if the name has a current definition. The
name may be defined using a # define, the -d option of the compiler, or certain names which are
automatically defined by the UNIX environment. If the identifier is defined then the statements
below #ifdef will be executed
#else
The syntax is as follows:
#else
{
statements;
}
#else is optional and ends the block started with #ifdef. It is used to create a 2 way optional
selection. If the identifier is not defined then the statements below #else will be executed.
#endif
Ends the block started by #ifdef or #else.
Where the #ifdef is true, statements between it and a following #else or #endif are included in the
program. Where it is false, and there is a following #else, statements between the #else and the
following #endif are included. Let us look into the illustrative example given below to get an idea.
Call by value
In call by value mechanism, the called function creates a new set of variables in stack
and copies the values of the arguments into them.
Example: Program showing the Call by Value mechanism.
To Declare any variable in C language you need to follow rules and regulation of C Language,
• Except underscore (_) no other special symbol are allowed in the middle of the
variable declaration (not allowed -> roll-no, allowed -> roll_no).
• Every variable name always should exist in the left hand side of assignment operator
(invalid -> 10=a; valid -> a=10;).
• No keyword should access variable name (int for <- invalid because for is keyword).
what recursion program with suitable example
A process in which a function calls itself directly or indirectly is called Recursion in C and the
corresponding function is called a Recursive function.
1. Direct Recursion:If function definition contains, the function call itself then it is direct
recursion.Example:
Fun( )
Fun( );
}2. Indirect Recursion:If function fun1() calls another function fun2() and function fun2() calls
function fun1(), then it is known as indirect recursion.
Example:
Fun2( )
{
…..
Fun1( );
}
Fun1( )
{
…..
Fun2( );
//Learnprogramo
#include<stdio.h>
int fact(int);
int main()
{
int num;
printf("Enter the number whose factorial is to be find :");
scanf("%d" ,&num);
if(num<0)
{
printf("ERROR. Factorial is not defined for negative integers");
}
printf("Factorial of %d is %d", num, fact(num));
return 0;
}
int fact(int num)
{
if(num==0) //base condition
{
return 1;
}
else
{
return(num*fact(num-1)); //recursive call
}
design an algorithm and draw a corresponding flow chart and write a cprograme to devide
two numbers
Step 1: Input String
*C=*C+32
}
Step 4: Print converted Lower case String
Step 5: Stop
FlowChart:
write a programe to calculate the smallest diviserof a number using break statement
#include <stdio.h>
main( )
int div,num,i;
scanf(“%d”,&num);
for (i=2;i<=num;++i)
if ((num % i) == 0)
break;
OUTPUT
Enter any number:
9
Smallest divisor for number 9 is 3
write a program in c to swap the values of two variables using pointer concept
#include<stdio.h>
int main() {
int num1, num2;
swap(&num1, &num2);
return (0);
}
Enter the first number : 12
Enter the Second number : 22
First number : 22
Second number : 12
write a programe that initialises 3 names in an array of string and display them
#include <stdio.h>
int main(){
#include<stdio.h>
int main()
{
int elements[30], limit, count;
printf("Enter Limit for Array:\t");
scanf("%d", &limit);
printf("\nEnter %d Elements in the Array\n", limit);
for(count = 0; count < limit; count++)
{
scanf("%d", &elements[count]);
}
quick_sort(elements, 0, limit - 1);
printf("\nSorted List:\n");
for(count = 0; count < limit; count++)
{
printf("%d\t", elements[count]);
}
printf("\n");
return 0;
}
#include<stdio.h>
void main()
{
printf("1");
printf("2");
printf("3");
printf("4");
printf("5");
printf("6");
printf("7");
printf("8");
printf("9");
printf("10");
}
int main()
{
//declare variable
int count,t,r;
//Read value of t
printf("Enter number: ");
scanf("%d",&t);
count=1;
start:
if(count<=10)
{
//Formula of table
r=t*count;
printf("%d*%d=%d\n",t,count,r);
count++;
goto start;
}
return 0;
}
explain function prototype with an example of each
Function prototype is the important feature of C programming which was borrowed from
C++. Early versions of C programming did not use function prototype.
Function prototype in C is used by the compiler to ensure whether the function call
matches the return type and the correct number of arguments or parameters with its
data type of the called function.
In the absence of the function prototype, a coder might call function improperly without
the compiler detecting errors that may lead to fatal execution-time errors that are difficult
to detect.
#include <stdio.h>
#include <string.h>
int main()
int result, i;
gets(Str1);
gets(Str2);
else
write a programe to test whether the given string is paindrome or not
#include <stdio.h>
#include <string.h>
int main()
char str[100];
flag = 0;
gets(str);
len = strlen(str);
flag = 1;
break;
if(flag == 0)
{
write a macro to demonstrate#define#if#else preprocessor commands
#include <stdio.h>
#define CHOICE 100
int my_int = 0;
#if (CHOICE == 100)
void set_my_int()
{ my_int = 35; }
#else
void set_my_int()
{
my_int = 27;
}
#endif
main ()
{
set_my_int();
clrscr();
printf(“%d\n”, my_int);
getch();
}
write a c programe using fread fwrite to create a file of records and then read and print the same file
#include<stdio.h>
#include<conio.h>
#include<process.h>
} stud;
void sort(int n)
{
FILE *fp_index, *fp_record;
int i, j, r1, r2, r3;
stud s1, s2, s3;
.txt”, “r+b”);
what is syntax error give an example of syntax error c program
Syntax errors: Errors that occur when you violate the rules of writing C/C++ syntax are
known as syntax errors. This compiler error indicates something that must be fixed before
the code can be compiled. All these errors are detected by compiler and thus are known as
compile-time errors.
#include <stdio.h>
#include <string.h>
int main()
int i, j;
gets(Str1);
gets(Str2);
Str1[i] = Str2[j];
Str1[i] = '\0';
The getch() function is very useful if you want to read a character input from
the keyboard.
While this is not a part of the C standard, this is still a POSIX C function. So,
we can still use this function from Windows / Linux / Mac.
This function takes in a single character from the standard input (stdin), and
returns an integer.
This is there as part of the <conio.h> header file, so you must include it in
your program.
#include <conio.h>
int getch();
Strcmp
In the C Programming Language, the strcmp function returns a negative, zero, or
positive integer depending on whether the object pointed to by s1 is less than, equal to,
or greater than the object pointed to by s2.
Syntax
The syntax for the strcmp function in the C Language is:
getchar
• The C library function int getchar (void) gets a character (an unsigned char) from
stdin. This is equivalent to getc with stdin as its argument.
• Declaration. Following is the declaration for getchar () function.
• Parameters
• Return Value. This function returns the character read as an unsigned char cast to an
int or EOF on end of file or error.
Gets()
C gets() function. The gets() function enables the user to enter some
characters followed by the enter key. All the characters entered by the user
get stored in a character array. The null character is added to the array to
make it a string. The gets() allows the user to enter the space-separated
strings. It returns the string entered by the user.
Points to remember:
• These are global and can be accessed by any function within its scope.
• A declaration is required if the external variable definition comes after the function definition.
• External variables can be assigned initial values as a part of variable definitions, but the values
must be constants rather than expressions.
main ( )
gv = 10;
function1( );
void function1 ( )
gv = 15: }
OUTPUT