0% found this document useful (0 votes)
17 views6 pages

Interesting Programs

Uploaded by

yevob11108
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views6 pages

Interesting Programs

Uploaded by

yevob11108
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Write a C program to print “GfG” repeatedly without using loop, recursion

and any control structure?

#include<stdio.h>
#include<stdlib.h>
int main()
{
printf("GFG");
system("test");
return 0;
}

Print “Hello World” in C/C++ without using any header file


We can easily achieve this by taking the advantage of C pre-processor directives. The fact is at the
time of compiling a program, the first phase of C preprocessing expands all header files into a single
file and after that compiler itself compiles the expanded file. Therefore we just need to extract the
declaration of printf() function from header file and use it in our main program.

// C Program
int printf(const char *format, ...);

int main() {
printf( "Hello World" );
return 0;
}

C++ language: We can’t directly put the declaration of printf() function as in previous case due to the
problem of Name mangling in C++. See this to know more about Name mangling. Therefore we just
need to declare the printf() inside extern keyword.

extern "C" {
int printf(const char *format, ...);
}

int main() {
printf( "Hello World" );
return 0;
}
Write a C program to print “Geeks for Geeks” without using a semicolon
printf() returns the total number of characters written to stdout. Therefore it can be used as a
condition check in an if condition, while condition, switch case and Macros.

#include<stdio.h>
int main()
{
if (printf("Geeks for Geeks") )
{ }
}

Write a C program to print “;” without using a semicolon

#include<stdio.h>
int main() {
// ASCII value of ; is 59
if (printf("%c", 59)) { }
}

C Program to print numbers from 1 to N without using semicolon

#include<stdio.h>
#define N 10

int main(int num, char *argv[])


{
while (num <= N && printf("%d ", num) && num++) {}
}
C program to print characters without using format specifiers

#include <stdio.h>

int main()
{
printf("\x47 \n");
printf("\x45 \n");
printf("\x45 \n");
printf("\x4b \n");
printf("\x53");
return 0;
}

C program to print a string without any quote (single or double) in the


program
The idea is to use macro processor in C. A token passed to macro can be converted to a string literal
by using # before it.

#include <stdio.h>
#define get(x) #x
int main()
{
printf(get(vignesh));
return 0;
}

What is the difference between single quoted and double quoted declaration
of char array?
In C, when a character array is initialized with a double-quoted string and the array size is not
specified, the compiler automatically allocates one extra space for string terminator ‘\0’.
#include <stdio.h>
int main()
{
// size of arr[] is 6 as it is '\0' terminated
char arr[] = "geeks";

printf("%lu", sizeof(arr));

return 0;
}

On the other hand, when the character array is initialized with comma comma-separated list of
characters and the array size is not specified, the compiler doesn’t create extra space for the string
terminator ‘\0’.

#include <stdio.h>
int main()
{
// arr[] is not terminated with '\0'
// and its size is 5
char arr[] = { 'g', 'e', 'e', 'k', 's' };

printf("%lu", sizeof(arr));

return 0;
}

Print “Even” or “Odd” without using conditional statement

#include <iostream>
using namespace std;

int main() {
int no = 8;
(no & 1 && cout << "odd" )|| cout << "even";
return 0;
}
Print substring of a given string without using any string function and loop
in C

#include <stdio.h>
// This function prints substring of str[] between low and high indexes (both inclusive).
void mysubstr(char str[], int low, int high) {
printf("%.*s", high - low + 1, (str + low));
}

int main() {
char str[] = "geeksforgeeks";
mysubstr(str, 1, 3);
return 0;
}

Sum of array Elements without using loops and recursion

#include <iostream>
using namespace std;

int operate(int array[], int N) {


int sum = 0, index = 0;

label:
sum += array[index++];
if (index < N) goto label;
return sum;
}

// Driver Code
int main() {
int N = 5, sum = 0;
int array[] = { 1, 2, 3, 4, 5 };
sum = operate(array, N);
cout << sum;
}
Print 1 to 100 in C++ Without Loops and Recursion

#include <iostream>
using namespace std;

int main()
{
short sum = 0;

update:
sum++;
cout << sum << endl;
if (sum == 100) return 0;
goto update;
}

Printing Array Elements without Indexing in C++

// C++ Program to Print Array Elements without Indexing


#include <iostream>
using namespace std;

int main(){

// Define an array
int arr[] = { 11, 22, 33, 44 };

// Print elements of an array


cout << "first element: " << *arr << endl;
cout << "Second element: " << *(arr + 1) << endl;
cout << "Third element: " << *(arr + 2) << endl;
cout << "fourth element: " << *(arr + 3) << endl;
return 0;
}

You might also like