Está en la página 1de 13

1. Write a program to print the mathematical table of numbers 1 to 10.

(Generalize it to display the table of numbers lying in a given range for


example range : between 30 and 50.)
/* C program to find multiplication table up to 10. */
#include <stdio.h>
int main()
{
int n, i;
printf("Enter an integer to find multiplication table: ");
scanf("%d",&n);
for(i=1;i<=10;++i)
{
printf("%d * %d = %d\n", n, i, n*i);
}
return 0;
}
2. Write a program to find the factorial value of a number entered by the user
and also display the maximum number that is allowed in the present
execution environment/platform. Make provision to handle negative
integers and floating numbers (i.e. wrong inputs)

3 Write a program to check whether the number is prime or not.


/* C program to check whether a number is prime or not. */
#include <stdio.h>
int main()
{
int n, i, flag=0;
printf("Enter a positive integer: ");
scanf("%d",&n);
for(i=2;i<=n/2;++i)
{
if(n%i==0)
{

flag=1;
break;

}
}
if (flag==0)
printf("%d is a prime number.",n);
else
printf("%d is not a prime number.",n);
return 0;
}

8/* Displaying Fibonacci sequence up to nth term where n is entered by user. */


#include <stdio.h>
int main()
{
int count, n, t1=0, t2=1, display=0;
printf("Enter number of terms: ");
scanf("%d",&n);
printf("Fibonacci Series: %d+%d+", t1, t2); /* Displaying first two terms */
count=2; /* count=2 because first two terms are already displayed. */
while (count<n)
{
display=t1+t2;
t1=t2;
t2=display;
++count;
printf("%d+",display);
}
return 0;
}

9/* C program to check whether a year is leap year or not using if else statement.*/
#include <stdio.h>
int main(){
int year;
printf("Enter a year: ");
scanf("%d",&year);
if(year%4 == 0)
{
if( year%100 == 0) /* Checking for a century year */

{
if ( year%400 == 0)
printf("%d is a leap year.", year);
else
printf("%d is not a leap year.", year);
}
else
printf("%d is a leap year.", year );

}
else
printf("%d is not a leap year.", year);
return 0;

10/* C programming code to check whether a character is alphabet or not.*/


#include <stdio.h>
int main()
{
char c;
printf("Enter a character: ");
scanf("%c",&c);
if( (c>='a'&& c<='z') || (c>='A' && c<='Z'))
printf("%c is an alphabet.",c);
else
printf("%c is not an alphabet.",c);
return 0;
}
11#include <stdio.h>
#include <stdlib.h>
int main() {
int i, n, *data, diff, sum, value = 1;
/* get the number of elements in AP series */
printf("Enter the value for number of elements:");
scanf("%d", &n);
/* get the common difference from the user */
printf("Common difference for AP series:");
scanf("%d", &diff);
/* allocate memory to store the elements in AP series */
data = (int *)malloc(sizeof(int) * n);

/* print the series and store the AP series in data array */


printf("AP series: ");
for (i = 0; i < n; i++) {
printf("%d ", value);
data[i] = value;
value = value + diff;
}
/* find the sum of the given AP series */
sum = (n * (data[0] + data[n - 1]))/2;
/* print the result */
printf("\nSum of the AP series is %d\n", sum);
return 0;
}

12/* C program to check whether a number is palindrome or not */


#include <stdio.h>
int main()
{
int n, reverse=0, rem,temp;
printf("Enter an integer: ");
scanf("%d", &n);
temp=n;
while(temp!=0)
{
rem=temp%10;
reverse=reverse*10+rem;
temp/=10;
}
/* Checking if number entered by user and it's reverse number is equal. */
if(reverse==n)
printf("%d is a palindrome.",n);
else
printf("%d is not a palindrome.",n);
return 0;
}
13#include <stdio.h>
int main()
{
int n, sum = 0, remainder;

printf("Enter an integer\n");
scanf("%d",&n);
while(n != 0)
{
remainder = n % 10;
sum = sum + remainder;
n = n / 10;
}
printf("Sum of digits of entered number = %d\n",sum);
return 0;
}
15/* Source code to create a simple calculator for addition, subtraction, multiplication
and division using switch...case statement in C programming. */
# include <stdio.h>
int main()
{
char o;
float num1,num2;
printf("Enter operator either + or - or * or divide : ");
scanf("%c",&o);
printf("Enter two operands: ");
scanf("%f%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;
}
return 0;
}

17-

#include <stdio.h>
#include <math.h>
void main()
{
long int x,n,xpown;
long int power(int x, int n);
printf("Enter the values of X and N\n");
scanf("%ld %ld",&x,&n);
xpown = power (x,n);
printf(" %d to the power %d = %ld\n",x,n,xpown);
}
/*Recursive function to computer the X to power N*/
long int power(int x, int n)
{
if (n==1)
return(x);
else if ( n%2 == 0)
return (pow(power(x,n/2),2)); /*if n is even*/
else
return (x*power(x, n-1));
/* if n is odd*/
}

18/* Program to find roots of a quadratic equation when coefficients are entered by user.
*/
/* Library function sqrt() computes the square root. */
#include <stdio.h>
#include <math.h> /* This is needed to use sqrt() function.*/
int main()
{
float a, b, c, determinant, r1,r2, real, imag;
printf("Enter coefficients a, b and c: ");
scanf("%f%f%f",&a,&b,&c);
determinant=b*b-4*a*c;
if (determinant>0)
{
r1= (-b+sqrt(determinant))/(2*a);
r2= (-b-sqrt(determinant))/(2*a);
printf("Roots are: %.2f and %.2f",r1 , r2);
}
else if (determinant==0)
{
r1 = r2 = -b/(2*a);
printf("Roots are: %.2f and %.2f", r1, r2);

}
else
{
real= -b/(2*a);
imag = sqrt(-determinant)/(2*a);
printf("Roots are: %.2f+%.2fi and %.2f-%.2fi", real, imag, real, imag);
}
return 0;
}
19#include <stdio.h>
#include <stdlib.h>
struct complex
{
int real, img;
};
int main()
{
int choice, temp1, temp2, temp3;
struct complex a, b, c;
while(1)
{
printf("Press 1 to add two complex numbers.\n");
printf("Press 2 to subtract two complex numbers.\n");
printf("Press 3 to multiply two complex numbers.\n");
printf("Press 4 to divide two complex numbers.\n");
printf("Press 5 to exit.\n");
printf("Enter your choice\n");
scanf("%d",&choice);
if( choice == 5)
exit(0);
if(choice >= 1 && choice <= 4)
{
printf("Enter a and b where a + ib is the first complex number.");
printf("\na = ");
scanf("%d", &a.real);
printf("b = ");
scanf("%d", &a.img);
printf("Enter c and d where c + id is the second complex number.");
printf("\nc = ");
scanf("%d", &b.real);
printf("d = ");
scanf("%d", &b.img);
}
if ( choice == 1 )
{
c.real = a.real + b.real;

c.img = a.img + b.img;


if ( c.img >= 0 )
printf("Sum of two complex numbers = %d + %di",c.real,c.img);
else
printf("Sum of two complex numbers = %d %di",c.real,c.img);
}
else if ( choice == 2 )
{
c.real = a.real - b.real;
c.img = a.img - b.img;
if ( c.img >= 0 )
printf("Difference of two complex numbers = %d + %di",c.real,c.img);
else
printf("Difference of two complex numbers = %d %di",c.real,c.img);
}
else if ( choice == 3 )
{
c.real = a.real*b.real - a.img*b.img;
c.img = a.img*b.real + a.real*b.img;
if ( c.img >= 0 )
printf("Multiplication of two complex numbers = %d +
%di",c.real,c.img);
else
printf("Multiplication of two complex numbers = %d %di",c.real,c.img);
}
else if ( choice == 4 )
{
if ( b.real == 0 && b.img == 0 )
printf("Division by 0 + 0i is not allowed.");
else
{
temp1 = a.real*b.real + a.img*b.img;
temp2 = a.img*b.real - a.real*b.img;
temp3 = b.real*b.real + b.img*b.img;
if ( temp1%temp3 == 0 && temp2%temp3 == 0 )
{
if ( temp2/temp3 >= 0)
printf("Division of two complex numbers = %d +
%di",temp1/temp3,temp2/temp3);
else
printf("Division of two complex numbers = %d
%di",temp1/temp3,temp2/temp3);
}
else if ( temp1%temp3 == 0 && temp2%temp3 != 0 )
{
if ( temp2/temp3 >= 0)
printf("Division of two complex numbers = %d + %d/
%di",temp1/temp3,temp2,temp3);
else
printf("Division of two complex numbers = %d %d/
%di",temp1/temp3,temp2,temp3);
}
else if ( temp1%temp3 != 0 && temp2%temp3 == 0 )

if ( temp2/temp3 >= 0)
printf("Division of two
%di",temp1,temp3,temp2/temp3);
else
printf("Division of two
%di",temp1,temp3,temp2/temp3);
}
else
{
if ( temp2/temp3 >= 0)
printf("Division of two
%di",temp1,temp3,temp2,temp3);
else
printf("Division of two
%di",temp1,temp3,temp2,temp3);
}
}
}
else
printf("Invalid choice.");
}

complex numbers = %d/%d +


complex numbers = %d %d/

complex numbers = %d/%d + %d/


complex numbers = %d/%d %d/

printf("\nPress any key to enter choice again...\n");

21#include<stdio.h>
int main(){
int a[50],size,i,big,small;
printf("\nEnter the size of the array: ");
scanf("%d",&size);
printf("\nEnter %d elements in to the array: ", size);
for(i=0;i<size;i++)
scanf("%d",&a[i]);
big=a[0];
for(i=1;i<size;i++){
if(big<a[i])
big=a[i];
}
printf("Largest element: %d",big);
small=a[0];
for(i=1;i<size;i++){
if(small>a[i])
small=a[i];
}
printf("Smallest element: %d",small);
return 0;
}

Sample Output:
Enter the size of the array: 4
Enter 4 elements in to the array: 2 7 8 1
Largest element: 8
Smallest element: 1
22#include<stdio.h>
#include<string.h>
int main() {
char str[100], temp;
int i, j = 0;
printf("\nEnter the string :");
gets(str);
i = 0;
j = strlen(str) - 1;
while (i < j) {
temp = str[i];
str[i] = str[j];
str[j] = temp;
i++;
j--;
}

printf("\nReverse string is :%s", str);


return (0);

23#include<conio.h>
#include<stdio.h>
void main()
{
char str[20];
int i;
clrscr();
printf("Enter any String ");
scanf("%s",str);
for(i=0;str[i]!='\0';i++)
{
if(str[i]>64&&str[i]<91)
// Uppercase characters
str[i]=str[i]+32;
// adding 32 to change to lowercase
else
if(str[i]>95&&str[i]<122)
// Lowercase characters
str[i]=str[i]-32;
// substracting 32 to change to Uppercase
}
printf("Here is the changed case: %s",str);
getch();
}

24#include <stdio.h>
int main()
{
int n, array[1000], c, d, t;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++) {
scanf("%d", &array[c]);
}
for (c = 1 ; c <= n - 1; c++) {
d = c;
while ( d > 0 && array[d] < array[d-1]) {
t
= array[d];
array[d]
= array[d-1];
array[d-1] = t;
}

d--;

}
printf("Sorted list in ascending order:\n");
for (c = 0; c <= n - 1; c++) {
printf("%d\n", array[c]);
}
}

return 0;

25#include<stdio.h>
/* Function to calculate x raised to the power y */
int power(int x, unsigned int y)
{
if( y == 0)
return 1;
else if (y%2 == 0)
return power(x, y/2)*power(x, y/2);
else
return x*power(x, y/2)*power(x, y/2);
}
/* Program to test function power */
int main()
{
int x = 2;

unsigned int y = 3;
printf("%d", power(x, y));
getchar();
return 0;

}
26#include<stdio.h>

#include<string.h>
int main() {
char str1[100];
char str2[100];
char str3[100];
int len;
printf("\nEnter the String 1 : ");
gets(str1);
printf("\nEnter the String 2 : ");
gets(str2);
strcpy(str3, str1);
strcat(str3, str2);
printf("\nConcated String : %s", str3);
return (0);
}
20#include<iostream>
using namespace std;
void expand(int);
int main()
{
int num;
cout<<"Enter a number : ";
cin>>num;
expand(num);
}
void expand(int value)
{
const char * const ones[20] = {"zero", "one", "two", "three","four","five","six","seven",
"eight","nine", "eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen",
"eighteen","nineteen"};
const char * const tens[10] = {"", "ten", "twenty", "thirty","forty","fifty","sixty","seventy",
"eighty","ninety"};
if(value<0)
{
cout<<"minus ";
expand(-value);
}
else if(value>=1000)
{
expand(value/1000);
cout<<" thousand";
if(value % 1000)
{

if(value % 1000 < 100)


{
cout << " and";
}
cout << " " ;
expand(value % 1000);
}
}
else if(value >= 100)
{
expand(value / 100);
cout<<" hundred";
if(value % 100)
{
cout << " and ";
expand (value % 100);
}
}
else if(value >= 20)
{
cout << tens[value / 10];
if(value % 10)
{
cout << " ";
expand(value % 10);
}
}
else
{
cout<<ones[value];
}
return;
}

También podría gustarte