This is my first C program!” #include void main ...
-
Upload
khangminh22 -
Category
Documents
-
view
2 -
download
0
Transcript of This is my first C program!” #include void main ...
1) Write a program to print ―”This is my first C program!”
#include<stdio.h>
void main()
{
clrscr();
printf("This is my first C program!\n");
}
2) Write a program that reads two nos. from key board and gives their addition,
subtraction, multiplication, division and modulo.
#include <stdio.h>
void main()
{
int first, second, add, subtract, multiply;
float divide;
printf("Enter two integers\n");
scanf("%d%d", &first, &second);
add = first + second;
subtract = first - second;
multiply = first * second;
divide = first / (float)second; //typecasting
printf("Sum = %d\n", add);
printf("Difference = %d\n", subtract);
printf("Multiplication = %d\n", multiply);
printf("Division = %.2f\n", divide);
}
3) Write a program to convert days into months and days.
#include<stdio.h>
#include<conio.h>
void main()
{
int m, d;
printf("Specify number of days:\n");
scanf("%d", &d);
m=d/30;
d=d%30;
printf("Months=%d Days=%d", m, d);
getch();
}
4) Write a program to solve Quadratic Equation.
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main()
{
double a, b, c, root1, root2;
printf(" Please enter a \n");
scanf("%lf", &a);
printf(" Please enter b \n");
scanf("%lf", &b);
printf(" Please enter c \n");
scanf("%lf", &c);
if (b*b-4.*a*c > 0)
{
root1 = (-b + sqrt(b*b-4.*a*c) ) / (2.*a);
root2 = (-b - sqrt(b*b-4.*a*c) ) / (2.*a);
printf("\n First root is %lf ", root1);
printf("\n Second root is %lf ", root2);
}
else
{
printf("\n Discriminant is negative! No roots!");
}
getch();
}
5) Write a program to select & print the largest of the three nos. using Nested-If-Else
statement.
#include<conio.h>
#include<stdio.h>
void main()
{
int a, b, c;
printf("Enter first number: ");
scanf("%d", &a);
printf("Enter second number: ");
scanf("%d", &b);
printf("Enter third number: ");
scanf("%d", &c);
if(a>b)
{
if(a>c)
{
printf("%d is biggest number", a);
}
else
{
printf("%d is biggest number", c);
}
}
else
{
if(b>c)
{
printf("%d is biggest number", b);
}
6) Write a program to display multiplication table.
#include <stdio.h>
#include<conio.h>
void main()
{
int n, range, i;
clrscr();
printf("Enter an integer to find multiplication table: ");
scanf("%d", &n);
printf("Enter range of multiplication table: ");
scanf("%d", &range);
for(i=1;i<=range;++i)
{
printf("%d * %d = %d\n", n, i, n*i);
}
getch();
}
7) Write a program to print 1+1/2+1/3+1/4+………+1/N series.
#include<stdio.h>
#include<conio.h>
void main()
{
double n, sum=0, i;
clrscr();
printf("\n Please give the value of n: ");
scanf("%lf", &n);
for(i=1 ; i<=n ; i++)
{
sum = sum + (1/i);
if(i==1)
printf("\n 1 +");
elseif(i==n)
printf(" (1/%d) ", i);
else
printf(" (1/%d) + ", i);
}
printf("\n\n The sum of this series is %.2lf", sum);
getch();
}
8) Write a program to find sum of all integers greater than 100 & less than 200 and are
divisible by 5.
#include<stdio.h>
#include<conio.h>
void main()
{
int i, sum = 0;
clrscr();
for(i = 100;i <= 200 ; i++)
{
if (i % 5 == 0)
sum = sum + i;
}
printf("\n Sum of all no between 100- 200 which is divisible by 5 is: %d", sum ");
getch();
}
9) The distance between two cities (in km) is input through key board. Write a program
to convert and print this distance in meters, feet, inches & centimetres.
#include<stdio.h>
#include<conio.h>
void main()
{
float km, m, feet, inch, cm;
printf("Enter the distance between two cities(in km) - ");
scanf("%f", &km);
m = km*1000; //since 1km = 1000m
feet= km*3280.84; //since 1km=3280.84feet
inch=km*39370.1; //since 1 km=39370.1inches
cm=km*100000; //since 1km = 100000cm
printf("\nDistance in kilometres = %f ", km);
printf(" \nDistance in metres = %f ", m);
printf("\nDistance in feet = %f ", feet);
printf("\nDistance in inches = %f ", inch);
printf("\nDistance in centimetres = %f ", cm);
getch();
}
10) Write a program to find sum of first N odd numbers. Ex. 1+3+5+7+………..+N.
#include<stdio.h>
void main()
{
int i, n, sum = 0;
i = 1;
clrscr();
printf(“Enter the number, n:”);
scanf("%d", &n);
while (i <= n)
{
if (i % 2 != 0)
{
sum = sum + i;
}
i++;
}
printf("Sum : %d", sum);
}
11) Write a program to demonstrate the use of:
a) getchar() function.
b) putchar() function.
a) getchar():
#include<stdio.h>
#include<conio.h>
void main()
{
char ans;
clrscr();
printf(“Would you like to know my name?\n”);
printf(“Type Y for yes and N for no:”);
ans=getchar();
if( ans==’Y’ || ans==’y’)
printf(“My name is BUSYBEE”);
else
printf(“You are good for nothing!”);
getch();
}
b) putchar():
#include<stdio.h>
#include<ctype.h>
void main()
{
char alpha;
printf(“Enter alphabet:”);
putchar(‘\n’);
alpha=getchar();
if(islower(alpha))
{
putchar(toupper(alpha));
putchar(‘\n’);
}
else
{
putchar(tolower(alpha));
putchar(‘\n’);
}
getch();
}
12) Write a program to print following patterns.
#include <stdio.h>
(a) (b)
*
* *
* * *
* * * *
* * * * *
12345
2345
345
45
5
(a)
#include<conio.h>
#include<stddio.h>
void main()
{
int i, j, rows;
clrscr();
printf("Enter the number of rows: ");
scanf("%d", &rows);
for(i=1;i<=rows;++i)
{
for(j=1;j<=i;++j)
{
printf("* ");
}
printf("\n");
}
}
b)
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j, row;
clrscr();
printf(“Enter number of rows”);
scanf(“%d”, &row);
for(i=1;i<=row;i++)
{
for(j=1;j<=row;j++)
{
if(j<i)
printf(" ");
else
printf("%d", j);
}
printf("\n");
}
getch();
}
13) Write a program to print Fibonacci series 1, 1, 2, 3, 5, ……N.
#include<stdio.h>
void main()
{
int n, first = 0, second = 1, next, c;
printf("Enter the number of terms:");
scanf("%d", &n);
printf("First %d terms of Fibonacci series are :\n", n);
for ( c = 0 ; c < n ; c++ )
{
if ( c <= 1 )
next = c;
else
{
next = first + second;
first = second;
second = next;
}
printf("%d", next);
}
}
14) Write a program to make a simple calculator to add, subtract, multiply and divide
two numbers using switch statement.
# include <stdio.h>
int main()
{
char o;
float num1, num2;
printf("Enter operator (+ - */ : )");
scanf("%c", &o);
printf("Enter two operands:\n ");
scanf("%f\n %f", &num1, &num2);
switch(o)
{
case '+':
printf("%.1f + %.1f = %.1f", num1, num2, num1+num2);
break;
case '-':
printf("%.1f - %.1f = %.1f", num1, num2, num1-num2);
break;
case '*':
printf("%.1f * %.1f = %.1f", num1, num2, num1*num2);
break;
case '/':
printf("%.1f / %.1f = %.1f", num1, num2, num1/num2);
break;
default:
/* If operator is other than +, -, * or /, error message is shown */
printf("Error! Operator is not correct");
break;
}
}
15) Write a program to add two matrices.
#include<stdio.h>
void main()
{
int i, j, mat1[10][10], mat2[10][10], mat3[10][10];
int row1, col1, row2, col2;
printf("\nEnter the number of Rows of Mat1 : ");
scanf("%d", &row1);
printf("\nEnter the number of Cols of Mat1 : ");
scanf("%d", &col1);
printf("\nEnter the number of Rows of Mat2 : ");
scanf("%d", &row2);
printf("\nEnter the number of Columns of Mat2 : ");
scanf("%d", &col2);
if (row1 != row2 || col1 != col2) {
printf("\nOrder of two matrices is not same! Hence addition not possible! ");
else
{
for (i = 0; i < row1; i++)
{
for (j = 0; j < col1; j++)
{
printf("Enter the Element a[%d][%d] : ", i, j);
scanf("%d", &mat1[i][j]);
}
}
for (i = 0; i < row2; i++)
{
for (j = 0; j < col2; j++)
{
printf("Enter the Element b[%d][%d] : ", i, j);
scanf("%d", &mat2[i][j]);
}
}
for (i = 0; i < row1; i++)
{
for (j = 0; j < col1; j++)
{
mat3[i][j] = mat1[i][j] + mat2[i][j];
}
}
printf("\nThe Addition of two Matrices is : \n");
for (i = 0; i < row1; i++)
{
for (j = 0; j < col1; j++)
{
mat3[i][j] = mat1[i][j] + mat2[i][j];
printf("%d\t", mat3[i][j]);
}
printf("\n");
}
}
getch();
}
16) Write a program to read array of integers and print it in reverse order.
#include<stdio.h>
void main()
{
int arr[30], i, j, num, temp;
printf("\nEnter no of elements : ");
scanf("%d", &num);
for (i = 0; i < num; i++)
{
scanf("%d", &arr[i]);
}
j = i – 1, i = 0;
while (i < j)
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++; // increment i
j--; // decrement j
}
printf("\nResult after reversal : ");
for (i = 0; i < num; i++)
{
printf("%d \t", arr[i]);
}
getch();
}
17) Find length of string using strlen( ) function.
#include<string.h>
#include<stdio.h>
#include<conio.h>
void main()
{
int strlength;
char *str;
clrscr();
printf("\nEnter the string: ");
gets(str);
strlength=strlen(str);
printf("\nThe length of the string is %d.", strlength);
getch();
}
18) Write a program convert character into Toggle character.
#include<stdio.h>
#include<conio.h>
int main()
{
char str[100];
int i;
clrscr();
printf("Please enter a string: ");
fgets(str, 100, stdin);
for(i=0;str[i]!=NULL;i++)
{
if(str[i]>='A'&&str[i]<='Z')
str[i]+=32;
else if(str[i]>='a'&&str[i]<='z')
str[i]-=32;
}
printf("String in toggle case is: %s", str);
getch();
}
19) Find given string is palindrome or not using string library function.
#include <stdio.h>
#include <string.h>
void main()
{
char string[25], reverse_string[25] = {'\0'};
int i, length = 0, flag = 0;
printf("\n Enter a string: ");
gets(string);
for (i = 0; string[i] != '\0'; i++)
{
length++;
}
printf("The length of the string '%s' is = %d\n", string, length);
for (i = length - 1; i >= 0 ; i--)
{
reverse_string[length - i - 1] = string[i];
}
for (flag = 1, i = 0; i < length ; i++)
{
if (reverse_string[i] != string[i])
flag = 0;
}
if (flag == 1)
printf ("%s is a palindrome \n", string);
else
printf("%s is not a palindrome \n", string);
getch();
}
20) Write a function to find out maximum out of four numbers.
#include<stdio.h>
#include<conio.h>
void main()
{
int greater(int, int);
clrscr();
int n1, n2, n3, n4, r1, r2, r3;
printf("\n Enter four Numbers:");
scanf("%d%d%d%d", &n1, &n2, &n3, &n4);
r1 = greater(n1, n2);
r2 = greater(r1, n3);
r3 = greater(r2, n4);
printf("\n The Greatest number of %d , %d , %d , %d is = %d", n1, n2, n3, n4, r3);
getch();
}
int greater(int x, int y)
{
if(x>y)
return(x);
else
return(y);
}
21) Write a function power that computes x raised to the power y for integer x and y and
returns double type value.
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<string.h>
double power(int, int);
void main()
{
int x, y;
clrscr();
printf("Enter value of x : ");
scanf("%d", &x);
printf("Enter value of y : ");
scanf("%d", &y);
printf("%d to power %d is = %f\n", x, y, power(x, y));
getch();
}
double power(int x, int y)
{
double p;
p=1.0;
if(y>=0)
while(y--)
p*=x;
else
while(y++)
p/=x;
return(p);
}
22) Write a program to find factorial of a number using recursion.
#include<stdio.h>
int factorial(int n);
int main()
{
int n;
printf("Enter a positive integer: ");
scanf("%d", &n);
printf("Factorial of %d = %ld", n, factorial(n));
return 0;
}
int factorial(int n)
{
if(n!=1)
return n*factorial(n-1);
}
23) Define a structure type, personal, that would contain person name, date of joining
and salary. Using this structure, write a program to read this information for one
person from the key board and print the same on the screen.
#include <stdio.h>
#include <iostream.h>
#include <iomanip.h>
#include <conio.h>
#include <string.h>
struct personal
{
int recno;
char name[20];
char doj[20];
int salary;
};
void main()
{
struct personal p;
clrscr();
printf("Please Enter Name");
gets(p.name);
printf("Please Enter doj");
gets(p.doj);
printf("Please Enter Salary");
scanf("%d", &p.salary);
printf("\n\n Persons Information IS\n\n");
printf("Name:%s \t DOJ:%s \t salary:%d ", p.name, p.doj, p.salary);
getch();
}
24) Write a function to enter rollno, marks of the three subject for 3 student and find
total obtained by each student.
#include<stdio.h>
#include<conio.h>
struct student
{
int rollno;
char name[20];
int m1, m2, m3;
};
void main()
{
struct student s[20], t;
int i, j, n;
clrscr();
printf("\n enter the limit");
scanf("%d", &n);
for(i=0;i<n;i++)
{
printf("\n enter the roll no\n");
scanf("%d", &s[i].rollno);
printf("\n enter the name \n");
scanf("%s", s[i].name);
printf("\n enter the mark1=");
scanf("%d", &s[i].m1);
printf("\n enter the mark2=");
scanf("%d", &s[i].m2);
printf("\n enter the mark3=");
scanf("%d", &s[i].m3);
}
for(i=0;i<n;i++)
{
printf("\n rollno=%d", s[i].rollno);
printf("\n name=%s", s[i].name);
printf("\n mark1=%d", s[i].m1);
printf("\n mark2=%d", s[i].m2);
printf("\n mark3=%d", s[i].m3);
}
}
25) Write a program to store a character string in block of memory space created by
malloc and then modify the same to store a large string.
#include <stdio.h>
#include<stdlib.h>
#define NULL 0
main()
{
char *buffer;
/* Allocating memory */
if((buffer = (char *)malloc(10)) == NULL)
{
printf(“malloc failed.\n”);
exit(1);
}
printf(“Buffer of size %d created \n”, _msize(buffer));
strcpy(buffer, “HYDERABAD”);
printf(“\nBuffer contains: %s \n “, buffer);
/* Realloction */
if((buffer = (char *)realloc(buffer, 15)) == NULL)
{
printf(“Reallocation failed. \n”);
exit(1);
}
printf(“\nBuffer size modified. \n”);
printf(“\nBuffer still contains: %s \n”, buffer);
strcpy(buffer, “SECUNDERBAD”);
printf(“\nBuffer now contains: %s \n”, buffer);
/* Freeing memory */
free(buffer);
}