Search This Blog

Showing posts with label C Programming. Show all posts
Showing posts with label C Programming. Show all posts
Tuesday, 1 April 2025

What will be the Output of These Code Snippets?

0 comments

Q1: What will be the output of the program ? 

#include<stdio.h> 

void main() 

int a[5] = {5, 1, 15, 20, 25}; 

int i, j, m; 

i = ++a[1]; 

j = a[1]++; 

m = a[i++]; 

printf("%d, %d, %d", i, j, m); 


A) 3, 2, 15 

B) 2, 3, 20 

C) 2, 1, 15 

D) 1, 2, 5

Continue reading →
Friday, 25 November 2022

Simple Programs in C for Problem Solving

0 comments

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);
}



Continue reading →
Thursday, 6 November 2014

Managing Formatted and Unformatted I/0 in C!

0 comments

FAQ on Formatted and Unformatted I/O in C!


Distinguish between the following:  getchar() and getche()

getch() : Reads a character and never waits for Enter key. Just getch processed after getting any key pressed and it never echoes the character on screen which u pressed. 

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.

Write single C Statements for the following:
  1. Declare variables x,y and z to store floating point value
  2. Display the product of 34.54 and 5.46
  3. Display the quotient and remainder when 453 is divided by 34
  4. Prompt the user to type an integer number
  5. Read an integer variable k, that has been declared earlier
  6. Read an integer variable k, and a character variable ch that has been declared earlier
  7. Display float variable x with an accuracy of three places after the decimal point
  8. Display integer number k with a field width of 7 columns
  9. Display the character whose ASCII number is 95
Answer:
  1. float x,y,z;
  2. printf(“%f”, 34.54 * 5.46);
  3. printf(“%d%d”, 453/34, 453%34);
  4. printf(“Type an integer number : ”);
  5. scanf(“%d”, &k);
  6. scanf(“%d %c”, &k, &ch);
  7. printf(“%10.3f”, x);
  8. printf(“%7d”, k);
  9. printf(“%c”, 95);
Continue reading →
Wednesday, 29 October 2014

FAQ on C Programming!

0 comments

FREQUENTLY ASKED QUESTIONS ON C!


1.      What is a program?
A program is a sequence of instructions written to perform a well defined task with a computer.
2.      What are the types of constants?
Based on the type of data they represent, constants are of four types:  Integer constant, Floating-point constant, Character constant and String constant.
3.      Give the different data type qualifiers.
Signed, unsigned, long and short are the qualifiers that can alter the characteristics such as size or sign of a data type.
4.      Define type conversion.
Type conversion is a process of converting operands in an expression from one data type to another during execution.  There are two types of types conversion: automatic and explicit.
5.      What is the use of break statement?  (April 2013)
Break statement is used to come out of a loop while the test condition is true.  It can also be used to come out of the switch statement after executing a case block.
6.      What is an array?  (October 2012)
An array is a vector data type in which a group of related data items can be stored under a single name.
7.      What are the functions used to read strings?  (October 2012)
The functions used for reading strings into memory are: scanf, getchar and gets.
8.      Define strcat() function.
Strcat() function is used to join two strings together to form a new string (i.e., concatenated string).  Its general form is: strcat(string1, string2).
9.      Name any two functions present in conio.h header file.  (April 2013)
The functions getch() and clrscr() are the two functions present in conio.h and are frequently used in a program.
10.  Define isupper() function.
This function is used to check whether the given alphabet is in upper case or not.  It returns a non-zero integer if its argument is in upper case or else it returns zero.
11.  What is a return statement?  (October 2012)
Return statement is an executable statement, which is used to return a value to the calling program (caller) from the called one (callee).  It also returns the control back to the caller.
12.  Give the general form of structure definition.
The general form of the structure definition is as follows:
            struct tag-field{
                        data type member-1;
                        data type member-2;
                        -----------------------
                        -----------------------
                        data type member-n;
            }
            where
                        struct               – keyword to define structure
                        tag-field          – name of the structure – a valid identifier
                        datat ype         – a valid data type such as int, float etc.
13.  What is a pointer?
A pointer is a special variable that contains the address of another variable.  Through pointer, we can access the content of another variable indirectly.
14.  How a pointer variable is declared?  Give the syntax.  (October 2012)
A pointer variable can be declared using the following syntax:
            data type * variable;
where,
data type         – refers to the type of the variable pointed to by the pointer    (e.g., int, float, etc.)
*                      – is a pointer symbol
variable            – is a valid C identifier
15.  What are the types of memory allocation?  (April 2013)
Memory allocation are of two types:  static memory allocation and dynamic memory allocation.  Static memory allocation takes place during compilation, whereas dynamic memory allocation happens during run time.
16.  Define calloc() function.
calloc() is a function defined in stdio.h for the purpose of allocating the memory dynamically.  Using calloc(), we can allocate multiple blocks of contiguous memory during runtime.   
17.  Define file.
A file is a resource available in the secondary storage for storing the data permanently.  It can act as a placeholder for storing and retrieving streams of data at any point of time.
18.  Write the input functions used in file operation.
The header file stdio.h has functions like getc(), getw() and fscanf() for reading data from a file.  getc() and getw() read a single character or integer at a time, whereas fscanf() can fetch different types of data from a file at a time.
19.  Write any two error situations.
An error may occur during any one of the following file operations:
a.       When trying to read a file beyond the end-of-file marker.
b.      When trying to use a file that has not yet been opened.
20.  What is pre-processor?  (April 2013)
Preprocessor is a software module that reads the source code written in C language and performs some operation on it before it is passed on to the compiler.
Continue reading →
Thursday, 16 October 2014

Advanced Programs in C!

0 comments

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.

#include <stdio.h>
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");
    }   
}
Continue reading →
Sunday, 15 June 2014

C Programs for Solving simple Problems!

0 comments
Problem 1: 
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:
#include <stdio.h>
int main(void) {
int i, j, k;
char a[50];
printf("Enter a String for Printing:");
scanf("%s", a);
printf("\n");
for(i=0; a[i] != '\0'; i++)
{
j = i+1;
for(k=0; k<j; k++)
printf("%c", a[k]);
printf("\n");
}
}
 
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:

#include <stdio.h>

int Sum_Digits(int n)
{
int sum=0;
if(n>0)
while (n>0)
{
sum = sum + (n%10);
n = n / 10;
}
else
return 0;
return sum;
}

void main()
{
int a, res;
printf("Enter an Integer having N digits:");
scanf("%d", &a);
res = Sum_Digits(a);
printf("\nThe sum of given Number %d is: %d", a, res);
}
  
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
Continue reading →