C Program Copy One String to Another using Pointers
The program implements the Copy string to another string. The inner function “stcpy” takes 2 string pointers as arguments. “copystr” pointer type function declared and pointer type char variable declare. On next move the argument written “I am Dinesh Thakur” in the variable char pointer type and array type string variable. The required function used “cpystr”.
In the next move void copystr() and pointer type two variable dest ,src. While condition src!=0 then dstn=src then the string will be copied in dest then return the result as copied string.
Problem Statement:-
This program User asks to Copy String from one To another string with the Help of Pointer.
- Declare pointer type variable.
- Using While Statement.
- Display result on the screen.
This C program is successfully compiled and run on a System. Output is shown below
#include<stdio.h>
void copystr(char*,char*);
void main()
{
char*str1="I am Dinesh Thakur";
char str2[30];
clrscr();
copystr(str2,str1);
printf("\n %s",str2);
getch();
}
void copystr(char *dest,char *src)
{
while(*src!='\0')
*dest++=*src++;
*dest='\0';
return;
}
Output :
I am Dinesh Thakur
C Program Sort a List of Strings using Pointers
In this Program pointer concept is been used with the use of variables of pointer type char and integer type which contain the values. Reorder () also used for sorting the the string as required. In the program string is to be entered after that loop statement works. During loop statement the pointer type variable acquires the memory using malloc() function.
The next move is to sort the string here loop statement again come in use and then printing the string. So on the next step string functions comes into the frame like Strcmp() or Strcpy() for conditionally checks the string. Here while using the string function if condition also comes into the place for checking if the string sorted or not in the last the sorted string is to be displayed as output.
Problem Statement:
Here we have to make a Program to sort the String with using Pointer and string Functions.
- Entering the String.
- Using required functions.
- Using loop Statement.
- And Display The Output on the Screen.
The C program is successfully compiled. The program output is also shown below.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char *x[20];
int i,n=0;
void reorder(int n,char *x[]);
clrscr();
printf("Enter no. of String : ");
scanf("%d",&n);
printf("\n");
for(i=0;i<n;i++)
{
printf("Enter the Strings %d : ",i+1);
x[i]=(char *)malloc(20*sizeof(char));
scanf("%s",x[i]);
}
reorder(n,x);
printf("\nreorder list is : \n");
for(i=0;i<n;i++)
{
printf("%d %s\n",i+1,x[i]);
}
getch();
}
void reorder(int n,char *x[])
{
int i,j;
char t[20];
for(i=0;i<n-1;i++)
for(j=i+1;j<n;j++)
if(strcmp(x[i],x[j])>0)
{
strcpy(t,x[j]);
strcpy(x[j],x[i]);
strcpy(x[i],t);
}
return;
}
C Program Interchange Values of 2 no: using Call By Reference
This is C program where user asks to interchange the two values with each other using Call by reference method. Declaring the required variable for functionality. Then after user declare void swap() function that which will give the reference to other class for interchange. After that user call the function that declare void swap() then using method to interchange the value of variables with the help of pointer concept to declare pointer type variables that are useful while using the call by reference method. In the end the result has to be display on the screen.
Problem statement:
This is C program that asks the user to interchange the value of two variables with use of call by reference Method.
- Declaring variables.
- Make function.
- Calls the function.
- Using Interchange method for value swapping.
- Display result on the screen.
Here is C source code for interchange value of two variables using Call by reference Method. The output of this program shown below.
#include<stdio.h>
void main()
{
int a=40;
int b=70;
void swap(int *x,int *y);
clrscr();
printf("a=%d,b=%d",a,b);
swap(&a,&b);
printf("\nAfter Swaping Result is :\na=%d b=%d ",a,b);
getch();
}
void swap(int *x,int *y)
{
int t;
t=*x;
*x=*y;
*y=t;
}
C Program Find the Maximum Number Between 5 No
In this program user ask to find the Maximum number between the 5 numbers. Here user declares the variables that which store the value. User asks to enter the five numbers for compilation. Here user uses for loop to vary the condition regarding to situation. Here user declare nested loop statement for number and then if condition. If (a[i]>a[j]) if all the condition got vary then the loop statement will collaborate the number that it varies and put on the screen as result it has.
Problem Statement:
This is C program that asks user to find the bigger number from the 5 numbers.
- Declaring the variable.
- Using loop statement.
- Vary control statement.
- Display result on the screen.
Here is C source code for finding the maximum number from five No. The output of this program shown below.
#include<stdio.h>
void main()
{
int a[5],i,j,c;
clrscr();
printf("Enter five no. :");
for(i=0;i<=4;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<=4;i++)
{
for(j=i+1;j<=4;j++)
{
if(a[i]>a[j])
{
c=a[i];
a[i]=a[j];
a[j]=c;
}
}
}
for(i=0;i<=4;i++)
{
printf("%d",a[i]);
}
printf("\n");
getch();
}
C Program String is Palandrom OR Not
#include<stdio.h>
#include<string.h>
main()
{
char a[5],b[5];
clrscr();
printf("Enter the character =");
scanf("%s",&a);
strcpy(b,a);
strrev(a);
if(strcmp(a,b)==0)
{
printf("String is palandrom");
}
else
printf("String is not palandrom");
getch();
}
C Program that Counts Number of Braces
#include <stdio.h>
int main()
{
int c, lbraces=0, rbraces=0, lb='{', rb='}';
while ((c=getchar()) != EOF)
{
putchar(c);
while (c==lb || c==rb)
{
if (c==lb) lbraces++;
if (c==rb) rbraces++;
if (rbraces > lbraces)
{
printf("??");
rbraces--;
}
c=0;
}
}
if (lbraces > rbraces)
printf(" %d missing ",lbraces-rbraces);
for (;lbraces > rbraces; lbraces--)
putchar(rb);
return 0;
}
DECIMAL to BINARY conversion – C program
In this program user ask to convert decimal number into binary number. Binary number has the base of 2. In this program user declare the variable and then the while condition in while condition (n>0). That will manipulated with a[d]=n%2. And then division with 2. Here after the increment operator and for printing out the binary number user uses the loop statement that will generate the binary number on the display.
Problem Statement:
This is c program that asks the user to convert Decimal number into Binary.
- Declaring the variable.
- Using while condition.
- Divide the number with required quotations.
- Using loop statement.
- Display result on the screen.
Here is C source code for calculating the gross salary of employees. The output of this program shown below.
#include<stdio.h>
void main()
{
int n,d=0,j,a[9];
clrscr();
printf("Enter the Integer which u want to Convert Decimal to Binary : ");
scanf("%d",&n);
while(n>0)
{
a[d]=n%2;
n=n/2;
d++;
}
printf("After Converting Decimal to Binary is : ");
for(j=d-1;j>=0;j--)
printf("%d",a[j]);
getch();
}
Output ;
C Program Find the Highest Marks and Average
This is C program that asks user to find out the highest marks and average of students. Here declaring required variables for storing the value. User declares an array type variable in this user puts the value static up to 10 element. Using clear screen method user also uses the loop statement for vary the condition for average and marks into it. And a control statement also use for getting the condition true. if (mark[i] > highest) then the highest marks will be in mark[i].
Then finding out the average of students on the base of marks. In the end to display result on the screen.
Problem statement:
This is C program that asks user to find out the highest marks and the average.
- Declaring the variables.
- Using conditions.
- Using method for average calculating.
- Display result on the screen.
This is C program to find out highest marks and average. Output of this program shown below.
#include <stdio.h>
int main ()
{
float mark[10] = {45.6, 78.4, 65.9, 58.3, 82.1, 44.5, 61.8, 53.6, 49.2, 37.7};
int i;
float sum = 0, average, highest = 0;
clrscr();
for (i = 0; i < 10; i++)
{
sum += mark[i];
if (mark[i] > highest)
highest = mark[i];
}
average = sum / 10.0;
printf("The Average Mark is %5.2f \n", average);
printf("The Highest Mark is %5.2f \n", highest);
getch();
return 0;
}
Output :
C Program Print DINESH through Array
#include<stdio.h)
main()
{
char a[]={'D','I','N','E',’S’,’H’},i;
for(i=0;i<=5;i++)
{
printf("%c",a[i]);
}
getch();
}
C Program Print the Array Value
C Program Sum of Two Matrix
This is C Program to Sum of Two Matrix. In this program user asks to add two matrixes. The array type variables are declared for containing the value. Here loop statement comes in use for laying the condition true nested for loop used for print the matrix in it. Then after the condition as follows the loop statement value is been entered for 2×2 matrix to print. Then another for loop to add the both matrix on it. And display the result on screen.
Problem Statement:
This is C program to add two Matrices.
- Declare the variables.
- Using the loop statement.
- Display the result on the screen.
Here is source code of the C program to add two Matrices. The C program is successfully compiled. The program output is also shown below.
#include<stdio.h>
void main()
{
int a[2][2],b[2][2],c[2][2],i,j;
clrscr();
printf("Enter the value of First 2 x 2 Matrix : ");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter the value of Second 2 x 2 Matrix : ");
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
c[i][j]=a[i][j]*b[i][j];
printf("Sum of Two Matrix : %d\n",c[i][j]);
}
}
getch();
}
C Program Write a Program to Check the Day in Month
The program segment given below uses an switch statement to determine the number of days in a given month (mm). The switch statements to handle the following cases: months having 31 days, months having 30 days, February having either 29 or 28 days.
#include<stdio.h>
void main()
{
int a;
clrscr();
printf("Enter any Month : ");
scanf("%d",&a);
switch(a)
{
case 1:
printf("there are 31 days in Jan ");
break;
case 2:
printf ("there are 28 days in Feb");
break;
case 3:
printf("there are 31 days in March");
break;
case 4:
printf("there are 30 days in April");
break;
case 5:
printf(" there are 31 days in May");
break;
case 6:
printf("there are 30 days in June");
break;
case 7:
printf("there are 31 days in July");
break;
case 8:
printf("there are 31 days in August");
break;
case 9:
printf("there are 30 days in Sept.");
break;
case 10:
printf("there are 31 days in Oct.");
break;
case 11:
printf("there are 30 days in Nove.");
break;
case 12:
printf("there are 31 days in Dece.");
break;
default:
printf("it is not found ");
}
getch();
}
Output :
Enter any Month : 2
there are 28 days in Feb
C Program Using Pointers that Deletes all Occurences of a Particular Character in a String and Returns corrected String
In this program user ask to delete character from String using pointer concept. User declares char type array variable. User using malloc() function for the allocate the memory to the variable. Then user asks to enter the string then shifted the ptr to str variable for address reference. Then user puts the while condition to verify condition. In condition while (*ptr! =NULL) then if (*ptr!=ch) is that all the conditions got true the *ptr2 will be *ptr2++ with increment operator. And in the end the given result will be on the screen at the end.
Problem Statement:
This is C program where coder has to use pointer function for the deletion of character from string.
- Declare the variables.
- Using loop statement and control statements.
- Display result in the screen.
Here is C source code for Deleting the Character from the string using pointer function. The output of this program shown below.
#include<stdio.h>
void main()
{
char str[100],ch,*ptr,*ptr2;
clrscr();
ptr2=(char*)malloc(20);
printf("Enter a String :");
gets(str);
printf("Enter the Character you Want to Delete :");
scanf("%c",&ch);
ptr = str;
while(*ptr!=NULL)
{
if(*ptr!=ch)
{
*ptr2=ch;
ptr2++;
}
ptr++;
}
printf("%s",ptr2);
getch();
}
C Program Count the Lines in a File
In this program user ask to count the line in a file. User declares the required variable for storing the value and some are of pointer type. User put the method for opening file and using if-else condition to vary the condition if content is not found then terminate else with the use of while condition read the content until (ch != EOF). Then varying the condition the counted lines are on the display on the screen. In the end a method in end to close the file.
Problem Statement:
This is C Program to count out the lines on a file.
- Declaring the variables.
- Using control statements.
- Display result on the screen.
Here is C source code for counting the lines on a file. The output of this program shown below.
#include<stdio.h>
void main()
{
FILE *p;
char ch;
int l=1;
clrscr();
p=fopen("source","r");
if(p==NULL)
{
printf("File not Found");
}
else
{
ch=fgetc(p);
while(ch!=EOF)
{
printf("%c",ch);
if(ch=='\n')
{
l++;
}
ch=fgetc(p);
}
printf("Lines in a file are= %d",l);
}
fclose(p);
getch();
}
C Program Lowercase to Uppercase Text Conversion
In this program user asks to text conversion from Lower case to uppercase. User declares the char type variable. User also uses the loop statement that will execute the concept of conversion. The other function for put char that has to get the character that converted. In the end the display the result on the screen as well.
Problem statement:
This is C program that asks the user to covert the lower case character into upper case.
- Declaring the variables.
- Using conversion method.
- Display result on the screen.
Here is C source code for Converting the Character into Uppercase from lowercase. The output of this program shown below.
#include <stdio.h>
#include <ctype.h>
void main( )
{
char letter [80] ;
int count, tag;
clrscr();
for (count = 0; ( letter [ count ] = getchar()) != '\n' ; ++count)
tag = count;
for (count = 0; count < tag; ++count)
putchar(toupper(letter[count]));
getch();
}
Output :
hello dinesh
HELLO DINESH
C Program Write a Program to Find the Exponential Power
In this program user ask to find the exponential Power of value. Exponents are shorthand for repeated multiplication of the same thing by itself. User declares double type variable or float and int. two variables temp and expo assigned value 1. User asks to enter the value for expo and then the loop statement configures the condition to evaluate output. In loop statement the term variable is been manipulated with the x or I. and after all the manipulation the output is print out on the screen.
Problem Statement:
This program is to find out the exponential power of digit.
- Declaring the variable.
- Using loop statement.
- Display result on the screen.
Here is C source code for finding the exponential power. The output of this program shown below.
#include<stdio.h>
#include<conio.h>
void main()
{
double term=1,expo=1;
float x;
int i;
clrscr();
printf("Enter the exp Value of x: ");
scanf("%f",&x);
for(i=1;term>=0.00001;i++)
{
term *= x/i;
expo+=term;
}
printf("\Our Exp = %f %f\n",x,expo);
getch();
}
C Program to Print Queue
#include<stdio.h>
#include<process.h>
#define SIZE 5
int rear=-1;
int front=-1;
int a[SIZE];
void main()
{
void insert(int);
int del();
void display();
int i,j,k;
char ch;
do
{
clrscr();
printf("\n1.PUSH");
printf("\n2.POP");
printf("\n3.DISPLAY");
printf("\n4.EXIT");
printf("\nENTER YOUR CHOICE");
scanf("%d",&i);
switch(i)
{
case 1: printf("enter the item to be pushed");
scanf("%d",&j);
insert(j);
display();
break;
case 2: j=del();
printf("the item deleted is %d\n",j);
display();
break;
case 3: display();
break;
case 4: exit(0);
break;
default:printf("\n any choice");
}
fflush(stdin);
printf("want to perform any more");
scanf("%c",&ch);
}while(ch=='y');
}
void insert(int item)
{
rear++;
if(rear==0)
{
front=0;
}
else if(rear==SIZE)
{
printf("queue full");
rear--;
return;
}
a[rear]=item;
return;
}
int del()
{
int item ;
if(front==-1)
{
printf("queue under flow");
return 0;
}
else if(rear==front)
{
item=a[front];
front=-1;
}
else
{
item =a[front];
}
return item;
}
void display()
{
int i;
for(i=front;i<=rear;i++)
{
printf("%d--",a[i]);
}
return;
}
C Program Calculate Sum of each Row and each Col of a Matrix
This is C Program to calculate the sum of the elements of each row & column. A matrix is made of rows and columns. The program will add all the rows and the columns of the matrix.
In this program a composed matrix will be used to add the rows and columns. The required variables are also declared that are contain the value. Array type variables also use for the loop statement. In this program nested for loop used to computing the sum for rows and columns of a composed matrix. First loop will be add to make matrix the other one will be the computing the rows addition and the next will be do the same for columns. In the last instance the result will be displayed on the screen.
Problem Statement :
This program will evaluate the sum of each row and column from composed matrix.
- Declaring array type variables.
- Using loop statement as required.
- Display the result on the screen.
Here is source code of the C program to compute the rows and columns of composed matrix. The C program is successfully compiled. The program output is also shown below.
#include<stdio.h>
void main()
{
int a[10][10],i,j,m,n;
clrscr();
printf("\nEnter Order of Matrix : ");
scanf("%d%d",&m,&n);
printf("\nEnter Elements : ");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
for(i=0;i<m;i++)
{
a[i][n]=0;
for(j=0;j<n;j++)
a[i][n]+=a[i][j];
}
for(j=0;j<n;j++)
{
a[m][j]=0;
for(i=0;i<m;i++)
a[m][j]+=a[i][j];
}
a[m][n]=0;
for(i=0;i<m;i++)
a[m][n]+=a[i][n];
for(i=0;i<m+1;i++)
{
for(j=0;j<n+1;j++)
{
printf("%d",a[i][j]);
printf(" ");
}
printf("\n");
}
getch();
}
C Program Convert the First Char of Every Word into Uppercase
Write a Program to Copy a File using File Handling Functions.
In this program user will take command to copy the content from one file to other file with the help of file handling concept. As required user declare the variables for function regarding here user must have to create a file with content in it for copying as user set the control statement that if (args!=3) then insufficient arguments message will be on the screen. In next move user made a condition if (fp1==Null) then the existing file can’t be open and the targeted file also been neglected by the control statement. Then awhile condition comes in lime light for vary the condition to copying the file. While (ch!=EOF) until the character not been checked to end of file. Put the content of fp1 to targeted place. And then a message appears file copied on the screen as result.
Problem Statement:
This is C Program that asks user to copy file from one to other file using file handling concept.
- Declaring the variables.
- Using control statement.
- Elaborate the while condition to function.
- Display the result on the screen.
Here is C source code for Copy file from one to other file using file handling concept. The output of this program shown below.
#include<stdio.h>
#include<process.h>
void main(int argc,char *argv[])
{
FILE *fp1,*fp2;
char ch;
clrscr();
if(argc!=3)
{
printf("Insufficient Arguments");
exit(0);
}
fp1=fopen(argv[1],"r");
if(fp1==NULL)
{
printf("Source File can't be Opened");
exit(0);
}
fp2=fopen(argv[2],"w");
if(fp2==NULL)
{
printf("Target File can't be Opened");
fclose(fp1);
exit(0);
}
ch=fgetc(fp1);
while(ch!=EOF)
{
fputc(ch,fp2);
ch=fgetc(fp1);
}
printf("File Copied");
fclose(fp1);
fclose(fp2);
}
Write a Program to Define a Structure
#include<stdio.h>
struct date
{
int day,month,year;
};
struct emp
{
char code[4],name[25],dep[20];
struct date doj;
};
void main()
{
struct date tdate;
struct emp e[20];
char ename[25];
int i,n;
clrscr();
printf("\n Enter no. of employes = ");
scanf("%d",&n);
printf("\n Enter data");
for(i=0;i<n;i++)
{
printf("\n Enter code,name,department,day,month,year of joining of %d employee \n",i+1);
scanf("%s%s%s%d%d%d",e[i].code,e[i].name,e[i].dep, &e[i].doj.day,&e[i].doj.month,&e[i].doj.year);
}
printf("Enter today's date=");
scanf("%d%d%d",&tdate.day,&tdate.month,&tdate.year);
for(i=0;i<n;i++)
{
if((tdate.year>e[i].doj.year+1) || (tdate.year>e[i].doj.year&&((tdate.month==e[i].doj.month&&tdate.day>e[i].doj.day)||tdate.month>e[i].doj.day,e[i].doj.month)))
{
printf("\n Employee's data is:");
printf("%s\t%s\t%s\t%d\t%d\t%d\t",e[i].code,e[i].name,e[i].dep, e[i].doj.day,e[i].doj.month,e[i].doj.year);
}
printf("\n");
}
getch();
}
C Program to Count Digits,White Space,Others
Let us use an array of characters (str) to store the string and variables nletter, ndigit, nspace and nother to count the letters, digits, white spaces and other characters, respectively.
The program given below first reads a string from the keyboard using the gets function and then uses a while loop to determine the desired counts. An if-else-if statement is used within the while loop to test the ith character and update the respective counter. Note the use of variable ch to minimize access to the array element (possibly improving its efficiency) as well as to improve the readability of the code. Finally, the program prints the counts.
/* Count letters, digits, whitespaces and other chars in a given string */
#include <stdio.h>
void main()
{
char str [81];
int nletter, ndigit, nspace, nother; /* char counts */
int i;
clrscr();
printf("Enter a line of text:\n");
gets(str);
/* count characters in string str */
nletter = ndigit = nspace = nother = 0; /* init counts */
i = 0;
while (str[i] != '\0')
{
char ch= str[i];
if (ch>= 'A' && ch<= 'Z' || ch>= 'a' && ch<= 'z')
nletter++;
else if (ch>= '0' && ch<= '9')
ndigit++;
else if (ch == ' ' || ch =='\n' || ch == '\t')
nspace++;
else nother++;
i++;
}
/* print counts */
printf("Letters: %d \tWhite spaces : %d", nletter, nspace);
printf(" Digits : %d \tOther chars : %d\n", ndigit, nother);
getch();
}
The output of the program is given below
C Program Swap Two Number
In this program user ask to swap the numbers. User declares the variables for containing the value in it. User define the method with arguments or parameter (*int, int*). Then user assign the value to each variables then print the message to before and after value phase in each variable. Next move to call the function that declares the early in the program. Using swapping method as usual. Display the result on the screen as output.
Problem Statement
This is C program that asks user to swap two numbers.
- Declare the variables.
- Using method.
- Display the result on the screen.
Here is C source code to swap the two numbers. The output of this program shown below.
#include<stdio.h>
int swaping(int *,int *);
main()
{
int a,b;
clrscr();
a=10;
b=20;
printf ( "Before swaping a = %d & b = %d",a,b);
swaping(&a,&b);
printf("\nAfter swaping a = %d & b = %d",a,b);
getch();
}
int swaping(int * a, int * b)
{
*a=*a+*b;
*b=*a-*b;
*a=*a-*b;
}
C Program Reverse the String using Recursion
This is C Program to Reverse the String using Recursion. In this Program user ask to reverse the string with the use of recursion function. Char type variable is a casual call for containing the value. In this program majorly the reverse function will come in use and some where the use of if condition too. The first instance program asks to put the line of text. Then after the reverse function and so on but in next move the if condition comes to check the condition for reversing the string if it’s take right then the reverse function will reverse the function and then display the reversed content on the screen.
Problem Statement:
This is the program that reverses the string using recursion function.
- Declaring the Function.
- Entering the line of text.
- Using if condition.
- Display result on the screen.
Here is source code of the C program that reverses the string using recursion function. The C program is successfully compiled. The program output is also shown below.
#include<stdio.h>
void main()
{
void reverse(void);
clrscr();
printf("\nEnter a Line of Text : ");
reverse();
getch();
}
void reverse(void)
{
char c;
if((c=getchar())!='\n')
{
reverse();
putchar(c);
return;
}
}
Output :
Enter a Line of Text : Hello Dinesh
hseniD olleH
C Program Trace a Matrix
This is C program where user asks to trace a matrix with giving the order of matrix and print it. Here user declares variables that are mandatory for storing the value with it. Some variables are array type to holding value multiple. User asks to enter the no. of rows and columns as order of matrix. And then enters a number then after user varies the loop statement for situation handling to print a matrix. Nested loop statements are used in this program to elaborate the condition itself for tracing the matrix. A printing message for the matrix elements. And then display matrix on the screen as a result.
Problem Statement:
This is C Program that ask user to trace a matrix.
- Declaring the variable.
- Using loop statement.
- Display result on the screen.
Here is C source code for tracing a Matrix. The output of this program shown below.
#include<stdio.h>
void main()
{
int i,j,a[10][10],m,n;
clrscr();
printf("Enter no: of Rows : ");
scanf("%d",&m);
printf("Enter no: of Columns : ");
scanf("%d",& n);
printf("Enter a Number : ");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\nElements are :\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
getch();
}
C Program Write a Program to Check No is +Ve ,-Ve, and Zero
void main()
{
int a;
clrscr();
printf("Enter the Value of a :");
scanf("%d",&a);
if(a>0)
{
printf("a is Positive");
}
else if(a==0)
{
printf("a is Zero");
}
else
{
printf("a is Nagative");
}
getch();
}
C Program Find the Greatest Between Four Number
#include<stdio.h>
main()
{
int a,b,c,d;
clrscr();
printf("Enter the Four Numbers :");
scanf("%d %d %d %d",&a,&b,&c,&d);
if(a>b)
{
if(a>c)
{
if(a>d)
{
printf("%d is big",a);
}
else
{
printf("%d is big",d);
}
}
}
else if(b>c)
{
if(b>d)
{
printf("%d is big",b);
}
else
{
printf("%d is big",d);
}
}
else if(c>d)
{
printf("%d is big",c);
}
else
{
printf("%d is big",d);
}
getch();
}
C Program Find The Leap Year
A year is leap if it is divisible by 4 but not by 100 or is divisible by 400. Thus, 1996, 2000 and 2004 are leap years but 1900, 2002 and 2100 are not. The program segment given below determines whether a given year (of type int) is leap or not.
#include<stdio.h>
#include<conio.h>
void main()
{
int year;
clrscr();
printf("Enter a Year : ");
scanf("%d",&year);
if(year%4==0&&year%100!=0||year%400==0)
{
printf("Leap Year");
}
else
{
printf("Not a Leap Year");
}
getch();
}
Output :
Enter a Year : 2013
Not a Leap Year
C Program Enter the Basic Salary and Find The Discount Against Salary
This is C program that asks user to find out the discount against salary. User declares the variables for storing the value in it. Here user asks to put the basic salary for finding out the discount against salary. For this procedure user declares the nested if-else condition. if(a==1000) then the discount will be 200 and if a=1500 then 500 and at the else condition no discount will be printing on the screen a s result as per. Here nested if-else condition comes into the lime light.
Problem Statement:
This is C program that asks user to find out the discount against the salary.
- Declaring the variables.
- Using if-else condition.
- Display result on the screen.
Here is C source code for calculate the discount against the salary. Output of this program shown below.
#include<stdio.h>
void main()
{
int a,discount;
clrscr();
printf("Enter Basic Salary :");
scanf("%d",&a);
if(a==1000)
{
discount=200;
printf("Discount : %d",discount);
}
else if(a==1500)
{
discount=500;
printf("Discount : %d",discount);
}
else if(a==2500)
{
discount=700;
printf("Discount : %d",discount);
}
else
{
printf("No Discount");
}
getch();
}