Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
40 views

Programming For Problem Solving (PPS)

The document contains a list of 25 programming problems or experiments ranging from basic calculations and logic programs to more complex programs involving arrays, functions, recursion, structures and file handling. The programs cover fundamental programming concepts and increase in complexity.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

Programming For Problem Solving (PPS)

The document contains a list of 25 programming problems or experiments ranging from basic calculations and logic programs to more complex programs involving arrays, functions, recursion, structures and file handling. The programs cover fundamental programming concepts and increase in complexity.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

List of Experiments:

1. Write a Program to calculate sum of two numbers


2. Write a Program to calculate Simple Interest.
3. Write a Program to find larger among two numbers
4. Write a Program to find largest among three numbers
5. Write a Program to design calculator using switch case.
6. Write a Program to print 1 to 10 using loop
7. Write a Program to print even numbers from 2 to 100
8. Write a Program to print sum of digits of a number
9. Write a Program to print the reverse of a number entered by user
10. Write a Program to print table of a number
11. Write a Program to print the Fibonacci series
12. Write a Program to calculate factorial of a number
13. Write a Program to find a^b
14. Write a Program to check if number is Prime
15. Write a Program to find largest and smallest element in an array
16. Write a Program to find sum of two 2-D arrays
17. Write a Program to multiply two 2-D arrays
18. Write a Program to define function:
1. Function without argument without return value
2. Function without argument with return value
3. Function with argument without return value
4. Function with argument with return value

19. Write a Program to calculate factorial of a number using functions


20. Write a Program to find factorial using recursion
21. Write a Program to calculate marks using array of structures.
22. Write a Program to use inbuilt string functions
a) Strlen
b) Strrev
c) Strcpy
d) Strcmp
e) Strcat
23. Write a Program to check whether entered string is palindrome
24. Write a Program to find length of a string using pointers
25. Write a Program to copy the contents of one file to another file
26. Write a Program to calculate roots of a quadratic equation
PROGRAM 1
AIM: Write a Program to calculate sum of two numbers

#include<stdio.h>
#include<conio.h>
Void main()
{
int a, b, sum=0;
printf(“Enter the number in a”);
scanf(“%d”, &a);
printf(“Enter the number in b”);
scanf(“%d”, &b);
sum= a+b;
printf(“Addition of two number is %d”, sum);
getch();
}
PROGRAM 2
AIM: Write a Program to calculate Simple Interest

#include<stdio.h>
#include<conio.h>
void main()
{
int amount, rate, time, si ;
printf("\n Enter Principal Amount : ");
scanf("%d", &amount);
printf("\n Enter Rate of Interest : ");
scanf("%d", &rate);
printf("\n Enter Period of Time : ");
scanf("%d", &time);
si = (amount * rate * time) / 100;
printf("\n Simple Interest : %d", si);
getch();
}
PROGRAM 3
AIM: Write a Program to find larger among two numbers
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,large;
printf(“enter the first number :”);
scanf(“%d”,&a);
printf(“enter the second number :”);
scanf(“%d”,&b);
if(b>a)
{
printf(“largest number =%d”,b);
}
else
{
printf(“largest number =%d”, a);
}
getch();
}
PROGRAM 4
AIM: Write a Program to find largest among three numbers
#include<stdio.h>
#include<conio.h>
void main()
{
Int n1, n2, n3;
printf("Enter three different numbers: ");
scanf("%d %d %d", &n1, &n2, &n3);

if( n1>=n2 && n1>=n3 )


printf("%d is the largest number.", n1);

else if( n2>=n1 && n2>=n3 )


printf("%d is the largest number.", n2);

else
printf("%d is the largest number.", n3);

getch();
}
PROGRAM 5
Write a Program to design calculator using switch case.
#include <stdio.h>
void main()
{
int num1,num2;
float result=0;
char ch;

printf("Enter first number: ");


scanf("%d",&num1);
printf("Enter second number: ");
scanf("%d",&num2);

printf("Choose operation to perform (+,-,*,/,%): ");


scanf(" %c",&ch);
switch(ch)
{
case '+':
result=num1+num2;
break;
case '-':
result=num1-num2;
break;
case '*':
result=num1*num2;
break;
case '/':
result=(float)num1/(float)num2;
break;
case '%':
result=num1%num2;
break;
default:
printf("Invalid operation.\n");
}
printf("Result: = %f\n",result);
getch();
}
PROGRAM 6
AIM: Write a Program to print 1 to 10 using loop

#include<stdio.h>
#include<conio.h>
void main()
{
int i;
for(i=1;i<=10;i++)
printf("%d\n" ,i );
getch();
}
PROGRAM 7

AIM: Write a Program to print even numbers from 2 to 100


#include <stdio.h>
#include<conio.h>
void main() {
int counter;
printf("Even numbers between 1 to 100\n");
for(counter = 1; counter <= 100; counter++)
{
if(counter%2 == 0)
{
printf("%d ", counter);
}
}
getch();
}
PROGRAM 8

AIM: Write a Program to print sum of digits of a number

#include <stdio.h>
#include<conio.h>
void main()
{
int n, t, sum = 0, remainder;
printf("Enter an integer\n");
scanf("%d", &n);
t = n;
while (t != 0)
{
remainder = t % 10;
sum = sum + remainder;
t = t / 10;
}
printf("Sum of digits of %d = %d\n", n, sum);

getch();
}
PROGRAM 9

AIM: Write a Program to print the reverse of a number entered by user

#include <stdio.h>
int main()
#include<stdio.h>
#include<conio.h>
Void main()
{
int n, reversedNumber = 0, remainder;
printf("Enter an integer: ");
scanf("%d", &n);
while(n != 0)
{
remainder = n%10;
reversedNumber = reversedNumber*10 + remainder;
n /= 10;
}
printf("Reversed Number = %d", reversedNumber);
getch();
}
PROGRAM 10
AIM: Write a Program to print table of a number

#include <stdio.h>
#include<conio.h>
void main()
{
int n, i;
printf("Enter an integer: ");
scanf("%d",&n);
for(i=1; i<=10; ++i)
{
printf("%d * %d = %d \n", n, i, n*i);
}
getch();
}
PROGRAM 11
AIM: Write a Program to print the Fibonacci series

#include<stdio.h>
#include<conio.h>
void main()
{
int n, first = 0, second = 1, next, c;

printf("Enter the number of terms\n");


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\n",next);
}
getch();
}
PROGRAM 12
AIM: Write a Program to calculate factorial of a number
#include <stdio.h>
#include<conio.h>
int main()
{
int c, n, fact = 1;
printf("Enter a number to calculate it's factorial\n");
scanf("%d", &n);

for (c = 1; c <= n; c++)


fact = fact * c;

printf("Factorial of %d = %d\n", n, fact);

getch();
}
PROGRAM 13
AIM: Write a Program to find a^b

# include <stdio.h>
# include <conio.h>
void main()
{
int x, n, count = 1, sum = 1 ;
clrscr() ;
printf("Enter the value of x : ") ;
scanf("%d", &x) ;
printf("\nEnter the value of n : ") ;
scanf("%d", &n) ;
while(count <= n)
{
sum = sum * x ;
count ++ ;
}
printf("\nThe power of %d^%d is : %d", x, n, sum) ;
getch() ;
}
PROGRAM 14
AIM: Write a Program to check if number is Prime
#include<stdio.h>

#include<conio.h>
Void main()
{
int i,n,c=0;
printf ("Enter a number \n");
scanf ("%d",&n);
for (i=1;i<=n;i++)
{
if(n%i==0)
c=c+1;
}
if (c==2)
printf ("The number is PRIME");
else
printf ("The number is COMPOSITE");
getch();
}
PROGRAM 15
AIM: . Write a Program to find largest and smallest element in an array
#include<stdio.h>
#include<conio.h>
Void main()
{
int i, n, lar,sm, elem;
printf ("Enter total number of elements \n");
scanf ("%d", &elem);
printf ("Enter first number \n");
scanf ("%d", &n);
lar = n;
sm=n;
for (i=1; i<= elem -1 ; i++)
{
printf ("\n Enter another number \n");
scanf ("%d",&n);
if (n>lar)
lar=n;
if (n<sm)
sm=n;
}
printf ("\n The largest number is %d", lar);
printf ("\n The smallest number is %d", sm);
getch();
}
PROGRAM 16

AIM: Write a Program to find sum of two 2-D arrays


#include <stdio.h>
#include <conio.h>
void main()
{
int matrix1[10][10], matrix2[10][10], sum[10][10], i, j, m,n,p,q;
printf("Enter the order of first matrix: ");
scanf("%d%d",&m,&n);
printf("Enter the order of second matrix: ");
scanf("%d%d",&p,&q);

if(m!=p && n!=q)


{
printf("Order of matrix did not matched!!");
exit(0);
}
printf("Enter first matrix: \n");
for(i = 0 ; i < m; i++){
for(j = 0; j < n; j++)
scanf("%d", &matrix1[i][j]);
}
printf("Enter second matrix: \n");
for(i = 0 ; i < p; i++){
for(j = 0; j < q; j++)
scanf("%d", &matrix2[i][j]);
}
for(i = 0 ; i < m; i++){
for(j = 0; j < n; j++)
sum[i][j] = matrix1[i][j] + matrix2[i][j];
}
printf("The sum of the matrix is :\n");
for(i = 0 ; i < m; i++){
for(j = 0; j < n; j++){
printf("%d", sum[i][j]);
printf("\t");
}
printf("\n");
}

getch();
}
PROGRAM 17
AIM: Write a Program to multiply two 2-D arrays
#include<stdio.h>
#include<conio.h>
Void main() {
int a[10][10], b[10][10], c[10][10], i, j, k;
int sum = 0;
printf("\nEnter First Matrix : n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
scanf("%d", &a[i][j]);
}
}
printf("\nEnter Second Matrix:n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
scanf("%d", &b[i][j]);
}
}
printf("The First Matrix is: \n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
printf(" %d ", a[i][j]);
}
printf("\n");
}
printf("The Second Matrix is : \n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
printf(" %d ", b[i][j]);
}
printf("\n");
}
//Multiplication Logic
for (i = 0; i <= 2; i++) {
for (j = 0; j <= 2; j++) {
sum = 0;
for (k = 0; k <= 2; k++) {
sum = sum + a[i][k] * b[k][j];
}
c[i][j] = sum;
}
}
printf("\n Multiplication Of Two Matrices : \n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
printf(" %d ", c[i][j]);
}
printf("\n");
}
getch();
}
PROGRAM 18

C program to print largest of two numbers using function

1. (Function without argument without return value)

#include<stdio.h>
#include<conio.h>
void largest();
void main()
{
largest();
getch();
}
void largest()
{
int x, y, largest;
printf("\n Enter values for a and b:");
scanf("%d%d",&x,&y);
if(x>y)
printf("\n Largest No. is %d ",x);
else
printf("\n Largest No. is %d ",y);
}

2. (Function without argument with return value)

#include<stdio.h>
#include<conio.h>
int largest();
void main()
int large;
{
large = largest();
printf("\n Larger num is %d”, large);
getch();
}
int largest()
{
int x, y, largest;
printf("\n Enter values for a and b:");
scanf("%d%d",&x,&y);
if(x>y)
return x;
else
return y;
}

3. (Function with argument without return value)

#include<stdio.h>
#include<conio.h>
void largest(int x, int y);
void main()
{
int a, b;
printf("\n Enter values for a and b:");
scanf("%d%d",&a,&b);
largest(a,b);
getch();
}
void largest(int x, int y)
{
if(x>y)
printf("\n Largest No. is %d ",x);
else
printf("\n Largest No. is %d ",y);
}

4. (Function with argument with return value)

#include<stdio.h>
#include<conio.h>
int largest(int x, int y);
void main()
{
int a, b, large;
printf("\n Enter values for a and b:");
scanf("%d%d",&a,&b);
large = largest(a,b);
printf("\n Larger amongst %d and %d is %d\n",a, b, large);
getch();
}
int largest(int x, int y)
{
if(x>y)
return x;
else
return y; }
PROGRAM 19
AIM: Write a Program to calculate factorial of a number using functions
#include<stdio.h>
#include<conio.h>
void main()
{
int n,f;
clrscr();
printf("enter a number");
scanf("%d",&n);
f=factorial(n);
printf("the factorial of the number %d is %d",n,f);
getch();
}
int factorial(int num)
{
int fact=1,i;
{
for(i=1;i<=num;i++)
{
fact=(fact*i);
}
}
return(fact);
}
PROGRAM 20
AIM: Write a Program to calculate factorial of a number using recursion
#include<stdio.h>
int fact(int);
void main()
{
int n,t;
clrscr();
printf("Enter any Number : ");
scanf("%d",& n);
t=fact(n);
printf("Factorial of %d is : %d",n,t);
getch();
}
int fact(int a)
{
int p;
if(a==1)
return(1);
else
{
p=a*fact(a-1);
return(p);
}
}
PROGRAM 21
AIM: Write a Program to calculate marks using array of structures.
#include<stdio.h>
struct student
{
int sub1;
int sub2;
int sub3;
};
void main()
{
struct student s[10];
int i,total=0;
clrscr();
for(i=0;i<=2;i++)
{
printf("\nEnter Marks in Three Subjects = ");
scanf("%d%d%d",& s[i].sub1,&s[i].sub2,&s[i].sub3);
total=s[i].sub1+s[i].sub2+s[i].sub3;
printf("\nTotal marks of s[%d] Student= %d",i,total);
}
getch();
}
PROGRAM 22
AIM: Write a Program to use inbuilt string functions
• String length(strlen)
#include<stdio.h>
#include<conio.h>
Void main() {
char str[20];
int length ;
printf("\nEnter the String : ");
gets(str);
length = strlen(str);
printf("\nLength of String : %d ", length);
getch();
}
a. String copy (strcpy)

#include <stdio.h>
#include <string.h>
void main () {
char src[40];
char dest[40];
printf("\n Enter the String : ");
gets(src);
strcpy(dest, src);
printf("\n Source String : %s\n ", src);
printf("Final copied string : %s\n", dest);
getch();
}
b. string compare(strcmp)

#include <stdio.h>
#include <string.h>
void main()
{
char a[100], b[100];
printf("Enter the first string\n");
gets(a);
printf("Enter the second string\n");
gets(b);
if (strcmp(a,b) == 0)
printf("Entered strings are equal.\n");
else
printf("Entered strings are not equal.\n");
getch();
}

c. String Reverse (strrev)

#include<stdio.h>
#include<string.h>
void main()
{
char name[30] = "Hello";
printf("String before strrev( ) : %s\n",name);
printf("String after strrev( ) : %s",strrev(name));
getch();
}
d. String Concatenation (strcat)
#include <stdio.h>
#include <string.h>
void main()
{
char a[100], b[100];
printf("Enter the first string\n");
gets(a);
printf("Enter the second string\n");
gets(b);
strcat(a,b);
printf("String obtained on concatenation is %s\n",a);
getch();
}
PROGRAM 23
. AIM: Write a Program to check whether entered string is palindrome
#include <stdio.h>
#include <string.h>
int main(){
char string1[20];
int i, length;
int flag = 0;
printf("Enter a string:");
scanf("%s", string1);
length = strlen(string1);
for(i=0;i < length ;i++){
if(string1[i] != string1[length-i-1]){
flag = 1;
break;
}
}
if (flag) {
printf("%s is not a palindrome", string1);
}
else {
printf("%s is a palindrome", string1);
}
return 0;
}
PROGRAM 24
AIM: Write a Program to calculate marks using array of structures.
#include<stdio.h>
struct student
{
int sub1;
int sub2;
int sub3;
};
void main()
{
struct student s[10];
int i,total=0;
clrscr();
for(i=0;i<=2;i++)
{
printf("\nEnter Marks in Three Subjects = ");
scanf("%d%d%d",& s[i].sub1,&s[i].sub2,&s[i].sub3);
total=s[i].sub1+s[i].sub2+s[i].sub3;
printf("\nTotal marks of s[%d] Student= %d",i,total);
}
getch();
}
PROGRAM 25
AIM: Write a Program to copy the contents of one file to another file

#include <stdio.h>
Void main()
{
FILE *fp1, *fp2;
char ch;
fp1 = fopen("abc.txt", "r");
fp2 = fopen("xyz.txt", "w");
while((ch = getc(fp1)) != EOF)
putc(ch, fp2);
fclose(fp1);
fclose(fp2);
getch();
}

STEP 1: Create a New Text Document and Rename it as "abc.txt"

STEP 2: Write Some Content in that file and Save.

STEP 3: Now Compile the C Program and RUN it.

That's it.

Now a new file will be created with a name "xyz.c" and all the content would
copy from abc.txt to xyz.txt.
PROGRAM 26
AIM: Write a Program to calculate roots of a quadratic equation
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

void main()
{
float a, b, c, root1, root2;
float realp, imagp, disc;
printf("Enter the values of a, b and c \n");
scanf("%f %f %f", &a, &b, &c);
/* If a = 0, it is not a quadratic equation */
if (a == 0 || b == 0 || c == 0)
{
printf("Error: Roots cannot be determined \n");
exit(1);
}
else
{
disc = b * b - 4.0 * a * c;
if (disc < 0)
{
printf("Imaginary Roots\n");
realp = -b / (2.0 * a) ;
imagp = sqrt(abs(disc)) / (2.0 * a);
printf("Root1 = %f +i %f\n", realp, imagp);
printf("Root2 = %f -i %f\n", realp, imagp);
}
else if (disc == 0)
{
printf("Roots are real and equal\n");
root1 = -b / (2.0 * a);
root2 = root1;
printf("Root1 = %f\n", root1);
printf("Root2 = %f\n", root2);
}
else if (disc > 0 )
{
printf("Roots are real and distinct \n");
root1 =(-b + sqrt(disc)) / (2.0 * a);
root2 =(-b - sqrt(disc)) / (2.0 * a);
printf("Root1 = %f \n", root1);
printf("Root2 = %f \n", root2);
}}
getch();
}

You might also like