Simple Programs in C for Problem Solving
Solving Problems in C using Operators
C Program for Calculating CI:
include<stdio.h>
#include<math.h>
int main(){
float pAmt, nYrs , rInt, aPaid;
printf("Enter the Principal Amount: ");
scanf("%f", &pAmt);
printf("Enter Rate Of Interest: ");
scanf("%f", &rInt);
printf("Enter the Time Period in Years: ");
scanf("%f", &nYrs);
//Formula for Compound Interest
aPaid = pAmt * (pow((1 + rInt / 100), nYrs));
printf("\nCompound Interest = %.2f/-\n", aPaid-pAmt);
return 0;
}
C Program for finding the Grade of Marks (in %):
#include <stdio.h>
void main()
{
float pMarks;
char *grade;
printf("Enter the Percentage of Marks of a Student: ");
scanf("%f", &pMarks);
grade = pMarks>100?"Not a Valid Input":(pMarks<40?"Failed":(pMarks<60?"Second Class":(pMarks<80?"First Class":"Distinction")));
puts(grade);
}
C Program for Displaying a Multiplication Table
#include<stdio.h>
void main()
{
int n, i=1;
printf("Enter the Table Number for Preparing the Table of Multiplication : ");
scanf("%d", &n);
printf("MULTIPLICATION TABLE FOR %d\n", n);
//i = i+1;
printf("%d\t*\t%d\t=\t%d\n",i++, n, i*n);
printf("%d\t*\t%d\t=\t%d\n",i++, n, i*n);
printf("%d\t*\t%d\t=\t%d\n",i++, n, i*n);
printf("%d\t*\t%d\t=\t%d\n",i++, n, i*n);
printf("%d\t*\t%d\t=\t%d\n",i++, n, i*n);
printf("%d\t*\t%d\t=\t%d\n",i++, n, i*n);
printf("%d\t*\t%d\t=\t%d\n",i++, n, i*n);
printf("%d\t*\t%d\t=\t%d\n",i++, n, i*n);
printf("%d\t*\t%d\t=\t%d\n",i++, n, i*n);
printf("%d\t*\t%d\t=\t%d\n",i++, n, i*n);
}
Managing Formatted and Unformatted I/0 in C!
FAQ on Formatted and Unformatted I/O in C!
getche() : it works same as getch() but it echoes on screen.
getchar() : It works differently from the other two. Whenever you press any key these are kept in Buffer. After hitting Enter key, the first character gets processed and it echoes on the screen.
- Declare variables x,y and z to store floating point
value
- Display the product of 34.54 and 5.46
- Display the quotient and remainder when 453 is divided
by 34
- Prompt the user to type an integer number
- Read an integer variable k, that has been declared
earlier
- Read an integer variable k, and a character variable ch
that has been declared earlier
- Display float variable x with an accuracy of three
places after the decimal point
- Display integer number k with a field width of 7 columns
- Display the character whose ASCII number is 95
- float x,y,z;
- printf(“%f”, 34.54
* 5.46);
- printf(“%d%d”,
453/34, 453%34);
- printf(“Type an
integer number : ”);
- scanf(“%d”,
&k);
- scanf(“%d %c”,
&k, &ch);
- printf(“%10.3f”,
x);
- printf(“%7d”, k);
- printf(“%c”, 95);
FAQ on C Programming!
FREQUENTLY ASKED QUESTIONS ON C!
Advanced Programs in C!
Solved Programs in C!
Program 1 :
Write a program to copy contents of one file to another file. Also find the number of characters, lines and words in the above file.
void main()
{
int cc=0, wc=1, lc=1;
char c;
FILE *fin;
FILE *fout;
fout = fopen("File1.txt","w");
printf("Enter few lines of Text with # as the last character \n");
c = getchar();
while(c != '#')
{
cc++;
putc(c, fout);
if (c == ' '||c=='\n')
wc++;
if (c== '\n')
lc++;
c = getchar();
}
fclose(fout);
printf("File created with the name File1.txt\n");
printf("# of Characters in the file is : %d\n",cc);
printf("# of Words in the file is : %d\n",wc);
printf("# of Lines in the file is : %d\n",lc);
fin = fopen("File1.txt", "r");
fout = fopen("File2.txt","w");
while (feof(fin) == 0)
{
c = getc(fin);
putc(c, fout);
}
printf("File copy from File1.txt to File2.txt is done Successfully!");
fclose(fin);
fclose(fout);
}
Program 2 :
Write a program to read a string S1 from the terminal. Again read a string S2 from the terminal and check the given string S2 in the string S1. If it does, remove S2 from the string S1 and print the updated string S1. (For example S1= Concatenate and S2 = cat, then the final result should be "Conenate"#include <stdio.h>
#include <string.h>
void main()
{
int l1=0,l2=0,i,j,k;
char str1[30], str2[30];
printf("Enter the String1 :");
gets(str1);
printf("Enter the String2 :");
gets(str2);
l1 = strlen(str1);
l2 = strlen(str2);
if(l2>l1)
printf("String2 does not exist in String1");
else
{
for(i=0; i<l1; i++)
{
for(j=0,k=i; j<l2; j++,k++)
{
if(str1[k] == str2[j])
continue;
else
break;
}
if(j==l2)
{
printf("The String2 exists in String1\n");
break;
}
}
if(j==l2)
{
printf("String1 after removing String2 from it : ");
for(i=0; i<k-j; i++)
putchar(str1[i]);
for(i=k; i<l1; i++)
putchar(str1[i]);
}
printf("\n");
}
}
C Programs for Solving simple Problems!
Write a C program, to find whether the given number N is odd or even by using conditional operator. (Don't use if elsestatement).
Solution:
#include <stdio.h>
int main(void) {
int n;
printf("Enter an Integer N:");
scanf("%d", &n);
n%2 ? printf("The Number %d is Odd", n) : printf("The Number %d is Even", n);
}
Problem 2:
Write a C program to find how many three digit numbers are available, that satisfies the following condition:
"The sum of first two digits is equal to the third digit (Example: 235, 437 etc.)"
Solution:
#include <stdio.h>
int main(void) {
// your code goes here
int i, n1, n2, n3, count=0;
for(i=100; i<=999; i++)
{
n1 = (i/100);
n2 = (i/10)%10;
n3 = i%10;
if ((n1+n2) == n3)
{
printf("%d\t", i);
count = count + 1;
}
}
printf("Total Number of 3 digit Numbers that satisfies the property is : %d", count);
}
Output:
101 112 123 134 145 156 167 178 189 202 213 224
235 246 257 268 279 303 314 325 336 347 358 369
404 415 426 437 448 459 505 516 527 538 549 606
617 628 639 707 718 729 808 819 909
Total Number of 3 digit Numbers that satisfies the property is : 45
Problem 3:
Write a C program to Read a string 'COMPUTER' and print the above string in the following format:
Solution:
Output:
Enter a String for Printing:
COMPUTER
C CO COM COMP COMPU COMPUT COMPUTE COMPUTER
Problem 4:
Write a user defined function to find the sum of individual digits of a number N and write a main program to call the above function.
Solution:
Output:
Enter an Integer having N digits:
12345
The sum of given Number 12345 is: 15
Problem 5:
Define a structure data type called 'time' containing three integer members - hour, minute and second. Develop a program that would assign values to the individual members and display the time in the format hh:mm:ss (Example: 16:40:51)
Solution:
#include <stdio.h>
struct time
{
int hh;
int mm;
int ss;
} t1;
int main(void) {
printf("Enter the Hour, Minute and Seconds of Current Time one by one:");
scanf("%d", &t1.hh);
scanf("%d", &t1.mm);
scanf("%d", &t1.ss);
printf("\nThe given Time is %d:%d:%d", t1.hh, t1.mm, t1.ss);
return 0;
}
Output:
Enter the Hour, Minute and Seconds of Current Time one by one:
16
40
51
The given Time is 16:40:51
Problem 6:
Write a C program to read through an array of integer using pointer. Also writ the code to scan through the array to find a particular value.
Solution:
#include <stdio.h>
#define N 50
int main(void) {
int n, *p, a[N], i, m;
printf("Enter the Number of elements you want to enter (Max-50):");
scanf("%d", &n);
if(n>50)
n = 50;
printf("\nEnter the elements of the Array one by one :\n");
for(i=0; i<n; i++)
scanf("%d", &a[i]);
printf("\nThe elements of the Array are:");
p = a;
for(i=0; i<n; i++,p++)
printf("%d\t", *p);
printf("\n Enter an Integer for scanning through the Array:");
scanf("%d", &m);
p = a;
for(i=0; i<n; i++,p++)
if(m == *p)
{
printf("\nThe element %d exists in the Array",m);
break;
}
if(i==n)
printf("\nThe element %d does not exist in the Array",m);
return 0;
}
Output 1:
Enter the Number of elements you want to enter (Max-50):5
Enter the elements of the Array one by one :
10
15
20
25
30
The elements of the Array are:10 15 20 25 30
Enter an Integer for scanning through the Array:20
The element 20 exists in the Array
Output 2:
Enter the Number of elements you want to enter (Max-50):4
Enter the elements of the Array one by one :
25
50
75
100
The elements of the Array are:25 50 75 100
Enter an Integer for scanning through the Array:125
The element 125 does not exist in the Array